]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fundamental/macro-fundamental.h
Merge pull request #25915 from poettering/arm-timer-rel
[thirdparty/systemd.git] / src / fundamental / macro-fundamental.h
CommitLineData
e5bc5f1f
YW
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2#pragma once
3
4#ifndef SD_BOOT
c3e4cbe0 5# include <assert.h>
e5bc5f1f
YW
6#endif
7
a36a0d15 8#include <limits.h>
6b852d22
JJ
9#include <stdbool.h>
10#include <stddef.h>
11#include <stdint.h>
e5bc5f1f 12
9137c03c 13#define _align_(x) __attribute__((__aligned__(x)))
b41ebe3d
JJ
14#define _alignas_(x) __attribute__((__aligned__(__alignof__(x))))
15#define _alignptr_ __attribute__((__aligned__(sizeof(void *))))
16#define _cleanup_(x) __attribute__((__cleanup__(x)))
e5bc5f1f 17#define _const_ __attribute__((__const__))
b41ebe3d
JJ
18#define _deprecated_ __attribute__((__deprecated__))
19#define _destructor_ __attribute__((__destructor__))
20#define _hidden_ __attribute__((__visibility__("hidden")))
21#define _likely_(x) (__builtin_expect(!!(x), 1))
22#define _malloc_ __attribute__((__malloc__))
23#define _noreturn_ _Noreturn
1328150d 24#define _packed_ __attribute__((__packed__))
b41ebe3d
JJ
25#define _printf_(a, b) __attribute__((__format__(printf, a, b)))
26#define _public_ __attribute__((__visibility__("default")))
27#define _pure_ __attribute__((__pure__))
a3aff1c4 28#define _retain_ __attribute__((__retain__))
9148312f 29#define _returns_nonnull_ __attribute__((__returns_nonnull__))
b41ebe3d
JJ
30#define _section_(x) __attribute__((__section__(x)))
31#define _sentinel_ __attribute__((__sentinel__))
f862e847 32#define _unlikely_(x) (__builtin_expect(!!(x), 0))
b41ebe3d
JJ
33#define _unused_ __attribute__((__unused__))
34#define _used_ __attribute__((__used__))
35#define _warn_unused_result_ __attribute__((__warn_unused_result__))
36#define _weak_ __attribute__((__weak__))
37#define _weakref_(x) __attribute__((__weakref__(#x)))
38
39#ifdef __clang__
40# define _alloc_(...)
41#else
42# define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__)))
43#endif
44
da519f8c 45#if __GNUC__ >= 7 || (defined(__clang__) && __clang_major__ >= 10)
b41ebe3d 46# define _fallthrough_ __attribute__((__fallthrough__))
f862e847 47#else
b41ebe3d 48# define _fallthrough_
f862e847 49#endif
e5bc5f1f 50
9137c03c
DJL
51#define XSTRINGIFY(x) #x
52#define STRINGIFY(x) XSTRINGIFY(x)
53
e5bc5f1f
YW
54#ifndef __COVERITY__
55# define VOID_0 ((void)0)
56#else
57# define VOID_0 ((void*)0)
58#endif
59
60#define ELEMENTSOF(x) \
61 (__builtin_choose_expr( \
62 !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
63 sizeof(x)/sizeof((x)[0]), \
64 VOID_0))
65
66#define XCONCATENATE(x, y) x ## y
67#define CONCATENATE(x, y) XCONCATENATE(x, y)
68
69#ifdef SD_BOOT
b1672234 70 _noreturn_ void efi_assert(const char *expr, const char *file, unsigned line, const char *function);
3b23a6c4 71
7a7267bf
JJ
72 #ifdef NDEBUG
73 #define assert(expr)
74 #define assert_not_reached() __builtin_unreachable()
75 #else
7a7267bf
JJ
76 #define assert(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); })
77 #define assert_not_reached() efi_assert("Code should not be reached", __FILE__, __LINE__, __PRETTY_FUNCTION__)
78 #endif
6c405f20 79 #define static_assert _Static_assert
3b23a6c4 80 #define assert_se(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); })
e5bc5f1f
YW
81#endif
82
d821e40c 83/* This passes the argument through after (if asserts are enabled) checking that it is not null. */
23cd0025
DT
84#define ASSERT_PTR(expr) _ASSERT_PTR(expr, UNIQ_T(_expr_, UNIQ), assert)
85#define ASSERT_SE_PTR(expr) _ASSERT_PTR(expr, UNIQ_T(_expr_, UNIQ), assert_se)
86#define _ASSERT_PTR(expr, var, check) \
87 ({ \
88 typeof(expr) var = (expr); \
89 check(var); \
90 var; \
8890ec82
LP
91 })
92
724e13b3 93#define ASSERT_NONNEG(expr) \
94 ({ \
95 typeof(expr) _expr_ = (expr), _zero = 0; \
96 assert(_expr_ >= _zero); \
97 _expr_; \
98 })
99
100#define ASSERT_SE_NONNEG(expr) \
101 ({ \
102 typeof(expr) _expr_ = (expr), _zero = 0; \
103 assert_se(_expr_ >= _zero); \
104 _expr_; \
105 })
106
6c405f20
JJ
107#define assert_cc(expr) static_assert(expr, #expr)
108
e5bc5f1f
YW
109
110#define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
111#define UNIQ __COUNTER__
112
ea42da38
DS
113/* Note that this works differently from pthread_once(): this macro does
114 * not synchronize code execution, i.e. code that is run conditionalized
115 * on this macro will run concurrently to all other code conditionalized
116 * the same way, there's no ordering or completion enforced. */
117#define ONCE __ONCE(UNIQ_T(_once_, UNIQ))
9ddb63f5 118#define __ONCE(o) \
119 ({ \
120 static bool (o) = false; \
121 __atomic_exchange_n(&(o), true, __ATOMIC_SEQ_CST); \
ea42da38
DS
122 })
123
e5bc5f1f
YW
124#undef MAX
125#define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
126#define __MAX(aq, a, bq, b) \
127 ({ \
128 const typeof(a) UNIQ_T(A, aq) = (a); \
129 const typeof(b) UNIQ_T(B, bq) = (b); \
130 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
131 })
132
addae96a
LP
133#define IS_UNSIGNED_INTEGER_TYPE(type) \
134 (__builtin_types_compatible_p(typeof(type), unsigned char) || \
135 __builtin_types_compatible_p(typeof(type), unsigned short) || \
136 __builtin_types_compatible_p(typeof(type), unsigned) || \
137 __builtin_types_compatible_p(typeof(type), unsigned long) || \
138 __builtin_types_compatible_p(typeof(type), unsigned long long))
139
140#define IS_SIGNED_INTEGER_TYPE(type) \
141 (__builtin_types_compatible_p(typeof(type), signed char) || \
142 __builtin_types_compatible_p(typeof(type), signed short) || \
143 __builtin_types_compatible_p(typeof(type), signed) || \
144 __builtin_types_compatible_p(typeof(type), signed long) || \
145 __builtin_types_compatible_p(typeof(type), signed long long))
146
147/* Evaluates to (void) if _A or _B are not constant or of different types (being integers of different sizes
148 * is also OK as long as the signedness matches) */
e5bc5f1f
YW
149#define CONST_MAX(_A, _B) \
150 (__builtin_choose_expr( \
151 __builtin_constant_p(_A) && \
152 __builtin_constant_p(_B) && \
addae96a
LP
153 (__builtin_types_compatible_p(typeof(_A), typeof(_B)) || \
154 (IS_UNSIGNED_INTEGER_TYPE(_A) && IS_UNSIGNED_INTEGER_TYPE(_B)) || \
155 (IS_SIGNED_INTEGER_TYPE(_A) && IS_SIGNED_INTEGER_TYPE(_B))), \
e5bc5f1f
YW
156 ((_A) > (_B)) ? (_A) : (_B), \
157 VOID_0))
158
159/* takes two types and returns the size of the larger one */
160#define MAXSIZE(A, B) (sizeof(union _packed_ { typeof(A) a; typeof(B) b; }))
161
162#define MAX3(x, y, z) \
163 ({ \
164 const typeof(x) _c = MAX(x, y); \
165 MAX(_c, z); \
166 })
167
168#define MAX4(x, y, z, a) \
169 ({ \
170 const typeof(x) _d = MAX3(x, y, z); \
171 MAX(_d, a); \
172 })
173
174#undef MIN
175#define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
176#define __MIN(aq, a, bq, b) \
177 ({ \
178 const typeof(a) UNIQ_T(A, aq) = (a); \
179 const typeof(b) UNIQ_T(B, bq) = (b); \
180 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
181 })
182
183/* evaluates to (void) if _A or _B are not constant or of different types */
184#define CONST_MIN(_A, _B) \
185 (__builtin_choose_expr( \
186 __builtin_constant_p(_A) && \
187 __builtin_constant_p(_B) && \
188 __builtin_types_compatible_p(typeof(_A), typeof(_B)), \
189 ((_A) < (_B)) ? (_A) : (_B), \
190 VOID_0))
191
192#define MIN3(x, y, z) \
193 ({ \
194 const typeof(x) _c = MIN(x, y); \
195 MIN(_c, z); \
196 })
197
c51e4c79
LP
198/* Returns true if the passed integer is a positive power of two */
199#define CONST_ISPOWEROF2(x) \
200 ((x) > 0 && ((x) & ((x) - 1)) == 0)
201
202#define ISPOWEROF2(x) \
203 __builtin_choose_expr( \
204 __builtin_constant_p(x), \
205 CONST_ISPOWEROF2(x), \
206 ({ \
207 const typeof(x) _x = (x); \
208 CONST_ISPOWEROF2(_x); \
209 }))
210
e5bc5f1f
YW
211#define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b))
212#define __LESS_BY(aq, a, bq, b) \
213 ({ \
214 const typeof(a) UNIQ_T(A, aq) = (a); \
215 const typeof(b) UNIQ_T(B, bq) = (b); \
216 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) - UNIQ_T(B, bq) : 0; \
217 })
218
219#define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
220#define __CMP(aq, a, bq, b) \
221 ({ \
222 const typeof(a) UNIQ_T(A, aq) = (a); \
223 const typeof(b) UNIQ_T(B, bq) = (b); \
224 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 : \
225 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0; \
226 })
227
228#undef CLAMP
229#define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high))
230#define __CLAMP(xq, x, lowq, low, highq, high) \
231 ({ \
232 const typeof(x) UNIQ_T(X, xq) = (x); \
233 const typeof(low) UNIQ_T(LOW, lowq) = (low); \
234 const typeof(high) UNIQ_T(HIGH, highq) = (high); \
235 UNIQ_T(X, xq) > UNIQ_T(HIGH, highq) ? \
236 UNIQ_T(HIGH, highq) : \
237 UNIQ_T(X, xq) < UNIQ_T(LOW, lowq) ? \
238 UNIQ_T(LOW, lowq) : \
239 UNIQ_T(X, xq); \
240 })
241
242/* [(x + y - 1) / y] suffers from an integer overflow, even though the
243 * computation should be possible in the given type. Therefore, we use
244 * [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
245 * quotient and the remainder, so both should be equally fast. */
246#define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, (x), UNIQ, (y))
247#define __DIV_ROUND_UP(xq, x, yq, y) \
248 ({ \
249 const typeof(x) UNIQ_T(X, xq) = (x); \
250 const typeof(y) UNIQ_T(Y, yq) = (y); \
251 (UNIQ_T(X, xq) / UNIQ_T(Y, yq) + !!(UNIQ_T(X, xq) % UNIQ_T(Y, yq))); \
252 })
253
790f4dda
JJ
254#define CASE_F_1(X) case X:
255#define CASE_F_2(X, ...) case X: CASE_F_1( __VA_ARGS__)
256#define CASE_F_3(X, ...) case X: CASE_F_2( __VA_ARGS__)
257#define CASE_F_4(X, ...) case X: CASE_F_3( __VA_ARGS__)
258#define CASE_F_5(X, ...) case X: CASE_F_4( __VA_ARGS__)
259#define CASE_F_6(X, ...) case X: CASE_F_5( __VA_ARGS__)
260#define CASE_F_7(X, ...) case X: CASE_F_6( __VA_ARGS__)
261#define CASE_F_8(X, ...) case X: CASE_F_7( __VA_ARGS__)
262#define CASE_F_9(X, ...) case X: CASE_F_8( __VA_ARGS__)
263#define CASE_F_10(X, ...) case X: CASE_F_9( __VA_ARGS__)
264#define CASE_F_11(X, ...) case X: CASE_F_10( __VA_ARGS__)
265#define CASE_F_12(X, ...) case X: CASE_F_11( __VA_ARGS__)
266#define CASE_F_13(X, ...) case X: CASE_F_12( __VA_ARGS__)
267#define CASE_F_14(X, ...) case X: CASE_F_13( __VA_ARGS__)
268#define CASE_F_15(X, ...) case X: CASE_F_14( __VA_ARGS__)
269#define CASE_F_16(X, ...) case X: CASE_F_15( __VA_ARGS__)
270#define CASE_F_17(X, ...) case X: CASE_F_16( __VA_ARGS__)
271#define CASE_F_18(X, ...) case X: CASE_F_17( __VA_ARGS__)
272#define CASE_F_19(X, ...) case X: CASE_F_18( __VA_ARGS__)
273#define CASE_F_20(X, ...) case X: CASE_F_19( __VA_ARGS__)
e5bc5f1f
YW
274
275#define GET_CASE_F(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,NAME,...) NAME
276#define FOR_EACH_MAKE_CASE(...) \
277 GET_CASE_F(__VA_ARGS__,CASE_F_20,CASE_F_19,CASE_F_18,CASE_F_17,CASE_F_16,CASE_F_15,CASE_F_14,CASE_F_13,CASE_F_12,CASE_F_11, \
278 CASE_F_10,CASE_F_9,CASE_F_8,CASE_F_7,CASE_F_6,CASE_F_5,CASE_F_4,CASE_F_3,CASE_F_2,CASE_F_1) \
790f4dda 279 (__VA_ARGS__)
e5bc5f1f 280
4f06325c 281#define IN_SET(x, first, ...) \
79893116 282 ({ \
6b852d22 283 bool _found = false; \
0bc4ac52
JJ
284 /* If the build breaks in the line below, you need to extend the case macros. We use typeof(+x) \
285 * here to widen the type of x if it is a bit-field as this would otherwise be illegal. */ \
4f06325c 286 static const typeof(+x) __assert_in_set[] _unused_ = { first, __VA_ARGS__ }; \
79893116
YW
287 assert_cc(ELEMENTSOF(__assert_in_set) <= 20); \
288 switch (x) { \
4f06325c 289 FOR_EACH_MAKE_CASE(first, __VA_ARGS__) \
6b852d22 290 _found = true; \
e967926b 291 break; \
79893116
YW
292 default: \
293 break; \
294 } \
295 _found; \
e5bc5f1f
YW
296 })
297
298/* Takes inspiration from Rust's Option::take() method: reads and returns a pointer, but at the same time
299 * resets it to NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
300#define TAKE_PTR(ptr) \
301 ({ \
b7759c8f
LP
302 typeof(ptr) *_pptr_ = &(ptr); \
303 typeof(ptr) _ptr_ = *_pptr_; \
304 *_pptr_ = NULL; \
e5bc5f1f
YW
305 _ptr_; \
306 })
f862e847
JJ
307
308/*
309 * STRLEN - return the length of a string literal, minus the trailing NUL byte.
310 * Contrary to strlen(), this is a constant expression.
311 * @x: a string literal.
312 */
313#define STRLEN(x) (sizeof(""x"") - sizeof(typeof(x[0])))
200b1d99
MR
314
315#define mfree(memory) \
316 ({ \
317 free(memory); \
318 (typeof(memory)) NULL; \
319 })
a36a0d15
JJ
320
321static inline size_t ALIGN_TO(size_t l, size_t ali) {
983ce0b5 322 assert(ISPOWEROF2(ali));
a36a0d15
JJ
323
324 if (l > SIZE_MAX - (ali - 1))
325 return SIZE_MAX; /* indicate overflow */
326
327 return ((l + ali - 1) & ~(ali - 1));
328}
329
4f073883 330#define ALIGN2(l) ALIGN_TO(l, 2)
4c8d7caf
YW
331#define ALIGN4(l) ALIGN_TO(l, 4)
332#define ALIGN8(l) ALIGN_TO(l, 8)
4f073883
LP
333#define ALIGN2_PTR(p) ((void*) ALIGN2((uintptr_t) p))
334#define ALIGN4_PTR(p) ((void*) ALIGN4((uintptr_t) p))
335#define ALIGN8_PTR(p) ((void*) ALIGN8((uintptr_t) p))
4c8d7caf
YW
336#ifndef SD_BOOT
337/* libefi also provides ALIGN, and we do not use them in sd-boot explicitly. */
338#define ALIGN(l) ALIGN_TO(l, sizeof(void*))
339#define ALIGN_PTR(p) ((void*) ALIGN((uintptr_t) (p)))
340#endif
341
4f073883
LP
342/* Checks if the specified pointer is aligned as appropriate for the specific type */
343#define IS_ALIGNED16(p) (((uintptr_t) p) % __alignof__(uint16_t) == 0)
344#define IS_ALIGNED32(p) (((uintptr_t) p) % __alignof__(uint32_t) == 0)
345#define IS_ALIGNED64(p) (((uintptr_t) p) % __alignof__(uint64_t) == 0)
346
a36a0d15
JJ
347/* Same as ALIGN_TO but callable in constant contexts. */
348#define CONST_ALIGN_TO(l, ali) \
349 __builtin_choose_expr( \
350 __builtin_constant_p(l) && \
351 __builtin_constant_p(ali) && \
983ce0b5 352 CONST_ISPOWEROF2(ali) && \
a36a0d15
JJ
353 (l <= SIZE_MAX - (ali - 1)), /* overflow? */ \
354 ((l) + (ali) - 1) & ~((ali) - 1), \
355 VOID_0)
a8a7723b 356
86bdf117
TH
357/* Similar to ((t *) (void *) (p)) to cast a pointer. The macro asserts that the pointer has a suitable
358 * alignment for type "t". This exists for places where otherwise "-Wcast-align=strict" would issue a
359 * warning or if you want to assert that the cast gives a pointer of suitable alignment. */
360#define CAST_ALIGN_PTR(t, p) \
361 ({ \
362 const void *_p = (p); \
363 assert(((uintptr_t) _p) % __alignof__(t) == 0); \
364 (t *) _p; \
365 })
366
a8a7723b
JJ
367#define UPDATE_FLAG(orig, flag, b) \
368 ((b) ? ((orig) | (flag)) : ((orig) & ~(flag)))
369#define SET_FLAG(v, flag, b) \
370 (v) = UPDATE_FLAG(v, flag, b)
371#define FLAGS_SET(v, flags) \
372 ((~(v) & (flags)) == 0)