93 lines · c
1/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */2 3/*4 * Common user-facing libbpf helpers.5 *6 * Copyright (c) 2019 Facebook7 */8 9#ifndef __LIBBPF_LIBBPF_COMMON_H10#define __LIBBPF_LIBBPF_COMMON_H11 12#include <string.h>13#include "libbpf_version.h"14 15#ifndef LIBBPF_API16#define LIBBPF_API __attribute__((visibility("default")))17#endif18 19#define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))20 21/* Mark a symbol as deprecated when libbpf version is >= {major}.{minor} */22#define LIBBPF_DEPRECATED_SINCE(major, minor, msg) \23 __LIBBPF_MARK_DEPRECATED_ ## major ## _ ## minor \24 (LIBBPF_DEPRECATED("libbpf v" # major "." # minor "+: " msg))25 26#define __LIBBPF_CURRENT_VERSION_GEQ(major, minor) \27 (LIBBPF_MAJOR_VERSION > (major) || \28 (LIBBPF_MAJOR_VERSION == (major) && LIBBPF_MINOR_VERSION >= (minor)))29 30/* Add checks for other versions below when planning deprecation of API symbols31 * with the LIBBPF_DEPRECATED_SINCE macro.32 */33#if __LIBBPF_CURRENT_VERSION_GEQ(1, 0)34#define __LIBBPF_MARK_DEPRECATED_1_0(X) X35#else36#define __LIBBPF_MARK_DEPRECATED_1_0(X)37#endif38 39/* This set of internal macros allows to do "function overloading" based on40 * number of arguments provided by used in backwards-compatible way during the41 * transition to libbpf 1.042 * It's ugly but necessary evil that will be cleaned up when we get to 1.0.43 * See bpf_prog_load() overload for example.44 */45#define ___libbpf_cat(A, B) A ## B46#define ___libbpf_select(NAME, NUM) ___libbpf_cat(NAME, NUM)47#define ___libbpf_nth(_1, _2, _3, _4, _5, _6, N, ...) N48#define ___libbpf_cnt(...) ___libbpf_nth(__VA_ARGS__, 6, 5, 4, 3, 2, 1)49#define ___libbpf_overload(NAME, ...) ___libbpf_select(NAME, ___libbpf_cnt(__VA_ARGS__))(__VA_ARGS__)50 51/* Helper macro to declare and initialize libbpf options struct52 *53 * This dance with uninitialized declaration, followed by memset to zero,54 * followed by assignment using compound literal syntax is done to preserve55 * ability to use a nice struct field initialization syntax and **hopefully**56 * have all the padding bytes initialized to zero. It's not guaranteed though,57 * when copying literal, that compiler won't copy garbage in literal's padding58 * bytes, but that's the best way I've found and it seems to work in practice.59 *60 * Macro declares opts struct of given type and name, zero-initializes,61 * including any extra padding, it with memset() and then assigns initial62 * values provided by users in struct initializer-syntax as varargs.63 */64#define LIBBPF_OPTS(TYPE, NAME, ...) \65 struct TYPE NAME = ({ \66 memset(&NAME, 0, sizeof(struct TYPE)); \67 (struct TYPE) { \68 .sz = sizeof(struct TYPE), \69 __VA_ARGS__ \70 }; \71 })72 73/* Helper macro to clear and optionally reinitialize libbpf options struct74 *75 * Small helper macro to reset all fields and to reinitialize the common76 * structure size member. Values provided by users in struct initializer-77 * syntax as varargs can be provided as well to reinitialize options struct78 * specific members.79 */80#define LIBBPF_OPTS_RESET(NAME, ...) \81 do { \82 typeof(NAME) ___##NAME = ({ \83 memset(&___##NAME, 0, sizeof(NAME)); \84 (typeof(NAME)) { \85 .sz = sizeof(NAME), \86 __VA_ARGS__ \87 }; \88 }); \89 memcpy(&NAME, &___##NAME, sizeof(NAME)); \90 } while (0)91 92#endif /* __LIBBPF_LIBBPF_COMMON_H */93