| 1 | // SPDX-License-Identifier: MIT |
| 2 | /* |
| 3 | * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | */ |
| 5 | |
| 6 | #ifndef _SIDE_MACROS_H |
| 7 | #define _SIDE_MACROS_H |
| 8 | |
| 9 | #include <stddef.h> |
| 10 | #include <limits.h> |
| 11 | |
| 12 | /* Helper macros */ |
| 13 | |
| 14 | #define SIDE_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) |
| 15 | |
| 16 | /* |
| 17 | * Compound literals with static storage are needed by SIDE |
| 18 | * instrumentation. |
| 19 | * Compound literals are part of the C99 and C11 standards, but not |
| 20 | * part of the C++ standards. They are supported by most C++ compilers |
| 21 | * though. |
| 22 | * |
| 23 | * Example use: |
| 24 | * static struct mystruct *var = LTTNG_UST_COMPOUND_LITERAL(struct mystruct, { 1, 2, 3 }); |
| 25 | */ |
| 26 | #define SIDE_COMPOUND_LITERAL(type, ...) (type[]) { __VA_ARGS__ } |
| 27 | |
| 28 | #define side_likely(x) __builtin_expect(!!(x), 1) |
| 29 | #define side_unlikely(x) __builtin_expect(!!(x), 0) |
| 30 | |
| 31 | #define SIDE_PARAM(...) __VA_ARGS__ |
| 32 | |
| 33 | /* |
| 34 | * SIDE_PARAM_SELECT_ARG1 |
| 35 | * |
| 36 | * Select second argument. Use inside macros to implement optional last |
| 37 | * macro argument, such as: |
| 38 | * |
| 39 | * #define macro(_a, _b, _c, _optional...) \ |
| 40 | * SIDE_PARAM_SELECT_ARG1(_, ##_optional, do_default_macro()) |
| 41 | * |
| 42 | * This macro is far from pretty, but attempts to create a cleaner layer |
| 43 | * on top fails for various reasons: |
| 44 | * |
| 45 | * - The libside API needs to use the default argument selection as an |
| 46 | * argument to itself (recursively), e.g. for fields and for types, so |
| 47 | * using the argument selection within an extra layer of macro fails |
| 48 | * because the extra layer cannot expand recursively. |
| 49 | * - Attempts to make the extra layer of macro support recursion through |
| 50 | * another layer of macros which expands all arguments failed because |
| 51 | * the optional argument may contain commas, and is therefore expanded |
| 52 | * into multiple arguments before argument selection, which fails to |
| 53 | * select the optional argument content after its first comma. |
| 54 | */ |
| 55 | #define SIDE_PARAM_SELECT_ARG1(_arg0, _arg1, ...) _arg1 |
| 56 | |
| 57 | /* |
| 58 | * side_container_of - Get the address of an object containing a field. |
| 59 | * |
| 60 | * @ptr: pointer to the field. |
| 61 | * @type: type of the object. |
| 62 | * @member: name of the field within the object. |
| 63 | */ |
| 64 | #define side_container_of(ptr, type, member) \ |
| 65 | __extension__ \ |
| 66 | ({ \ |
| 67 | const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \ |
| 68 | (type *)((char *)__ptr - offsetof(type, member)); \ |
| 69 | }) |
| 70 | |
| 71 | #define side_struct_field_sizeof(_struct, _field) \ |
| 72 | sizeof(((_struct * )NULL)->_field) |
| 73 | |
| 74 | #if defined(__SIZEOF_LONG__) |
| 75 | #define SIDE_BITS_PER_LONG (__SIZEOF_LONG__ * 8) |
| 76 | #elif defined(_LP64) |
| 77 | #define SIDE_BITS_PER_LONG 64 |
| 78 | #else |
| 79 | #define SIDE_BITS_PER_LONG 32 |
| 80 | #endif |
| 81 | |
| 82 | #define SIDE_PACKED __attribute__((packed)) |
| 83 | |
| 84 | #endif /* _SIDE_MACROS_H */ |