Use structure size and version for event description extensibility
[libside.git] / include / side / macros.h
CommitLineData
f611d0c3
MD
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 */
5
67337c4a
MD
6#ifndef _SIDE_MACROS_H
7#define _SIDE_MACROS_H
f611d0c3 8
6e46f5e6 9#include <stddef.h>
8e87e437 10#include <limits.h>
2e197497
MD
11#include <stdint.h>
12#include <side/endian.h>
6e46f5e6 13
f611d0c3
MD
14/* Helper macros */
15
67337c4a 16#define SIDE_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
f611d0c3
MD
17
18/*
67337c4a 19 * Compound literals with static storage are needed by SIDE
f611d0c3
MD
20 * instrumentation.
21 * Compound literals are part of the C99 and C11 standards, but not
22 * part of the C++ standards. They are supported by most C++ compilers
23 * though.
24 *
25 * Example use:
26 * static struct mystruct *var = LTTNG_UST_COMPOUND_LITERAL(struct mystruct, { 1, 2, 3 });
27 */
67337c4a 28#define SIDE_COMPOUND_LITERAL(type, ...) (type[]) { __VA_ARGS__ }
f611d0c3 29
67337c4a
MD
30#define side_likely(x) __builtin_expect(!!(x), 1)
31#define side_unlikely(x) __builtin_expect(!!(x), 0)
f611d0c3 32
67337c4a 33#define SIDE_PARAM(...) __VA_ARGS__
f611d0c3 34
441235e7
MD
35#define side_offsetofend(type, member) \
36 (offsetof(type, member) + sizeof(((type *)0)->member))
37
d0ee69ff
MD
38/*
39 * SIDE_PARAM_SELECT_ARG1
40 *
41 * Select second argument. Use inside macros to implement optional last
42 * macro argument, such as:
43 *
44 * #define macro(_a, _b, _c, _optional...) \
45 * SIDE_PARAM_SELECT_ARG1(_, ##_optional, do_default_macro())
46 *
47 * This macro is far from pretty, but attempts to create a cleaner layer
48 * on top fails for various reasons:
49 *
50 * - The libside API needs to use the default argument selection as an
51 * argument to itself (recursively), e.g. for fields and for types, so
52 * using the argument selection within an extra layer of macro fails
53 * because the extra layer cannot expand recursively.
54 * - Attempts to make the extra layer of macro support recursion through
55 * another layer of macros which expands all arguments failed because
56 * the optional argument may contain commas, and is therefore expanded
57 * into multiple arguments before argument selection, which fails to
58 * select the optional argument content after its first comma.
59 */
60#define SIDE_PARAM_SELECT_ARG1(_arg0, _arg1, ...) _arg1
61
2e197497
MD
62/*
63 * Compile time assertion.
64 * - predicate: boolean expression to evaluate,
65 * - msg: string to print to the user on failure when `static_assert()` is
66 * supported,
67 * - c_identifier_msg: message to be included in the typedef to emulate a
68 * static assertion. This parameter must be a valid C identifier as it will
69 * be used as a typedef name.
70 */
71#ifdef __cplusplus
72#define side_static_assert(predicate, msg, c_identifier_msg) \
73 static_assert(predicate, msg)
74#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
75#define side_static_assert(predicate, msg, c_identifier_msg) \
76 _Static_assert(predicate, msg)
77#else
78/*
79 * Evaluates the predicate and emit a compilation error on failure.
80 *
81 * If the predicate evaluates to true, this macro emits a function
82 * prototype with an argument type which is an array of size 0.
83 *
84 * If the predicate evaluates to false, this macro emits a function
85 * prototype with an argument type which is an array of negative size
86 * which is invalid in C and forces a compiler error. The
87 * c_identifier_msg parameter is used as the argument identifier so it
88 * is printed to the user when the error is reported.
89 */
90#define side_static_assert(predicate, msg, c_identifier_msg) \
91 void side_static_assert_proto(char c_identifier_msg[2*!!(predicate)-1])
92#endif
93
b59abc69 94/*
67337c4a 95 * side_container_of - Get the address of an object containing a field.
b59abc69
MD
96 *
97 * @ptr: pointer to the field.
98 * @type: type of the object.
99 * @member: name of the field within the object.
100 */
67337c4a 101#define side_container_of(ptr, type, member) \
b59abc69
MD
102 __extension__ \
103 ({ \
104 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
105 (type *)((char *)__ptr - offsetof(type, member)); \
106 })
107
67337c4a 108#define side_struct_field_sizeof(_struct, _field) \
88bab79c 109 sizeof(((_struct * )NULL)->_field)
8e87e437 110
2e197497
MD
111#define SIDE_PACKED __attribute__((packed))
112
113#define side_padding(bytes) char padding[bytes]
114
115#define side_check_size(_type, _len) \
116 side_static_assert(sizeof(_type) == (_len), \
117 "Unexpected size for type: `" #_type "`", \
118 unexpected_size_for_type_##_type)
119
120#if (SIDE_BYTE_ORDER == SIDE_LITTLE_ENDIAN)
121#define SIDE_ENDIAN_ORDER(_low, _high) _low, _high
f61301bb 122#else
2e197497 123#define SIDE_ENDIAN_ORDER(_low, _high) _high, _low
f61301bb
MD
124#endif
125
326c676c
MD
126/*
127 * The side_ptr macros allow defining a pointer type which is suitable
2e197497
MD
128 * for use by 32-bit, 64-bit and 128-bit kernels without compatibility
129 * code, while preserving information about the pointer type.
326c676c 130 *
2e197497
MD
131 * Those pointers are stored as 128-bit integers, and the type of the
132 * actual pointer is kept alongside with the 128-bit pointer value in a
326c676c 133 * 0-len array within a union.
326c676c
MD
134 */
135#if (__SIZEOF_POINTER__ <= 8)
2e197497 136# define side_raw_ptr_t(_type) \
326c676c 137 union { \
2e197497
MD
138 struct { \
139 uint64_t SIDE_ENDIAN_ORDER(low, high); \
140 } v; \
141 struct { \
142 _type t[0]; \
143 } SIDE_PACKED s; \
144 side_padding(16); \
326c676c
MD
145 }
146# define side_ptr_get(_field) \
2e197497 147 ((__typeof__((_field).s.t[0]))(uintptr_t)(_field).v.low)
326c676c
MD
148# define side_ptr_set(_field, _ptr) \
149 do { \
2e197497
MD
150 (_field).v.low = (uint64_t)(uintptr_t)(_ptr); \
151 (_field).v.high = 0; \
326c676c 152 } while (0)
2e197497
MD
153
154/* Keep the correct field init order to make old g++ happy. */
155# if (SIDE_BYTE_ORDER == SIDE_LITTLE_ENDIAN)
156# define SIDE_PTR_INIT(...) \
157 { \
158 .v = { \
159 .low = (uintptr_t) (__VA_ARGS__), \
160 .high = 0, \
161 }, \
162 }
163# else
164# define SIDE_PTR_INIT(...) \
165 { \
166 .v = { \
167 .high = 0, \
168 .low = (uintptr_t) (__VA_ARGS__), \
169 }, \
170 }
171# endif
172#elif (__SIZEOF_POINTER__ == 16)
173# define side_raw_ptr_t(_type) \
326c676c
MD
174 union { \
175 uintptr_t v; \
2e197497
MD
176 struct { \
177 _type t[0]; \
178 } SIDE_PACKED s; \
179 side_padding(16); \
326c676c
MD
180 }
181# define side_ptr_get(_field) \
2e197497 182 ((__typeof__((_field).s.t[0]))(_field).v)
326c676c
MD
183# define side_ptr_set(_field, _ptr) \
184 do { \
185 (_field).v = (uintptr_t)(_ptr); \
186 } while (0)
2e197497
MD
187# define SIDE_PTR_INIT(...) { .v = (uintptr_t) (__VA_ARGS__) }
188#else
189# error "Unsupported pointer size"
326c676c
MD
190#endif
191
2e197497
MD
192#define side_ptr_t(_type) side_raw_ptr_t(_type *)
193#define side_func_ptr_t(_type) side_raw_ptr_t(_type)
194
195side_static_assert(sizeof(side_ptr_t(int)) == 16,
196 "Unexpected size for side_ptr_t",
197 unexpected_size_side_ptr_t);
0e2cd351 198
f2a90e2a
MD
199/*
200 * side_enum_t allows defining fixed-sized enumerations while preserving
201 * typing information.
202 */
203#define side_enum_t(_enum_type, _size_type) \
204 union { \
205 _size_type v; \
2e197497
MD
206 struct { \
207 _enum_type t[0]; \
208 } SIDE_PACKED s; \
f2a90e2a
MD
209 }
210
211#define side_enum_get(_field) \
2e197497 212 ((__typeof__((_field).s.t[0]))(_field).v)
f2a90e2a
MD
213
214#define side_enum_set(_field, _v) \
215 do { \
216 (_field).v = (_v); \
217 } while (0)
218
219#define SIDE_ENUM_INIT(...) { .v = (__VA_ARGS__) }
220
67337c4a 221#endif /* _SIDE_MACROS_H */
This page took 0.048932 seconds and 4 git commands to generate.