Skip to content

Commit

Permalink
libbpf: Btf typed dump does not need to allocate dump data
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Alan Maguire authored and Andrii Nakryiko committed Jul 17, 2021
1 parent 04eb4df commit add192f
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions tools/lib/bpf/btf_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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';
Expand All @@ -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);
}

0 comments on commit add192f

Please sign in to comment.