]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/macro.h
core: reduce scope of variants
[thirdparty/systemd.git] / src / basic / macro.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <inttypes.h>
7 #include <stdbool.h>
8 #include <sys/param.h>
9 #include <sys/sysmacros.h>
10 #include <sys/types.h>
11
12 #define _printf_(a, b) __attribute__((__format__(printf, a, b)))
13 #ifdef __clang__
14 # define _alloc_(...)
15 #else
16 # define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__)))
17 #endif
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)))
38 #if __GNUC__ >= 7
39 #define _fallthrough_ __attribute__((__fallthrough__))
40 #else
41 #define _fallthrough_
42 #endif
43 /* Define C11 noreturn without <stdnoreturn.h> and even on older gcc
44 * compiler versions */
45 #ifndef _noreturn_
46 #if __STDC_VERSION__ >= 201112L
47 #define _noreturn_ _Noreturn
48 #else
49 #define _noreturn_ __attribute__((__noreturn__))
50 #endif
51 #endif
52
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
57 # endif
58 # endif
59 # if !defined(HAS_FEATURE_MEMORY_SANITIZER)
60 # define HAS_FEATURE_MEMORY_SANITIZER 0
61 # endif
62 #endif
63
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
70 # endif
71 # endif
72 # if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
73 # define HAS_FEATURE_ADDRESS_SANITIZER 0
74 # endif
75 #endif
76
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__))
83 #else
84 #define _variable_no_sanitize_address_
85 #endif
86
87 /* Apparently there's no has_feature() call defined to check for ubsan, hence let's define this
88 * unconditionally on llvm */
89 #if defined(__clang__)
90 #define _function_no_sanitize_float_cast_overflow_ __attribute__((no_sanitize("float-cast-overflow")))
91 #else
92 #define _function_no_sanitize_float_cast_overflow_
93 #endif
94
95 /* Temporarily disable some warnings */
96 #define DISABLE_WARNING_DEPRECATED_DECLARATIONS \
97 _Pragma("GCC diagnostic push"); \
98 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
99
100 #define DISABLE_WARNING_FORMAT_NONLITERAL \
101 _Pragma("GCC diagnostic push"); \
102 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
103
104 #define DISABLE_WARNING_MISSING_PROTOTYPES \
105 _Pragma("GCC diagnostic push"); \
106 _Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"")
107
108 #define DISABLE_WARNING_NONNULL \
109 _Pragma("GCC diagnostic push"); \
110 _Pragma("GCC diagnostic ignored \"-Wnonnull\"")
111
112 #define DISABLE_WARNING_SHADOW \
113 _Pragma("GCC diagnostic push"); \
114 _Pragma("GCC diagnostic ignored \"-Wshadow\"")
115
116 #define DISABLE_WARNING_INCOMPATIBLE_POINTER_TYPES \
117 _Pragma("GCC diagnostic push"); \
118 _Pragma("GCC diagnostic ignored \"-Wincompatible-pointer-types\"")
119
120 #if HAVE_WSTRINGOP_TRUNCATION
121 # define DISABLE_WARNING_STRINGOP_TRUNCATION \
122 _Pragma("GCC diagnostic push"); \
123 _Pragma("GCC diagnostic ignored \"-Wstringop-truncation\"")
124 #else
125 # define DISABLE_WARNING_STRINGOP_TRUNCATION \
126 _Pragma("GCC diagnostic push")
127 #endif
128
129 #define DISABLE_WARNING_FLOAT_EQUAL \
130 _Pragma("GCC diagnostic push"); \
131 _Pragma("GCC diagnostic ignored \"-Wfloat-equal\"")
132
133 #define DISABLE_WARNING_TYPE_LIMITS \
134 _Pragma("GCC diagnostic push"); \
135 _Pragma("GCC diagnostic ignored \"-Wtype-limits\"")
136
137 #define REENABLE_WARNING \
138 _Pragma("GCC diagnostic pop")
139
140 /* automake test harness */
141 #define EXIT_TEST_SKIP 77
142
143 #define XSTRINGIFY(x) #x
144 #define STRINGIFY(x) XSTRINGIFY(x)
145
146 #define XCONCATENATE(x, y) x ## y
147 #define CONCATENATE(x, y) XCONCATENATE(x, y)
148
149 #define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
150 #define UNIQ __COUNTER__
151
152 /* builtins */
153 #if __SIZEOF_INT__ == 4
154 #define BUILTIN_FFS_U32(x) __builtin_ffs(x);
155 #elif __SIZEOF_LONG__ == 4
156 #define BUILTIN_FFS_U32(x) __builtin_ffsl(x);
157 #else
158 #error "neither int nor long are four bytes long?!?"
159 #endif
160
161 /* Rounds up */
162
163 #define ALIGN4(l) (((l) + 3) & ~3)
164 #define ALIGN8(l) (((l) + 7) & ~7)
165
166 #if __SIZEOF_POINTER__ == 8
167 #define ALIGN(l) ALIGN8(l)
168 #elif __SIZEOF_POINTER__ == 4
169 #define ALIGN(l) ALIGN4(l)
170 #else
171 #error "Wut? Pointers are neither 4 nor 8 bytes long?"
172 #endif
173
174 #define ALIGN_PTR(p) ((void*) ALIGN((unsigned long) (p)))
175 #define ALIGN4_PTR(p) ((void*) ALIGN4((unsigned long) (p)))
176 #define ALIGN8_PTR(p) ((void*) ALIGN8((unsigned long) (p)))
177
178 static inline size_t ALIGN_TO(size_t l, size_t ali) {
179 return ((l + ali - 1) & ~(ali - 1));
180 }
181
182 #define ALIGN_TO_PTR(p, ali) ((void*) ALIGN_TO((unsigned long) (p), (ali)))
183
184 /* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */
185 static inline unsigned long ALIGN_POWER2(unsigned long u) {
186
187 /* Avoid subtraction overflow */
188 if (u == 0)
189 return 0;
190
191 /* clz(0) is undefined */
192 if (u == 1)
193 return 1;
194
195 /* left-shift overflow is undefined */
196 if (__builtin_clzl(u - 1UL) < 1)
197 return 0;
198
199 return 1UL << (sizeof(u) * 8 - __builtin_clzl(u - 1UL));
200 }
201
202 static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) {
203 size_t m;
204
205 /* Round up allocation sizes a bit to some reasonable, likely larger value. This is supposed to be
206 * used for cases which are likely called in an allocation loop of some form, i.e. that repetitively
207 * grow stuff, for example strv_extend() and suchlike.
208 *
209 * Note the difference to GREEDY_REALLOC() here, as this helper operates on a single size value only,
210 * and rounds up to next multiple of 2, needing no further counter.
211 *
212 * Note the benefits of direct ALIGN_POWER2() usage: type-safety for size_t, sane handling for very
213 * small (i.e. <= 2) and safe handling for very large (i.e. > SSIZE_MAX) values. */
214
215 if (l <= 2)
216 return 2; /* Never allocate less than 2 of something. */
217
218 m = ALIGN_POWER2(l);
219 if (m == 0) /* overflow? */
220 return l;
221
222 return m;
223 }
224
225 #ifndef __COVERITY__
226 # define VOID_0 ((void)0)
227 #else
228 # define VOID_0 ((void*)0)
229 #endif
230
231 #define ELEMENTSOF(x) \
232 (__builtin_choose_expr( \
233 !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
234 sizeof(x)/sizeof((x)[0]), \
235 VOID_0))
236
237 /*
238 * STRLEN - return the length of a string literal, minus the trailing NUL byte.
239 * Contrary to strlen(), this is a constant expression.
240 * @x: a string literal.
241 */
242 #define STRLEN(x) (sizeof(""x"") - 1)
243
244 /*
245 * container_of - cast a member of a structure out to the containing structure
246 * @ptr: the pointer to the member.
247 * @type: the type of the container struct this is embedded in.
248 * @member: the name of the member within the struct.
249 */
250 #define container_of(ptr, type, member) __container_of(UNIQ, (ptr), type, member)
251 #define __container_of(uniq, ptr, type, member) \
252 ({ \
253 const typeof( ((type*)0)->member ) *UNIQ_T(A, uniq) = (ptr); \
254 (type*)( (char *)UNIQ_T(A, uniq) - offsetof(type, member) ); \
255 })
256
257 #undef MAX
258 #define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
259 #define __MAX(aq, a, bq, b) \
260 ({ \
261 const typeof(a) UNIQ_T(A, aq) = (a); \
262 const typeof(b) UNIQ_T(B, bq) = (b); \
263 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
264 })
265
266 /* evaluates to (void) if _A or _B are not constant or of different types */
267 #define CONST_MAX(_A, _B) \
268 (__builtin_choose_expr( \
269 __builtin_constant_p(_A) && \
270 __builtin_constant_p(_B) && \
271 __builtin_types_compatible_p(typeof(_A), typeof(_B)), \
272 ((_A) > (_B)) ? (_A) : (_B), \
273 VOID_0))
274
275 /* takes two types and returns the size of the larger one */
276 #define MAXSIZE(A, B) (sizeof(union _packed_ { typeof(A) a; typeof(B) b; }))
277
278 #define MAX3(x, y, z) \
279 ({ \
280 const typeof(x) _c = MAX(x, y); \
281 MAX(_c, z); \
282 })
283
284 #define MAX4(x, y, z, a) \
285 ({ \
286 const typeof(x) _d = MAX3(x, y, z); \
287 MAX(_d, a); \
288 })
289
290 #undef MIN
291 #define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
292 #define __MIN(aq, a, bq, b) \
293 ({ \
294 const typeof(a) UNIQ_T(A, aq) = (a); \
295 const typeof(b) UNIQ_T(B, bq) = (b); \
296 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
297 })
298
299 /* evaluates to (void) if _A or _B are not constant or of different types */
300 #define CONST_MIN(_A, _B) \
301 (__builtin_choose_expr( \
302 __builtin_constant_p(_A) && \
303 __builtin_constant_p(_B) && \
304 __builtin_types_compatible_p(typeof(_A), typeof(_B)), \
305 ((_A) < (_B)) ? (_A) : (_B), \
306 VOID_0))
307
308 #define MIN3(x, y, z) \
309 ({ \
310 const typeof(x) _c = MIN(x, y); \
311 MIN(_c, z); \
312 })
313
314 #define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b))
315 #define __LESS_BY(aq, a, bq, b) \
316 ({ \
317 const typeof(a) UNIQ_T(A, aq) = (a); \
318 const typeof(b) UNIQ_T(B, bq) = (b); \
319 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) - UNIQ_T(B, bq) : 0; \
320 })
321
322 #define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
323 #define __CMP(aq, a, bq, b) \
324 ({ \
325 const typeof(a) UNIQ_T(A, aq) = (a); \
326 const typeof(b) UNIQ_T(B, bq) = (b); \
327 UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 : \
328 UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0; \
329 })
330
331 #undef CLAMP
332 #define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high))
333 #define __CLAMP(xq, x, lowq, low, highq, high) \
334 ({ \
335 const typeof(x) UNIQ_T(X, xq) = (x); \
336 const typeof(low) UNIQ_T(LOW, lowq) = (low); \
337 const typeof(high) UNIQ_T(HIGH, highq) = (high); \
338 UNIQ_T(X, xq) > UNIQ_T(HIGH, highq) ? \
339 UNIQ_T(HIGH, highq) : \
340 UNIQ_T(X, xq) < UNIQ_T(LOW, lowq) ? \
341 UNIQ_T(LOW, lowq) : \
342 UNIQ_T(X, xq); \
343 })
344
345 /* [(x + y - 1) / y] suffers from an integer overflow, even though the
346 * computation should be possible in the given type. Therefore, we use
347 * [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
348 * quotient and the remainder, so both should be equally fast. */
349 #define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, (x), UNIQ, (y))
350 #define __DIV_ROUND_UP(xq, x, yq, y) \
351 ({ \
352 const typeof(x) UNIQ_T(X, xq) = (x); \
353 const typeof(y) UNIQ_T(Y, yq) = (y); \
354 (UNIQ_T(X, xq) / UNIQ_T(Y, yq) + !!(UNIQ_T(X, xq) % UNIQ_T(Y, yq))); \
355 })
356
357 #ifdef __COVERITY__
358
359 /* Use special definitions of assertion macros in order to prevent
360 * false positives of ASSERT_SIDE_EFFECT on Coverity static analyzer
361 * for uses of assert_se() and assert_return().
362 *
363 * These definitions make expression go through a (trivial) function
364 * call to ensure they are not discarded. Also use ! or !! to ensure
365 * the boolean expressions are seen as such.
366 *
367 * This technique has been described and recommended in:
368 * https://community.synopsys.com/s/question/0D534000046Yuzb/suppressing-assertsideeffect-for-functions-that-allow-for-sideeffects
369 */
370
371 extern void __coverity_panic__(void);
372
373 static inline void __coverity_check__(int condition) {
374 if (!condition)
375 __coverity_panic__();
376 }
377
378 static inline int __coverity_check_and_return__(int condition) {
379 return condition;
380 }
381
382 #define assert_message_se(expr, message) __coverity_check__(!!(expr))
383
384 #define assert_log(expr, message) __coverity_check_and_return__(!!(expr))
385
386 #else /* ! __COVERITY__ */
387
388 #define assert_message_se(expr, message) \
389 do { \
390 if (_unlikely_(!(expr))) \
391 log_assert_failed(message, PROJECT_FILE, __LINE__, __PRETTY_FUNCTION__); \
392 } while (false)
393
394 #define assert_log(expr, message) ((_likely_(expr)) \
395 ? (true) \
396 : (log_assert_failed_return(message, PROJECT_FILE, __LINE__, __PRETTY_FUNCTION__), false))
397
398 #endif /* __COVERITY__ */
399
400 #define assert_se(expr) assert_message_se(expr, #expr)
401
402 /* We override the glibc assert() here. */
403 #undef assert
404 #ifdef NDEBUG
405 #define assert(expr) do {} while (false)
406 #else
407 #define assert(expr) assert_message_se(expr, #expr)
408 #endif
409
410 #define assert_not_reached(t) \
411 log_assert_failed_unreachable(t, PROJECT_FILE, __LINE__, __PRETTY_FUNCTION__)
412
413 #if defined(static_assert)
414 #define assert_cc(expr) \
415 static_assert(expr, #expr)
416 #else
417 #define assert_cc(expr) \
418 struct CONCATENATE(_assert_struct_, __COUNTER__) { \
419 char x[(expr) ? 0 : -1]; \
420 }
421 #endif
422
423 #define assert_return(expr, r) \
424 do { \
425 if (!assert_log(expr, #expr)) \
426 return (r); \
427 } while (false)
428
429 #define assert_return_errno(expr, r, err) \
430 do { \
431 if (!assert_log(expr, #expr)) { \
432 errno = err; \
433 return (r); \
434 } \
435 } while (false)
436
437 #define return_with_errno(r, err) \
438 do { \
439 errno = abs(err); \
440 return r; \
441 } while (false)
442
443 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
444 #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
445 #define PTR_TO_UINT(p) ((unsigned) ((uintptr_t) (p)))
446 #define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
447
448 #define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
449 #define LONG_TO_PTR(u) ((void *) ((intptr_t) (u)))
450 #define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p)))
451 #define ULONG_TO_PTR(u) ((void *) ((uintptr_t) (u)))
452
453 #define PTR_TO_UINT8(p) ((uint8_t) ((uintptr_t) (p)))
454 #define UINT8_TO_PTR(u) ((void *) ((uintptr_t) (u)))
455
456 #define PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
457 #define INT32_TO_PTR(u) ((void *) ((intptr_t) (u)))
458 #define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
459 #define UINT32_TO_PTR(u) ((void *) ((uintptr_t) (u)))
460
461 #define PTR_TO_INT64(p) ((int64_t) ((intptr_t) (p)))
462 #define INT64_TO_PTR(u) ((void *) ((intptr_t) (u)))
463 #define PTR_TO_UINT64(p) ((uint64_t) ((uintptr_t) (p)))
464 #define UINT64_TO_PTR(u) ((void *) ((uintptr_t) (u)))
465
466 #define PTR_TO_SIZE(p) ((size_t) ((uintptr_t) (p)))
467 #define SIZE_TO_PTR(u) ((void *) ((uintptr_t) (u)))
468
469 #define CHAR_TO_STR(x) ((char[2]) { x, 0 })
470
471 #define char_array_0(x) x[sizeof(x)-1] = 0;
472
473 #define sizeof_field(struct_type, member) sizeof(((struct_type *) 0)->member)
474
475 /* Returns the number of chars needed to format variables of the
476 * specified type as a decimal string. Adds in extra space for a
477 * negative '-' prefix (hence works correctly on signed
478 * types). Includes space for the trailing NUL. */
479 #define DECIMAL_STR_MAX(type) \
480 (2+(sizeof(type) <= 1 ? 3 : \
481 sizeof(type) <= 2 ? 5 : \
482 sizeof(type) <= 4 ? 10 : \
483 sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
484
485 #define DECIMAL_STR_WIDTH(x) \
486 ({ \
487 typeof(x) _x_ = (x); \
488 unsigned ans = 1; \
489 while ((_x_ /= 10) != 0) \
490 ans++; \
491 ans; \
492 })
493
494 #define UPDATE_FLAG(orig, flag, b) \
495 ((b) ? ((orig) | (flag)) : ((orig) & ~(flag)))
496 #define SET_FLAG(v, flag, b) \
497 (v) = UPDATE_FLAG(v, flag, b)
498 #define FLAGS_SET(v, flags) \
499 ((~(v) & (flags)) == 0)
500
501 #define CASE_F(X) case X:
502 #define CASE_F_1(CASE, X) CASE_F(X)
503 #define CASE_F_2(CASE, X, ...) CASE(X) CASE_F_1(CASE, __VA_ARGS__)
504 #define CASE_F_3(CASE, X, ...) CASE(X) CASE_F_2(CASE, __VA_ARGS__)
505 #define CASE_F_4(CASE, X, ...) CASE(X) CASE_F_3(CASE, __VA_ARGS__)
506 #define CASE_F_5(CASE, X, ...) CASE(X) CASE_F_4(CASE, __VA_ARGS__)
507 #define CASE_F_6(CASE, X, ...) CASE(X) CASE_F_5(CASE, __VA_ARGS__)
508 #define CASE_F_7(CASE, X, ...) CASE(X) CASE_F_6(CASE, __VA_ARGS__)
509 #define CASE_F_8(CASE, X, ...) CASE(X) CASE_F_7(CASE, __VA_ARGS__)
510 #define CASE_F_9(CASE, X, ...) CASE(X) CASE_F_8(CASE, __VA_ARGS__)
511 #define CASE_F_10(CASE, X, ...) CASE(X) CASE_F_9(CASE, __VA_ARGS__)
512 #define CASE_F_11(CASE, X, ...) CASE(X) CASE_F_10(CASE, __VA_ARGS__)
513 #define CASE_F_12(CASE, X, ...) CASE(X) CASE_F_11(CASE, __VA_ARGS__)
514 #define CASE_F_13(CASE, X, ...) CASE(X) CASE_F_12(CASE, __VA_ARGS__)
515 #define CASE_F_14(CASE, X, ...) CASE(X) CASE_F_13(CASE, __VA_ARGS__)
516 #define CASE_F_15(CASE, X, ...) CASE(X) CASE_F_14(CASE, __VA_ARGS__)
517 #define CASE_F_16(CASE, X, ...) CASE(X) CASE_F_15(CASE, __VA_ARGS__)
518 #define CASE_F_17(CASE, X, ...) CASE(X) CASE_F_16(CASE, __VA_ARGS__)
519 #define CASE_F_18(CASE, X, ...) CASE(X) CASE_F_17(CASE, __VA_ARGS__)
520 #define CASE_F_19(CASE, X, ...) CASE(X) CASE_F_18(CASE, __VA_ARGS__)
521 #define CASE_F_20(CASE, X, ...) CASE(X) CASE_F_19(CASE, __VA_ARGS__)
522
523 #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
524 #define FOR_EACH_MAKE_CASE(...) \
525 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, \
526 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) \
527 (CASE_F,__VA_ARGS__)
528
529 #define IN_SET(x, ...) \
530 ({ \
531 bool _found = false; \
532 /* If the build breaks in the line below, you need to extend the case macros. (We use "long double" as \
533 * type for the array, in the hope that checkers such as ubsan don't complain that the initializers for \
534 * the array are not representable by the base type. Ideally we'd use typeof(x) as base type, but that \
535 * doesn't work, as we want to use this on bitfields and gcc refuses typeof() on bitfields.) */ \
536 static const long double __assert_in_set[] _unused_ = { __VA_ARGS__ }; \
537 assert_cc(ELEMENTSOF(__assert_in_set) <= 20); \
538 switch(x) { \
539 FOR_EACH_MAKE_CASE(__VA_ARGS__) \
540 _found = true; \
541 break; \
542 default: \
543 break; \
544 } \
545 _found; \
546 })
547
548 #define SWAP_TWO(x, y) do { \
549 typeof(x) _t = (x); \
550 (x) = (y); \
551 (y) = (_t); \
552 } while (false)
553
554 #define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL }))
555 #define STRV_MAKE_EMPTY ((char*[1]) { NULL })
556
557 /* Pointers range from NULL to POINTER_MAX */
558 #define POINTER_MAX ((void*) UINTPTR_MAX)
559
560 /* Iterates through a specified list of pointers. Accepts NULL pointers, but uses POINTER_MAX as internal marker for EOL. */
561 #define FOREACH_POINTER(p, x, ...) \
562 for (typeof(p) *_l = (typeof(p)[]) { ({ p = x; }), ##__VA_ARGS__, POINTER_MAX }; \
563 p != (typeof(p)) POINTER_MAX; \
564 p = *(++_l))
565
566 /* Define C11 thread_local attribute even on older gcc compiler
567 * version */
568 #ifndef thread_local
569 /*
570 * Don't break on glibc < 2.16 that doesn't define __STDC_NO_THREADS__
571 * see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53769
572 */
573 #if __STDC_VERSION__ >= 201112L && !(defined(__STDC_NO_THREADS__) || (defined(__GNU_LIBRARY__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16))
574 #define thread_local _Thread_local
575 #else
576 #define thread_local __thread
577 #endif
578 #endif
579
580 #define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func) \
581 static inline void name(type *p) { \
582 func(p); \
583 }
584
585 #define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
586 static inline void func##p(type *p) { \
587 if (*p) \
588 func(*p); \
589 }
590
591 #define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \
592 scope type *name##_ref(type *p) { \
593 if (!p) \
594 return NULL; \
595 \
596 assert(p->n_ref > 0); \
597 p->n_ref++; \
598 return p; \
599 }
600
601 #define _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, scope) \
602 scope type *name##_unref(type *p) { \
603 if (!p) \
604 return NULL; \
605 \
606 assert(p->n_ref > 0); \
607 p->n_ref--; \
608 if (p->n_ref > 0) \
609 return NULL; \
610 \
611 return free_func(p); \
612 }
613
614 #define DEFINE_TRIVIAL_REF_FUNC(type, name) \
615 _DEFINE_TRIVIAL_REF_FUNC(type, name,)
616 #define DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name) \
617 _DEFINE_TRIVIAL_REF_FUNC(type, name, static)
618 #define DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name) \
619 _DEFINE_TRIVIAL_REF_FUNC(type, name, _public_)
620
621 #define DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
622 _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func,)
623 #define DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
624 _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, static)
625 #define DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func) \
626 _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, _public_)
627
628 #define DEFINE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
629 DEFINE_TRIVIAL_REF_FUNC(type, name); \
630 DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func);
631
632 #define DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
633 DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name); \
634 DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func);
635
636 #define DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
637 DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \
638 DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func);
639
640 /* A macro to force copying of a variable from memory. This is useful whenever we want to read something from
641 * memory and want to make sure the compiler won't optimize away the destination variable for us. It's not
642 * supposed to be a full CPU memory barrier, i.e. CPU is still allowed to reorder the reads, but it is not
643 * allowed to remove our local copies of the variables. We want this to work for unaligned memory, hence
644 * memcpy() is great for our purposes. */
645 #define READ_NOW(x) \
646 ({ \
647 typeof(x) _copy; \
648 memcpy(&_copy, &(x), sizeof(_copy)); \
649 asm volatile ("" : : : "memory"); \
650 _copy; \
651 })
652
653 static inline size_t size_add(size_t x, size_t y) {
654 return y >= SIZE_MAX - x ? SIZE_MAX : x + y;
655 }
656
657 #include "log.h"