xref: /openbmc/linux/tools/lib/bpf/libbpf_common.h (revision 76ce0265)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 /*
4  * Common user-facing libbpf helpers.
5  *
6  * Copyright (c) 2019 Facebook
7  */
8 
9 #ifndef __LIBBPF_LIBBPF_COMMON_H
10 #define __LIBBPF_LIBBPF_COMMON_H
11 
12 #include <string.h>
13 
14 #ifndef LIBBPF_API
15 #define LIBBPF_API __attribute__((visibility("default")))
16 #endif
17 
18 /* Helper macro to declare and initialize libbpf options struct
19  *
20  * This dance with uninitialized declaration, followed by memset to zero,
21  * followed by assignment using compound literal syntax is done to preserve
22  * ability to use a nice struct field initialization syntax and **hopefully**
23  * have all the padding bytes initialized to zero. It's not guaranteed though,
24  * when copying literal, that compiler won't copy garbage in literal's padding
25  * bytes, but that's the best way I've found and it seems to work in practice.
26  *
27  * Macro declares opts struct of given type and name, zero-initializes,
28  * including any extra padding, it with memset() and then assigns initial
29  * values provided by users in struct initializer-syntax as varargs.
30  */
31 #define DECLARE_LIBBPF_OPTS(TYPE, NAME, ...)				    \
32 	struct TYPE NAME = ({ 						    \
33 		memset(&NAME, 0, sizeof(struct TYPE));			    \
34 		(struct TYPE) {						    \
35 			.sz = sizeof(struct TYPE),			    \
36 			__VA_ARGS__					    \
37 		};							    \
38 	})
39 
40 #endif /* __LIBBPF_LIBBPF_COMMON_H */
41