Skip to content

Commit

Permalink
libbpf: Add helper macro to clear opts structs
Browse files Browse the repository at this point in the history
Add a small and generic LIBBPF_OPTS_CLEAR() helper macros which clears
an opts structure and reinitializes its .sz member to place the structure
size. I found this very useful when developing selftests, but it is also
generic enough as a macro next to the existing LIBBPF_OPTS() which hides
the .sz initialization, too.

Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
borkmann committed Jul 7, 2023
1 parent dbf28df commit 38127e9
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tools/lib/bpf/libbpf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@
}; \
})

/* Helper macro to clear a libbpf options struct
*
* Small helper macro to reset all fields and to reinitialize the common
* structure size member.
*/
#define LIBBPF_OPTS_CLEAR(NAME) \
do { \
memset(&NAME, 0, sizeof(NAME)); \
NAME.sz = sizeof(NAME); \
} while (0)

#endif /* __LIBBPF_LIBBPF_COMMON_H */

0 comments on commit 38127e9

Please sign in to comment.