From cd8e1448537c8ad27997b5a2bb27d860a5a29cf8 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Fri, 7 Jul 2023 19:24:52 +0200 Subject: [PATCH] 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 --- tools/lib/bpf/libbpf_common.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/lib/bpf/libbpf_common.h b/tools/lib/bpf/libbpf_common.h index 9a7937f339dfaa..eb180023aa97d4 100644 --- a/tools/lib/bpf/libbpf_common.h +++ b/tools/lib/bpf/libbpf_common.h @@ -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 */