xref: /linux/tools/lib/bpf/libbpf_common.h (revision d9c00c3b1639a3c8f46663cc042a3768d222021f)
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 #ifndef LIBBPF_API
13 #define LIBBPF_API __attribute__((visibility("default")))
14 #endif
15 
16 /* Helper macro to declare and initialize libbpf options struct
17  *
18  * This dance with uninitialized declaration, followed by memset to zero,
19  * followed by assignment using compound literal syntax is done to preserve
20  * ability to use a nice struct field initialization syntax and **hopefully**
21  * have all the padding bytes initialized to zero. It's not guaranteed though,
22  * when copying literal, that compiler won't copy garbage in literal's padding
23  * bytes, but that's the best way I've found and it seems to work in practice.
24  *
25  * Macro declares opts struct of given type and name, zero-initializes,
26  * including any extra padding, it with memset() and then assigns initial
27  * values provided by users in struct initializer-syntax as varargs.
28  */
29 #define DECLARE_LIBBPF_OPTS(TYPE, NAME, ...)				    \
30 	struct TYPE NAME = ({ 						    \
31 		memset(&NAME, 0, sizeof(struct TYPE));			    \
32 		(struct TYPE) {						    \
33 			.sz = sizeof(struct TYPE),			    \
34 			__VA_ARGS__					    \
35 		};							    \
36 	})
37 
38 #endif /* __LIBBPF_LIBBPF_COMMON_H */
39