]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fundamental/macro-fundamental.h
Merge pull request #23504 from keszybz/bls-reordering
[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
ZJS
5# include <assert.h>
6# include <stddef.h>
e5bc5f1f
YW
7#endif
8
a36a0d15 9#include <limits.h>
e514b507 10#include "types-fundamental.h"
e5bc5f1f 11
9137c03c 12#define _align_(x) __attribute__((__aligned__(x)))
e5bc5f1f
YW
13#define _const_ __attribute__((__const__))
14#define _pure_ __attribute__((__pure__))
9137c03c 15#define _section_(x) __attribute__((__section__(x)))
1328150d 16#define _packed_ __attribute__((__packed__))
a3aff1c4 17#define _retain_ __attribute__((__retain__))
9137c03c 18#define _used_ __attribute__((__used__))
e5bc5f1f
YW
19#define _unused_ __attribute__((__unused__))
20#define _cleanup_(x) __attribute__((__cleanup__(x)))
f862e847
JJ
21#define _likely_(x) (__builtin_expect(!!(x), 1))
22#define _unlikely_(x) (__builtin_expect(!!(x), 0))
23#if __GNUC__ >= 7
24#define _fallthrough_ __attribute__((__fallthrough__))
25#else
26#define _fallthrough_
27#endif
f862e847 28#define _noreturn_ _Noreturn
e5bc5f1f 29
9137c03c
DJL
30#define XSTRINGIFY(x) #x
31#define STRINGIFY(x) XSTRINGIFY(x)
32
e5bc5f1f
YW
33#ifndef __COVERITY__
34# define VOID_0 ((void)0)
35#else
36# define VOID_0 ((void*)0)
37#endif
38
39#define ELEMENTSOF(x) \
40 (__builtin_choose_expr( \
41 !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
42 sizeof(x)/sizeof((x)[0]), \
43 VOID_0))
44
45#define XCONCATENATE(x, y) x ## y
46#define CONCATENATE(x, y) XCONCATENATE(x, y)
47
48#ifdef SD_BOOT
b1672234 49 _noreturn_ void efi_assert(const char *expr, const char *file, unsigned line, const char *function);
3b23a6c4 50
7a7267bf
JJ
51 #ifdef NDEBUG
52 #define assert(expr)
53 #define assert_not_reached() __builtin_unreachable()
54 #else
7a7267bf
JJ
55 #define assert(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); })
56 #define assert_not_reached() efi_assert("Code should not be reached", __FILE__, __LINE__, __PRETTY_FUNCTION__)
57 #endif
6c405f20 58 #define static_assert _Static_assert
3b23a6c4 59 #define assert_se(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); })
5d8a725b
LB
60
61 #define memcpy(a, b, c) CopyMem((a), (b), (c))
200b1d99 62 #define free(a) FreePool(a)
e5bc5f1f
YW
63#endif
64
d821e40c
ZJS
65/* This passes the argument through after (if asserts are enabled) checking that it is not null. */
66#define ASSERT_PTR(expr) \
67 ({ \
68 typeof(expr) _expr_ = (expr); \
69 assert(_expr_); \
70 _expr_; \
71 })
72
8890ec82
LP
73#define ASSERT_SE_PTR(expr) \
74 ({ \
75 typeof(expr) _expr_ = (expr); \
76 assert_se(_expr_); \
77 _expr_; \
78 })
79
6c405f20
JJ
80#define assert_cc(expr) static_assert(expr, #expr)
81
e5bc5f1f
YW
82
83#define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
84#define UNIQ __COUNTER__
85
ea42da38
DS
86/* Note that this works differently from pthread_once(): this macro does
87 * not synchronize code execution, i.e. code that is run conditionalized
88 * on this macro will run concurrently to all other code conditionalized
89 * the same way, there's no ordering or completion enforced. */
90#define ONCE __ONCE(UNIQ_T(_once_, UNIQ))
91#define __ONCE(o) \
92 ({ \
e7cef2a6 93 static sd_bool (o) = sd_false; \
f22abf38 94 __sync_bool_compare_and_swap(&(o), sd_false, sd_true); \
ea42da38
DS
95 })
96
e5bc5f1f
YW
97#undef MAX
98#define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
99#define __MAX(aq, a, bq, b) \
100 ({ \
101 const typeof(a) UNIQ_T(A, aq) = (a); \
102 const typeof(b) UNIQ_T(B, bq) = (b); \
103 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
104 })
105
addae96a
LP
106#define IS_UNSIGNED_INTEGER_TYPE(type) \
107 (__builtin_types_compatible_p(typeof(type), unsigned char) || \
108 __builtin_types_compatible_p(typeof(type), unsigned short) || \
109 __builtin_types_compatible_p(typeof(type), unsigned) || \
110 __builtin_types_compatible_p(typeof(type), unsigned long) || \
111 __builtin_types_compatible_p(typeof(type), unsigned long long))
112
113#define IS_SIGNED_INTEGER_TYPE(type) \
114 (__builtin_types_compatible_p(typeof(type), signed char) || \
115 __builtin_types_compatible_p(typeof(type), signed short) || \
116 __builtin_types_compatible_p(typeof(type), signed) || \
117 __builtin_types_compatible_p(typeof(type), signed long) || \
118 __builtin_types_compatible_p(typeof(type), signed long long))
119
120/* Evaluates to (void) if _A or _B are not constant or of different types (being integers of different sizes
121 * is also OK as long as the signedness matches) */
e5bc5f1f
YW
122#define CONST_MAX(_A, _B) \
123 (__builtin_choose_expr( \
124 __builtin_constant_p(_A) && \
125 __builtin_constant_p(_B) && \
addae96a
LP
126 (__builtin_types_compatible_p(typeof(_A), typeof(_B)) || \
127 (IS_UNSIGNED_INTEGER_TYPE(_A) && IS_UNSIGNED_INTEGER_TYPE(_B)) || \
128 (IS_SIGNED_INTEGER_TYPE(_A) && IS_SIGNED_INTEGER_TYPE(_B))), \
e5bc5f1f
YW
129 ((_A) > (_B)) ? (_A) : (_B), \
130 VOID_0))
131
132/* takes two types and returns the size of the larger one */
133#define MAXSIZE(A, B) (sizeof(union _packed_ { typeof(A) a; typeof(B) b; }))
134
135#define MAX3(x, y, z) \
136 ({ \
137 const typeof(x) _c = MAX(x, y); \
138 MAX(_c, z); \
139 })
140
141#define MAX4(x, y, z, a) \
142 ({ \
143 const typeof(x) _d = MAX3(x, y, z); \
144 MAX(_d, a); \
145 })
146
147#undef MIN
148#define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
149#define __MIN(aq, a, bq, b) \
150 ({ \
151 const typeof(a) UNIQ_T(A, aq) = (a); \
152 const typeof(b) UNIQ_T(B, bq) = (b); \
153 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
154 })
155
156/* evaluates to (void) if _A or _B are not constant or of different types */
157#define CONST_MIN(_A, _B) \
158 (__builtin_choose_expr( \
159 __builtin_constant_p(_A) && \
160 __builtin_constant_p(_B) && \
161 __builtin_types_compatible_p(typeof(_A), typeof(_B)), \
162 ((_A) < (_B)) ? (_A) : (_B), \
163 VOID_0))
164
165#define MIN3(x, y, z) \
166 ({ \
167 const typeof(x) _c = MIN(x, y); \
168 MIN(_c, z); \
169 })
170
171#define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b))
172#define __LESS_BY(aq, a, bq, b) \
173 ({ \
174 const typeof(a) UNIQ_T(A, aq) = (a); \
175 const typeof(b) UNIQ_T(B, bq) = (b); \
176 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) - UNIQ_T(B, bq) : 0; \
177 })
178
179#define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
180#define __CMP(aq, a, bq, b) \
181 ({ \
182 const typeof(a) UNIQ_T(A, aq) = (a); \
183 const typeof(b) UNIQ_T(B, bq) = (b); \
184 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 : \
185 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0; \
186 })
187
188#undef CLAMP
189#define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high))
190#define __CLAMP(xq, x, lowq, low, highq, high) \
191 ({ \
192 const typeof(x) UNIQ_T(X, xq) = (x); \
193 const typeof(low) UNIQ_T(LOW, lowq) = (low); \
194 const typeof(high) UNIQ_T(HIGH, highq) = (high); \
195 UNIQ_T(X, xq) > UNIQ_T(HIGH, highq) ? \
196 UNIQ_T(HIGH, highq) : \
197 UNIQ_T(X, xq) < UNIQ_T(LOW, lowq) ? \
198 UNIQ_T(LOW, lowq) : \
199 UNIQ_T(X, xq); \
200 })
201
202/* [(x + y - 1) / y] suffers from an integer overflow, even though the
203 * computation should be possible in the given type. Therefore, we use
204 * [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
205 * quotient and the remainder, so both should be equally fast. */
206#define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, (x), UNIQ, (y))
207#define __DIV_ROUND_UP(xq, x, yq, y) \
208 ({ \
209 const typeof(x) UNIQ_T(X, xq) = (x); \
210 const typeof(y) UNIQ_T(Y, yq) = (y); \
211 (UNIQ_T(X, xq) / UNIQ_T(Y, yq) + !!(UNIQ_T(X, xq) % UNIQ_T(Y, yq))); \
212 })
213
214#define CASE_F(X) case X:
215#define CASE_F_1(CASE, X) CASE_F(X)
216#define CASE_F_2(CASE, X, ...) CASE(X) CASE_F_1(CASE, __VA_ARGS__)
217#define CASE_F_3(CASE, X, ...) CASE(X) CASE_F_2(CASE, __VA_ARGS__)
218#define CASE_F_4(CASE, X, ...) CASE(X) CASE_F_3(CASE, __VA_ARGS__)
219#define CASE_F_5(CASE, X, ...) CASE(X) CASE_F_4(CASE, __VA_ARGS__)
220#define CASE_F_6(CASE, X, ...) CASE(X) CASE_F_5(CASE, __VA_ARGS__)
221#define CASE_F_7(CASE, X, ...) CASE(X) CASE_F_6(CASE, __VA_ARGS__)
222#define CASE_F_8(CASE, X, ...) CASE(X) CASE_F_7(CASE, __VA_ARGS__)
223#define CASE_F_9(CASE, X, ...) CASE(X) CASE_F_8(CASE, __VA_ARGS__)
224#define CASE_F_10(CASE, X, ...) CASE(X) CASE_F_9(CASE, __VA_ARGS__)
225#define CASE_F_11(CASE, X, ...) CASE(X) CASE_F_10(CASE, __VA_ARGS__)
226#define CASE_F_12(CASE, X, ...) CASE(X) CASE_F_11(CASE, __VA_ARGS__)
227#define CASE_F_13(CASE, X, ...) CASE(X) CASE_F_12(CASE, __VA_ARGS__)
228#define CASE_F_14(CASE, X, ...) CASE(X) CASE_F_13(CASE, __VA_ARGS__)
229#define CASE_F_15(CASE, X, ...) CASE(X) CASE_F_14(CASE, __VA_ARGS__)
230#define CASE_F_16(CASE, X, ...) CASE(X) CASE_F_15(CASE, __VA_ARGS__)
231#define CASE_F_17(CASE, X, ...) CASE(X) CASE_F_16(CASE, __VA_ARGS__)
232#define CASE_F_18(CASE, X, ...) CASE(X) CASE_F_17(CASE, __VA_ARGS__)
233#define CASE_F_19(CASE, X, ...) CASE(X) CASE_F_18(CASE, __VA_ARGS__)
234#define CASE_F_20(CASE, X, ...) CASE(X) CASE_F_19(CASE, __VA_ARGS__)
235
236#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
237#define FOR_EACH_MAKE_CASE(...) \
238 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, \
239 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) \
240 (CASE_F,__VA_ARGS__)
241
79893116
YW
242#define IN_SET(x, ...) \
243 ({ \
244 sd_bool _found = sd_false; \
e5bc5f1f
YW
245 /* If the build breaks in the line below, you need to extend the case macros. (We use "long double" as \
246 * type for the array, in the hope that checkers such as ubsan don't complain that the initializers for \
247 * the array are not representable by the base type. Ideally we'd use typeof(x) as base type, but that \
248 * doesn't work, as we want to use this on bitfields and gcc refuses typeof() on bitfields.) */ \
249 static const long double __assert_in_set[] _unused_ = { __VA_ARGS__ }; \
79893116
YW
250 assert_cc(ELEMENTSOF(__assert_in_set) <= 20); \
251 switch (x) { \
252 FOR_EACH_MAKE_CASE(__VA_ARGS__) \
253 _found = sd_true; \
254 break; \
255 default: \
256 break; \
257 } \
258 _found; \
e5bc5f1f
YW
259 })
260
261/* Takes inspiration from Rust's Option::take() method: reads and returns a pointer, but at the same time
262 * resets it to NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
263#define TAKE_PTR(ptr) \
264 ({ \
b7759c8f
LP
265 typeof(ptr) *_pptr_ = &(ptr); \
266 typeof(ptr) _ptr_ = *_pptr_; \
267 *_pptr_ = NULL; \
e5bc5f1f
YW
268 _ptr_; \
269 })
f862e847
JJ
270
271/*
272 * STRLEN - return the length of a string literal, minus the trailing NUL byte.
273 * Contrary to strlen(), this is a constant expression.
274 * @x: a string literal.
275 */
276#define STRLEN(x) (sizeof(""x"") - sizeof(typeof(x[0])))
200b1d99
MR
277
278#define mfree(memory) \
279 ({ \
280 free(memory); \
281 (typeof(memory)) NULL; \
282 })
a36a0d15
JJ
283
284static inline size_t ALIGN_TO(size_t l, size_t ali) {
285 /* sd-boot uses UINTN for size_t, let's make sure SIZE_MAX is correct. */
286 assert_cc(SIZE_MAX == ~(size_t)0);
287
288 /* Check that alignment is exponent of 2 */
289#if SIZE_MAX == UINT_MAX
290 assert(__builtin_popcount(ali) == 1);
291#elif SIZE_MAX == ULONG_MAX
292 assert(__builtin_popcountl(ali) == 1);
293#elif SIZE_MAX == ULLONG_MAX
294 assert(__builtin_popcountll(ali) == 1);
295#else
296 #error "Unexpected size_t"
297#endif
298
299 if (l > SIZE_MAX - (ali - 1))
300 return SIZE_MAX; /* indicate overflow */
301
302 return ((l + ali - 1) & ~(ali - 1));
303}
304
305/* Same as ALIGN_TO but callable in constant contexts. */
306#define CONST_ALIGN_TO(l, ali) \
307 __builtin_choose_expr( \
308 __builtin_constant_p(l) && \
309 __builtin_constant_p(ali) && \
310 __builtin_popcountll(ali) == 1 && /* is power of 2? */ \
311 (l <= SIZE_MAX - (ali - 1)), /* overflow? */ \
312 ((l) + (ali) - 1) & ~((ali) - 1), \
313 VOID_0)
a8a7723b
JJ
314
315#define UPDATE_FLAG(orig, flag, b) \
316 ((b) ? ((orig) | (flag)) : ((orig) & ~(flag)))
317#define SET_FLAG(v, flag, b) \
318 (v) = UPDATE_FLAG(v, flag, b)
319#define FLAGS_SET(v, flags) \
320 ((~(v) & (flags)) == 0)