libbpf: Add helper macro to clear opts structs
authorDaniel Borkmann <daniel@iogearbox.net>
Wed, 19 Jul 2023 14:08:55 +0000 (16:08 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 19 Jul 2023 17:07:28 +0000 (10:07 -0700)
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 <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20230719140858.13224-6-daniel@iogearbox.net
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/libbpf_common.h

index 9a7937f339dfaaada456fdaec057a8e36cf984e2..b7060f25448615a96e314b633cdd481aa0170e19 100644 (file)
                };                                                          \
        })
 
+/* Helper macro to clear and optionally reinitialize libbpf options struct
+ *
+ * Small helper macro to reset all fields and to reinitialize the common
+ * structure size member. Values provided by users in struct initializer-
+ * syntax as varargs can be provided as well to reinitialize options struct
+ * specific members.
+ */
+#define LIBBPF_OPTS_RESET(NAME, ...)                                       \
+       do {                                                                \
+               memset(&NAME, 0, sizeof(NAME));                             \
+               NAME = (typeof(NAME)) {                                     \
+                       .sz = sizeof(NAME),                                 \
+                       __VA_ARGS__                                         \
+               };                                                          \
+       } while (0)
+
 #endif /* __LIBBPF_LIBBPF_COMMON_H */