18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/*
48c2ecf20Sopenharmony_ci * Common user-facing libbpf helpers.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (c) 2019 Facebook
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef __LIBBPF_LIBBPF_COMMON_H
108c2ecf20Sopenharmony_ci#define __LIBBPF_LIBBPF_COMMON_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <string.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#ifndef LIBBPF_API
158c2ecf20Sopenharmony_ci#define LIBBPF_API __attribute__((visibility("default")))
168c2ecf20Sopenharmony_ci#endif
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* Helper macro to declare and initialize libbpf options struct
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * This dance with uninitialized declaration, followed by memset to zero,
238c2ecf20Sopenharmony_ci * followed by assignment using compound literal syntax is done to preserve
248c2ecf20Sopenharmony_ci * ability to use a nice struct field initialization syntax and **hopefully**
258c2ecf20Sopenharmony_ci * have all the padding bytes initialized to zero. It's not guaranteed though,
268c2ecf20Sopenharmony_ci * when copying literal, that compiler won't copy garbage in literal's padding
278c2ecf20Sopenharmony_ci * bytes, but that's the best way I've found and it seems to work in practice.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * Macro declares opts struct of given type and name, zero-initializes,
308c2ecf20Sopenharmony_ci * including any extra padding, it with memset() and then assigns initial
318c2ecf20Sopenharmony_ci * values provided by users in struct initializer-syntax as varargs.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci#define DECLARE_LIBBPF_OPTS(TYPE, NAME, ...)				    \
348c2ecf20Sopenharmony_ci	struct TYPE NAME = ({ 						    \
358c2ecf20Sopenharmony_ci		memset(&NAME, 0, sizeof(struct TYPE));			    \
368c2ecf20Sopenharmony_ci		(struct TYPE) {						    \
378c2ecf20Sopenharmony_ci			.sz = sizeof(struct TYPE),			    \
388c2ecf20Sopenharmony_ci			__VA_ARGS__					    \
398c2ecf20Sopenharmony_ci		};							    \
408c2ecf20Sopenharmony_ci	})
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#endif /* __LIBBPF_LIBBPF_COMMON_H */
43