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