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