Skip to content

Commit b97d0c1

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_RESET() helper macros which clears an opts structure and reinitializes its .sz member to place the structure size. Additionally, the user can pass option-specific data to reinitialize via varargs. 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 ad7cb9d commit b97d0c1

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

tools/lib/bpf/libbpf_common.h

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

73+
/* Helper macro to clear and optionally reinitialize libbpf options struct
74+
*
75+
* Small helper macro to reset all fields and to reinitialize the common
76+
* structure size member. Values provided by users in struct initializer-
77+
* syntax as varargs can be provided as well to reinitialize options struct
78+
* specific members.
79+
*/
80+
#define LIBBPF_OPTS_RESET(NAME, ...) \
81+
do { \
82+
memset(&NAME, 0, sizeof(NAME)); \
83+
NAME = (typeof(NAME)) { \
84+
.sz = sizeof(NAME), \
85+
__VA_ARGS__ \
86+
}; \
87+
} while (0)
88+
7389
#endif /* __LIBBPF_LIBBPF_COMMON_H */

0 commit comments

Comments
 (0)