]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/macro.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include <sys/sysmacros.h>
10 #include <sys/types.h>
12 #define _printf_(a, b) __attribute__((__format__(printf, a, b)))
16 # define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__)))
18 #define _sentinel_ __attribute__((__sentinel__))
19 #define _section_(x) __attribute__((__section__(x)))
20 #define _used_ __attribute__((__used__))
21 #define _unused_ __attribute__((__unused__))
22 #define _destructor_ __attribute__((__destructor__))
23 #define _pure_ __attribute__((__pure__))
24 #define _const_ __attribute__((__const__))
25 #define _deprecated_ __attribute__((__deprecated__))
26 #define _packed_ __attribute__((__packed__))
27 #define _malloc_ __attribute__((__malloc__))
28 #define _weak_ __attribute__((__weak__))
29 #define _likely_(x) (__builtin_expect(!!(x), 1))
30 #define _unlikely_(x) (__builtin_expect(!!(x), 0))
31 #define _public_ __attribute__((__visibility__("default")))
32 #define _hidden_ __attribute__((__visibility__("hidden")))
33 #define _weakref_(x) __attribute__((__weakref__(#x)))
34 #define _align_(x) __attribute__((__aligned__(x)))
35 #define _alignas_(x) __attribute__((__aligned__(__alignof(x))))
36 #define _alignptr_ __attribute__((__aligned__(sizeof(void*))))
37 #define _cleanup_(x) __attribute__((__cleanup__(x)))
39 #define _fallthrough_ __attribute__((__fallthrough__))
43 /* Define C11 noreturn without <stdnoreturn.h> and even on older gcc
44 * compiler versions */
46 #if __STDC_VERSION__ >= 201112L
47 #define _noreturn_ _Noreturn
49 #define _noreturn_ __attribute__((__noreturn__))
53 #if !defined(HAS_FEATURE_MEMORY_SANITIZER)
54 # if defined(__has_feature)
55 # if __has_feature(memory_sanitizer)
56 # define HAS_FEATURE_MEMORY_SANITIZER 1
59 # if !defined(HAS_FEATURE_MEMORY_SANITIZER)
60 # define HAS_FEATURE_MEMORY_SANITIZER 0
64 #if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
65 # ifdef __SANITIZE_ADDRESS__
66 # define HAS_FEATURE_ADDRESS_SANITIZER 1
67 # elif defined(__has_feature)
68 # if __has_feature(address_sanitizer)
69 # define HAS_FEATURE_ADDRESS_SANITIZER 1
72 # if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
73 # define HAS_FEATURE_ADDRESS_SANITIZER 0
77 /* Note: on GCC "no_sanitize_address" is a function attribute only, on llvm it may also be applied to global
78 * variables. We define a specific macro which knows this. Note that on GCC we don't need this decorator so much, since
79 * our primary usecase for this attribute is registration structures placed in named ELF sections which shall not be
80 * padded, but GCC doesn't pad those anyway if AddressSanitizer is enabled. */
81 #if HAS_FEATURE_ADDRESS_SANITIZER && defined(__clang__)
82 #define _variable_no_sanitize_address_ __attribute__((__no_sanitize_address__))
84 #define _variable_no_sanitize_address_
87 /* Temporarily disable some warnings */
88 #define DISABLE_WARNING_FORMAT_NONLITERAL \
89 _Pragma("GCC diagnostic push"); \
90 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
92 #define DISABLE_WARNING_MISSING_PROTOTYPES \
93 _Pragma("GCC diagnostic push"); \
94 _Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"")
96 #define DISABLE_WARNING_NONNULL \
97 _Pragma("GCC diagnostic push"); \
98 _Pragma("GCC diagnostic ignored \"-Wnonnull\"")
100 #define DISABLE_WARNING_SHADOW \
101 _Pragma("GCC diagnostic push"); \
102 _Pragma("GCC diagnostic ignored \"-Wshadow\"")
104 #define DISABLE_WARNING_INCOMPATIBLE_POINTER_TYPES \
105 _Pragma("GCC diagnostic push"); \
106 _Pragma("GCC diagnostic ignored \"-Wincompatible-pointer-types\"")
108 #define REENABLE_WARNING \
109 _Pragma("GCC diagnostic pop")
111 /* automake test harness */
112 #define EXIT_TEST_SKIP 77
114 #define XSTRINGIFY(x) #x
115 #define STRINGIFY(x) XSTRINGIFY(x)
117 #define XCONCATENATE(x, y) x ## y
118 #define CONCATENATE(x, y) XCONCATENATE(x, y)
120 #define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
121 #define UNIQ __COUNTER__
124 #if __SIZEOF_INT__ == 4
125 #define BUILTIN_FFS_U32(x) __builtin_ffs(x);
126 #elif __SIZEOF_LONG__ == 4
127 #define BUILTIN_FFS_U32(x) __builtin_ffsl(x);
129 #error "neither int nor long are four bytes long?!?"
134 #define ALIGN4(l) (((l) + 3) & ~3)
135 #define ALIGN8(l) (((l) + 7) & ~7)
137 #if __SIZEOF_POINTER__ == 8
138 #define ALIGN(l) ALIGN8(l)
139 #elif __SIZEOF_POINTER__ == 4
140 #define ALIGN(l) ALIGN4(l)
142 #error "Wut? Pointers are neither 4 nor 8 bytes long?"
145 #define ALIGN_PTR(p) ((void*) ALIGN((unsigned long) (p)))
146 #define ALIGN4_PTR(p) ((void*) ALIGN4((unsigned long) (p)))
147 #define ALIGN8_PTR(p) ((void*) ALIGN8((unsigned long) (p)))
149 static inline size_t ALIGN_TO(size_t l
, size_t ali
) {
150 return ((l
+ ali
- 1) & ~(ali
- 1));
153 #define ALIGN_TO_PTR(p, ali) ((void*) ALIGN_TO((unsigned long) (p), (ali)))
155 /* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */
156 static inline unsigned long ALIGN_POWER2(unsigned long u
) {
157 /* clz(0) is undefined */
161 /* left-shift overflow is undefined */
162 if (__builtin_clzl(u
- 1UL) < 1)
165 return 1UL << (sizeof(u
) * 8 - __builtin_clzl(u
- 1UL));
169 # define VOID_0 ((void)0)
171 # define VOID_0 ((void*)0)
174 #define ELEMENTSOF(x) \
175 (__builtin_choose_expr( \
176 !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
177 sizeof(x)/sizeof((x)[0]), \
181 * STRLEN - return the length of a string literal, minus the trailing NUL byte.
182 * Contrary to strlen(), this is a constant expression.
183 * @x: a string literal.
185 #define STRLEN(x) (sizeof(""x"") - 1)
188 * container_of - cast a member of a structure out to the containing structure
189 * @ptr: the pointer to the member.
190 * @type: the type of the container struct this is embedded in.
191 * @member: the name of the member within the struct.
193 #define container_of(ptr, type, member) __container_of(UNIQ, (ptr), type, member)
194 #define __container_of(uniq, ptr, type, member) \
196 const typeof( ((type*)0)->member ) *UNIQ_T(A, uniq) = (ptr); \
197 (type*)( (char *)UNIQ_T(A, uniq) - offsetof(type, member) ); \
201 #define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
202 #define __MAX(aq, a, bq, b) \
204 const typeof(a) UNIQ_T(A, aq) = (a); \
205 const typeof(b) UNIQ_T(B, bq) = (b); \
206 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
209 /* evaluates to (void) if _A or _B are not constant or of different types */
210 #define CONST_MAX(_A, _B) \
211 (__builtin_choose_expr( \
212 __builtin_constant_p(_A) && \
213 __builtin_constant_p(_B) && \
214 __builtin_types_compatible_p(typeof(_A), typeof(_B)), \
215 ((_A) > (_B)) ? (_A) : (_B), \
218 /* takes two types and returns the size of the larger one */
219 #define MAXSIZE(A, B) (sizeof(union _packed_ { typeof(A) a; typeof(B) b; }))
221 #define MAX3(x, y, z) \
223 const typeof(x) _c = MAX(x, y); \
228 #define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
229 #define __MIN(aq, a, bq, b) \
231 const typeof(a) UNIQ_T(A, aq) = (a); \
232 const typeof(b) UNIQ_T(B, bq) = (b); \
233 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
236 #define MIN3(x, y, z) \
238 const typeof(x) _c = MIN(x, y); \
242 #define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b))
243 #define __LESS_BY(aq, a, bq, b) \
245 const typeof(a) UNIQ_T(A, aq) = (a); \
246 const typeof(b) UNIQ_T(B, bq) = (b); \
247 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) - UNIQ_T(B, bq) : 0; \
250 #define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
251 #define __CMP(aq, a, bq, b) \
253 const typeof(a) UNIQ_T(A, aq) = (a); \
254 const typeof(b) UNIQ_T(B, bq) = (b); \
255 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 : \
256 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0; \
260 #define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high))
261 #define __CLAMP(xq, x, lowq, low, highq, high) \
263 const typeof(x) UNIQ_T(X, xq) = (x); \
264 const typeof(low) UNIQ_T(LOW, lowq) = (low); \
265 const typeof(high) UNIQ_T(HIGH, highq) = (high); \
266 UNIQ_T(X, xq) > UNIQ_T(HIGH, highq) ? \
267 UNIQ_T(HIGH, highq) : \
268 UNIQ_T(X, xq) < UNIQ_T(LOW, lowq) ? \
269 UNIQ_T(LOW, lowq) : \
273 /* [(x + y - 1) / y] suffers from an integer overflow, even though the
274 * computation should be possible in the given type. Therefore, we use
275 * [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
276 * quotient and the remainder, so both should be equally fast. */
277 #define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, (x), UNIQ, (y))
278 #define __DIV_ROUND_UP(xq, x, yq, y) \
280 const typeof(x) UNIQ_T(X, xq) = (x); \
281 const typeof(y) UNIQ_T(Y, yq) = (y); \
282 (UNIQ_T(X, xq) / UNIQ_T(Y, yq) + !!(UNIQ_T(X, xq) % UNIQ_T(Y, yq))); \
287 /* Use special definitions of assertion macros in order to prevent
288 * false positives of ASSERT_SIDE_EFFECT on Coverity static analyzer
289 * for uses of assert_se() and assert_return().
291 * These definitions make expression go through a (trivial) function
292 * call to ensure they are not discarded. Also use ! or !! to ensure
293 * the boolean expressions are seen as such.
295 * This technique has been described and recommended in:
296 * https://community.synopsys.com/s/question/0D534000046Yuzb/suppressing-assertsideeffect-for-functions-that-allow-for-sideeffects
299 extern void __coverity_panic__(void);
301 static inline int __coverity_check__(int condition
) {
305 #define assert_message_se(expr, message) \
307 if (__coverity_check__(!(expr))) \
308 __coverity_panic__(); \
311 #define assert_log(expr, message) __coverity_check__(!!(expr))
313 #else /* ! __COVERITY__ */
315 #define assert_message_se(expr, message) \
317 if (_unlikely_(!(expr))) \
318 log_assert_failed(message, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
321 #define assert_log(expr, message) ((_likely_(expr)) \
323 : (log_assert_failed_return(message, __FILE__, __LINE__, __PRETTY_FUNCTION__), false))
325 #endif /* __COVERITY__ */
327 #define assert_se(expr) assert_message_se(expr, #expr)
329 /* We override the glibc assert() here. */
332 #define assert(expr) do {} while (false)
334 #define assert(expr) assert_message_se(expr, #expr)
337 #define assert_not_reached(t) \
339 log_assert_failed_unreachable(t, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
342 #if defined(static_assert)
343 #define assert_cc(expr) \
344 static_assert(expr, #expr);
346 #define assert_cc(expr) \
347 struct CONCATENATE(_assert_struct_, __COUNTER__) { \
348 char x[(expr) ? 0 : -1]; \
352 #define assert_return(expr, r) \
354 if (!assert_log(expr, #expr)) \
358 #define assert_return_errno(expr, r, err) \
360 if (!assert_log(expr, #expr)) { \
366 #define return_with_errno(r, err) \
372 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
373 #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
374 #define PTR_TO_UINT(p) ((unsigned) ((uintptr_t) (p)))
375 #define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
377 #define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
378 #define LONG_TO_PTR(u) ((void *) ((intptr_t) (u)))
379 #define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p)))
380 #define ULONG_TO_PTR(u) ((void *) ((uintptr_t) (u)))
382 #define PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
383 #define INT32_TO_PTR(u) ((void *) ((intptr_t) (u)))
384 #define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
385 #define UINT32_TO_PTR(u) ((void *) ((uintptr_t) (u)))
387 #define PTR_TO_INT64(p) ((int64_t) ((intptr_t) (p)))
388 #define INT64_TO_PTR(u) ((void *) ((intptr_t) (u)))
389 #define PTR_TO_UINT64(p) ((uint64_t) ((uintptr_t) (p)))
390 #define UINT64_TO_PTR(u) ((void *) ((uintptr_t) (u)))
392 #define PTR_TO_SIZE(p) ((size_t) ((uintptr_t) (p)))
393 #define SIZE_TO_PTR(u) ((void *) ((uintptr_t) (u)))
395 #define CHAR_TO_STR(x) ((char[2]) { x, 0 })
397 #define char_array_0(x) x[sizeof(x)-1] = 0;
399 /* Returns the number of chars needed to format variables of the
400 * specified type as a decimal string. Adds in extra space for a
401 * negative '-' prefix (hence works correctly on signed
402 * types). Includes space for the trailing NUL. */
403 #define DECIMAL_STR_MAX(type) \
404 (2+(sizeof(type) <= 1 ? 3 : \
405 sizeof(type) <= 2 ? 5 : \
406 sizeof(type) <= 4 ? 10 : \
407 sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
409 #define DECIMAL_STR_WIDTH(x) \
411 typeof(x) _x_ = (x); \
413 while ((_x_ /= 10) != 0) \
418 #define SET_FLAG(v, flag, b) \
419 (v) = (b) ? ((v) | (flag)) : ((v) & ~(flag))
420 #define FLAGS_SET(v, flags) \
421 ((~(v) & (flags)) == 0)
423 #define CASE_F(X) case X:
424 #define CASE_F_1(CASE, X) CASE_F(X)
425 #define CASE_F_2(CASE, X, ...) CASE(X) CASE_F_1(CASE, __VA_ARGS__)
426 #define CASE_F_3(CASE, X, ...) CASE(X) CASE_F_2(CASE, __VA_ARGS__)
427 #define CASE_F_4(CASE, X, ...) CASE(X) CASE_F_3(CASE, __VA_ARGS__)
428 #define CASE_F_5(CASE, X, ...) CASE(X) CASE_F_4(CASE, __VA_ARGS__)
429 #define CASE_F_6(CASE, X, ...) CASE(X) CASE_F_5(CASE, __VA_ARGS__)
430 #define CASE_F_7(CASE, X, ...) CASE(X) CASE_F_6(CASE, __VA_ARGS__)
431 #define CASE_F_8(CASE, X, ...) CASE(X) CASE_F_7(CASE, __VA_ARGS__)
432 #define CASE_F_9(CASE, X, ...) CASE(X) CASE_F_8(CASE, __VA_ARGS__)
433 #define CASE_F_10(CASE, X, ...) CASE(X) CASE_F_9(CASE, __VA_ARGS__)
434 #define CASE_F_11(CASE, X, ...) CASE(X) CASE_F_10(CASE, __VA_ARGS__)
435 #define CASE_F_12(CASE, X, ...) CASE(X) CASE_F_11(CASE, __VA_ARGS__)
436 #define CASE_F_13(CASE, X, ...) CASE(X) CASE_F_12(CASE, __VA_ARGS__)
437 #define CASE_F_14(CASE, X, ...) CASE(X) CASE_F_13(CASE, __VA_ARGS__)
438 #define CASE_F_15(CASE, X, ...) CASE(X) CASE_F_14(CASE, __VA_ARGS__)
439 #define CASE_F_16(CASE, X, ...) CASE(X) CASE_F_15(CASE, __VA_ARGS__)
440 #define CASE_F_17(CASE, X, ...) CASE(X) CASE_F_16(CASE, __VA_ARGS__)
441 #define CASE_F_18(CASE, X, ...) CASE(X) CASE_F_17(CASE, __VA_ARGS__)
442 #define CASE_F_19(CASE, X, ...) CASE(X) CASE_F_18(CASE, __VA_ARGS__)
443 #define CASE_F_20(CASE, X, ...) CASE(X) CASE_F_19(CASE, __VA_ARGS__)
445 #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
446 #define FOR_EACH_MAKE_CASE(...) \
447 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, \
448 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) \
451 #define IN_SET(x, ...) \
453 bool _found = false; \
454 /* If the build breaks in the line below, you need to extend the case macros. (We use "long double" as \
455 * type for the array, in the hope that checkers such as ubsan don't complain that the initializers for \
456 * the array are not representable by the base type. Ideally we'd use typeof(x) as base type, but that \
457 * doesn't work, as we want to use this on bitfields and gcc refuses typeof() on bitfields.) */ \
458 assert_cc((sizeof((long double[]){__VA_ARGS__})/sizeof(long double)) <= 20); \
460 FOR_EACH_MAKE_CASE(__VA_ARGS__) \
469 #define SWAP_TWO(x, y) do { \
470 typeof(x) _t = (x); \
475 /* Define C11 thread_local attribute even on older gcc compiler
479 * Don't break on glibc < 2.16 that doesn't define __STDC_NO_THREADS__
480 * see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53769
482 #if __STDC_VERSION__ >= 201112L && !(defined(__STDC_NO_THREADS__) || (defined(__GNU_LIBRARY__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16))
483 #define thread_local _Thread_local
485 #define thread_local __thread
489 #define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func) \
490 static inline void name(type *p) { \
494 #define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
495 static inline void func##p(type *p) { \
500 #define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \
501 scope type *name##_ref(type *p) { \
505 assert(p->n_ref > 0); \
510 #define _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, scope) \
511 scope type *name##_unref(type *p) { \
515 assert(p->n_ref > 0); \
520 return free_func(p); \
523 #define DEFINE_TRIVIAL_REF_FUNC(type, name) \
524 _DEFINE_TRIVIAL_REF_FUNC(type, name,)
525 #define DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name) \
526 _DEFINE_TRIVIAL_REF_FUNC(type, name, static)
527 #define DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name) \
528 _DEFINE_TRIVIAL_REF_FUNC(type, name, _public_)
530 #define DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
531 _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func,)
532 #define DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
533 _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, static)
534 #define DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func) \
535 _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, _public_)
537 #define DEFINE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
538 DEFINE_TRIVIAL_REF_FUNC(type, name); \
539 DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func);
541 #define DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
542 DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name); \
543 DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func);
545 #define DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
546 DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \
547 DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func);