summaryrefslogtreecommitdiff
path: root/tools/lib/bpf/btf_dump.c
diff options
context:
space:
mode:
authorAlan Maguire <alan.maguire@oracle.com>2021-07-17 01:46:57 +0300
committerAndrii Nakryiko <andrii@kernel.org>2021-07-17 03:29:56 +0300
commitadd192f81ab21b58471577c75e7be9c9add98223 (patch)
tree6c8b85f1aef8f28ef64175e20b8fa09212d00cd6 /tools/lib/bpf/btf_dump.c
parent04eb4dff6a64d842f7f2c85c7cb1affc5ab3ebc9 (diff)
downloadlinux-add192f81ab21b58471577c75e7be9c9add98223.tar.xz
libbpf: Btf typed dump does not need to allocate dump data
By using the stack for this small structure, we avoid the need for freeing memory in error paths. Suggested-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alan Maguire <alan.maguire@oracle.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/1626475617-25984-4-git-send-email-alan.maguire@oracle.com
Diffstat (limited to 'tools/lib/bpf/btf_dump.c')
-rw-r--r--tools/lib/bpf/btf_dump.c9
1 files changed, 4 insertions, 5 deletions
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index aa695ab9b826..accf6fea57da 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -2238,6 +2238,7 @@ int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
const void *data, size_t data_sz,
const struct btf_dump_type_data_opts *opts)
{
+ struct btf_dump_data typed_dump = {};
const struct btf_type *t;
int ret;
@@ -2248,12 +2249,10 @@ int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
if (!t)
return libbpf_err(-ENOENT);
- d->typed_dump = calloc(1, sizeof(struct btf_dump_data));
- if (!d->typed_dump)
- return libbpf_err(-ENOMEM);
-
+ d->typed_dump = &typed_dump;
d->typed_dump->data_end = data + data_sz;
d->typed_dump->indent_lvl = OPTS_GET(opts, indent_level, 0);
+
/* default indent string is a tab */
if (!opts->indent_str)
d->typed_dump->indent_str[0] = '\t';
@@ -2267,7 +2266,7 @@ int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
ret = btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0);
- free(d->typed_dump);
+ d->typed_dump = NULL;
return libbpf_err(ret);
}