Skip to content

Commit 02221a3

Browse files
borkmannKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
libbpf: Add helper macro to clear opts structs
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]>
1 parent efb92ef commit 02221a3

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

tools/lib/bpf/libbpf_common.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,15 @@
7070
}; \
7171
})
7272

73+
/* Helper macro to clear a libbpf options struct
74+
*
75+
* Small helper macro to reset all fields and to reinitialize the common
76+
* structure size member.
77+
*/
78+
#define LIBBPF_OPTS_CLEAR(NAME) \
79+
do { \
80+
memset(&NAME, 0, sizeof(NAME)); \
81+
NAME.sz = sizeof(NAME); \
82+
} while (0)
83+
7384
#endif /* __LIBBPF_LIBBPF_COMMON_H */

0 commit comments

Comments
 (0)