Skip to content

Commit

Permalink
tools/libpf: Add offsetof/container_of macro in bpf_helpers.h
Browse files Browse the repository at this point in the history
These two helpers will be used later in bpf_iter bpf program
bpf_iter_netlink.c. Put them in bpf_helpers.h since they could
be useful in other cases.

Signed-off-by: Yonghong Song <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Acked-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
yonghong-song authored and Alexei Starovoitov committed May 10, 2020
1 parent c09add2 commit 5fbc220
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tools/lib/bpf/bpf_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
#define __weak __attribute__((weak))
#endif

/*
* Helper macro to manipulate data structures
*/
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
#endif
#ifndef container_of
#define container_of(ptr, type, member) \
({ \
void *__mptr = (void *)(ptr); \
((type *)(__mptr - offsetof(type, member))); \
})
#endif

/*
* Helper structure used by eBPF C program
* to describe BPF map attributes to libbpf loader
Expand Down

0 comments on commit 5fbc220

Please sign in to comment.