| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <stdalign.h> |
| 5 |
#include <stdbool.h> |
| 6 |
#include <stddef.h> |
| 7 |
#include <stdint.h> |
| 8 |
|
| 9 |
/* Temporarily disable some warnings */ |
| 10 |
#define DISABLE_WARNING_DEPRECATED_DECLARATIONS \ |
| 11 |
_Pragma("GCC diagnostic push"); \ |
| 12 |
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") |
| 13 |
|
| 14 |
#define DISABLE_WARNING_FORMAT_NONLITERAL \ |
| 15 |
_Pragma("GCC diagnostic push"); \ |
| 16 |
_Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"") |
| 17 |
|
| 18 |
#define DISABLE_WARNING_MISSING_PROTOTYPES \ |
| 19 |
_Pragma("GCC diagnostic push"); \ |
| 20 |
_Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"") |
| 21 |
|
| 22 |
#define DISABLE_WARNING_NONNULL \ |
| 23 |
_Pragma("GCC diagnostic push"); \ |
| 24 |
_Pragma("GCC diagnostic ignored \"-Wnonnull\"") |
| 25 |
|
| 26 |
#define DISABLE_WARNING_SHADOW \ |
| 27 |
_Pragma("GCC diagnostic push"); \ |
| 28 |
_Pragma("GCC diagnostic ignored \"-Wshadow\"") |
| 29 |
|
| 30 |
#define DISABLE_WARNING_STRINGOP_OVERREAD \ |
| 31 |
_Pragma("GCC diagnostic push"); \ |
| 32 |
_Pragma("GCC diagnostic ignored \"-Wstringop-overread\"") |
| 33 |
|
| 34 |
#define DISABLE_WARNING_INCOMPATIBLE_POINTER_TYPES \ |
| 35 |
_Pragma("GCC diagnostic push"); \ |
| 36 |
_Pragma("GCC diagnostic ignored \"-Wincompatible-pointer-types\"") |
| 37 |
|
| 38 |
#define DISABLE_WARNING_TYPE_LIMITS \ |
| 39 |
_Pragma("GCC diagnostic push"); \ |
| 40 |
_Pragma("GCC diagnostic ignored \"-Wtype-limits\"") |
| 41 |
|
| 42 |
#define DISABLE_WARNING_ADDRESS \ |
| 43 |
_Pragma("GCC diagnostic push"); \ |
| 44 |
_Pragma("GCC diagnostic ignored \"-Waddress\"") |
| 45 |
|
| 46 |
#define DISABLE_WARNING_STRINGOP_TRUNCATION \ |
| 47 |
_Pragma("GCC diagnostic push"); \ |
| 48 |
_Pragma("GCC diagnostic ignored \"-Wstringop-truncation\"") |
| 49 |
|
| 50 |
#define DISABLE_WARNING_REDUNDANT_DECLS \ |
| 51 |
_Pragma("GCC diagnostic push"); \ |
| 52 |
_Pragma("GCC diagnostic ignored \"-Wredundant-decls\"") |
| 53 |
|
| 54 |
#if HAVE_WARNING_ZERO_LENGTH_BOUNDS |
| 55 |
# define DISABLE_WARNING_ZERO_LENGTH_BOUNDS \ |
| 56 |
_Pragma("GCC diagnostic push"); \ |
| 57 |
_Pragma("GCC diagnostic ignored \"-Wzero-length-bounds\"") |
| 58 |
#else |
| 59 |
# define DISABLE_WARNING_ZERO_LENGTH_BOUNDS \ |
| 60 |
_Pragma("GCC diagnostic push") |
| 61 |
#endif |
| 62 |
|
| 63 |
#if HAVE_WARNING_ZERO_AS_NULL_POINTER_CONSTANT |
| 64 |
# define DISABLE_WARNING_ZERO_AS_NULL_POINTER_CONSTANT \ |
| 65 |
_Pragma("GCC diagnostic push"); \ |
| 66 |
_Pragma("GCC diagnostic ignored \"-Wzero-as-null-pointer-constant\"") |
| 67 |
#else |
| 68 |
# define DISABLE_WARNING_ZERO_AS_NULL_POINTER_CONSTANT \ |
| 69 |
_Pragma("GCC diagnostic push") |
| 70 |
#endif |
| 71 |
|
| 72 |
#define REENABLE_WARNING \ |
| 73 |
_Pragma("GCC diagnostic pop") |
| 74 |
|
| 75 |
#define _alias_(x) typeof(x) __attribute__((alias(#x))) |
| 76 |
#define _align_(x) __attribute__((__aligned__(x))) |
| 77 |
#define _alignas_(x) __attribute__((__aligned__(alignof(x)))) |
| 78 |
#define _alignptr_ __attribute__((__aligned__(sizeof(void *)))) |
| 79 |
#define _cleanup_(x) __attribute__((__cleanup__(x))) |
| 80 |
#define _const_ __attribute__((__const__)) |
| 81 |
#define _deprecated_ __attribute__((__deprecated__)) |
| 82 |
#define _destructor_ __attribute__((__destructor__)) |
| 83 |
#define _hidden_ __attribute__((__visibility__("hidden"))) |
| 84 |
#define _likely_(x) (__builtin_expect(!!(x), 1)) |
| 85 |
#define _malloc_ __attribute__((__malloc__)) |
| 86 |
#define _noinline_ __attribute__((noinline)) |
| 87 |
#define _noreturn_ _Noreturn |
| 88 |
#define _packed_ __attribute__((__packed__)) |
| 89 |
#define _printf_(a, b) __attribute__((__format__(printf, a, b))) |
| 90 |
#define _public_ __attribute__((__visibility__("default"))) |
| 91 |
#define _pure_ __attribute__((__pure__)) |
| 92 |
#define _returns_nonnull_ __attribute__((__returns_nonnull__)) |
| 93 |
#define _section_(x) __attribute__((__section__(x))) |
| 94 |
#define _sentinel_ __attribute__((__sentinel__)) |
| 95 |
#define _unlikely_(x) (__builtin_expect(!!(x), 0)) |
| 96 |
#define _unused_ __attribute__((__unused__)) |
| 97 |
#define _used_ __attribute__((__used__)) |
| 98 |
#define _warn_unused_result_ __attribute__((__warn_unused_result__)) |
| 99 |
#define _weak_ __attribute__((__weak__)) |
| 100 |
#define _weakref_(x) __attribute__((__weakref__(#x))) |
| 101 |
|
| 102 |
#if HAVE_ATTRIBUTE_ALLOC_SIZE |
| 103 |
# define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__))) |
| 104 |
#else |
| 105 |
# define _alloc_(...) |
| 106 |
#endif |
| 107 |
|
| 108 |
#if HAVE_ATTRIBUTE_FALLTHROUGH |
| 109 |
# define _fallthrough_ __attribute__((__fallthrough__)) |
| 110 |
#else |
| 111 |
# define _fallthrough_ |
| 112 |
#endif |
| 113 |
|
| 114 |
#if HAVE_ATTRIBUTE_RETAIN |
| 115 |
# define _retain_ __attribute__((__retain__)) |
| 116 |
#else |
| 117 |
# define _retain_ |
| 118 |
#endif |
| 119 |
|
| 120 |
#if HAVE_ATTRIBUTE_NO_REORDER |
| 121 |
# define _no_reorder_ __attribute__((__no_reorder__)) |
| 122 |
#else |
| 123 |
# define _no_reorder_ |
| 124 |
#endif |
| 125 |
|
| 126 |
#if __GNUC__ >= 15 |
| 127 |
# define _nonnull_if_nonzero_(p, n) __attribute__((nonnull_if_nonzero(p, n))) |
| 128 |
#else |
| 129 |
# define _nonnull_if_nonzero_(p, n) |
| 130 |
#endif |
| 131 |
|
| 132 |
#define XSTRINGIFY(x) #x |
| 133 |
#define STRINGIFY(x) XSTRINGIFY(x) |
| 134 |
|
| 135 |
/* C23 changed char8_t from char to unsigned char, hence we cannot pass u8 literals to e.g. fputs() without |
| 136 |
* casting. Let's introduce our own way to declare UTF-8 literals, which casts u8 literals to const char*. */ |
| 137 |
#define UTF8(s) ((const char*) (u8"" s)) |
| 138 |
|
| 139 |
#ifndef __COVERITY__ |
| 140 |
# define VOID_0 ((void)0) |
| 141 |
#else |
| 142 |
# define VOID_0 ((void*)0) |
| 143 |
#endif |
| 144 |
|
| 145 |
#define ELEMENTSOF(x) \ |
| 146 |
(__builtin_choose_expr( \ |
| 147 |
!__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \ |
| 148 |
sizeof(x)/sizeof((x)[0]), \ |
| 149 |
VOID_0)) |
| 150 |
|
| 151 |
#define XCONCATENATE(x, y) x ## y |
| 152 |
#define CONCATENATE(x, y) XCONCATENATE(x, y) |
| 153 |
|
| 154 |
#define assert_cc(expr) _Static_assert(expr, #expr) |
| 155 |
|
| 156 |
#define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq)) |
| 157 |
#define UNIQ __COUNTER__ |
| 158 |
|
| 159 |
/* The macro is true when the code block is called first time, and is false for the second and later times. |
| 160 |
* Note that this works differently from pthread_once(): this macro does not synchronize code execution, i.e. |
| 161 |
* code that is run conditionalized on this macro will run concurrently to all other code conditionalized the |
| 162 |
* same way, there's no ordering or completion enforced. */ |
| 163 |
#define ONCE __ONCE(UNIQ_T(_once_, UNIQ)) |
| 164 |
#define __ONCE(o) \ |
| 165 |
({ \ |
| 166 |
static bool (o) = false; \ |
| 167 |
!__atomic_exchange_n(&(o), true, __ATOMIC_SEQ_CST); \ |
| 168 |
}) |
| 169 |
|
| 170 |
#define U64_KB UINT64_C(1024) |
| 171 |
#define U64_MB (UINT64_C(1024) * U64_KB) |
| 172 |
#define U64_GB (UINT64_C(1024) * U64_MB) |
| 173 |
|
| 174 |
#undef MAX |
| 175 |
#define MAX(a, b) __MAX(UNIQ, a, UNIQ, b) |
| 176 |
#define __MAX(aq, a, bq, b) \ |
| 177 |
({ \ |
| 178 |
const typeof(a) UNIQ_T(A, aq) = (a); \ |
| 179 |
const typeof(b) UNIQ_T(B, bq) = (b); \ |
| 180 |
UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \ |
| 181 |
}) |
| 182 |
|
| 183 |
#define ABS(a) _Generic((a), \ |
| 184 |
float: __builtin_fabsf((float) (a)), \ |
| 185 |
double: __builtin_fabs((double) (a)), \ |
| 186 |
long double: __builtin_fabsl((long double) (a)), \ |
| 187 |
unsigned long long: (a), \ |
| 188 |
unsigned long: (a), \ |
| 189 |
unsigned int: (a), \ |
| 190 |
default: __builtin_llabs((long long) (a))) |
| 191 |
|
| 192 |
#define IS_UNSIGNED_INTEGER_TYPE(type) \ |
| 193 |
(__builtin_types_compatible_p(typeof(type), unsigned char) || \ |
| 194 |
__builtin_types_compatible_p(typeof(type), unsigned short) || \ |
| 195 |
__builtin_types_compatible_p(typeof(type), unsigned) || \ |
| 196 |
__builtin_types_compatible_p(typeof(type), unsigned long) || \ |
| 197 |
__builtin_types_compatible_p(typeof(type), unsigned long long)) |
| 198 |
|
| 199 |
#define IS_SIGNED_INTEGER_TYPE(type) \ |
| 200 |
(__builtin_types_compatible_p(typeof(type), signed char) || \ |
| 201 |
__builtin_types_compatible_p(typeof(type), signed short) || \ |
| 202 |
__builtin_types_compatible_p(typeof(type), signed) || \ |
| 203 |
__builtin_types_compatible_p(typeof(type), signed long) || \ |
| 204 |
__builtin_types_compatible_p(typeof(type), signed long long)) |
| 205 |
|
| 206 |
/* Evaluates to (void) if _A or _B are not constant or of different types (being integers of different sizes |
| 207 |
* is also OK as long as the signedness matches) */ |
| 208 |
#define CONST_MAX(_A, _B) \ |
| 209 |
(__builtin_choose_expr( \ |
| 210 |
__builtin_constant_p(_A) && \ |
| 211 |
__builtin_constant_p(_B) && \ |
| 212 |
(__builtin_types_compatible_p(typeof(_A), typeof(_B)) || \ |
| 213 |
(IS_UNSIGNED_INTEGER_TYPE(_A) && IS_UNSIGNED_INTEGER_TYPE(_B)) || \ |
| 214 |
(IS_SIGNED_INTEGER_TYPE(_A) && IS_SIGNED_INTEGER_TYPE(_B))), \ |
| 215 |
((_A) > (_B)) ? (_A) : (_B), \ |
| 216 |
VOID_0)) |
| 217 |
|
| 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; })) |
| 220 |
|
| 221 |
#define MAX3(x, y, z) \ |
| 222 |
({ \ |
| 223 |
const typeof(x) _c = MAX(x, y); \ |
| 224 |
MAX(_c, z); \ |
| 225 |
}) |
| 226 |
|
| 227 |
#define MAX4(x, y, z, a) \ |
| 228 |
({ \ |
| 229 |
const typeof(x) _d = MAX3(x, y, z); \ |
| 230 |
MAX(_d, a); \ |
| 231 |
}) |
| 232 |
|
| 233 |
#define MAX5(x, y, z, a, b) \ |
| 234 |
({ \ |
| 235 |
const typeof(x) _e = MAX4(x, y, z, a); \ |
| 236 |
MAX(_e, b); \ |
| 237 |
}) |
| 238 |
|
| 239 |
#undef MIN |
| 240 |
#define MIN(a, b) __MIN(UNIQ, a, UNIQ, b) |
| 241 |
#define __MIN(aq, a, bq, b) \ |
| 242 |
({ \ |
| 243 |
const typeof(a) UNIQ_T(A, aq) = (a); \ |
| 244 |
const typeof(b) UNIQ_T(B, bq) = (b); \ |
| 245 |
UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \ |
| 246 |
}) |
| 247 |
|
| 248 |
#define ABS_DIFF(a, b) __ABS_DIFF(UNIQ, a, UNIQ, b) |
| 249 |
#define __ABS_DIFF(aq, a, bq, b) \ |
| 250 |
({ \ |
| 251 |
const typeof(a) UNIQ_T(A, aq) = (a); \ |
| 252 |
const typeof(b) UNIQ_T(B, bq) = (b); \ |
| 253 |
UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(B, bq) - UNIQ_T(A, aq) : UNIQ_T(A, aq) - UNIQ_T(B, bq); \ |
| 254 |
}) |
| 255 |
|
| 256 |
/* evaluates to (void) if _A or _B are not constant or of different types */ |
| 257 |
#define CONST_MIN(_A, _B) \ |
| 258 |
(__builtin_choose_expr( \ |
| 259 |
__builtin_constant_p(_A) && \ |
| 260 |
__builtin_constant_p(_B) && \ |
| 261 |
__builtin_types_compatible_p(typeof(_A), typeof(_B)), \ |
| 262 |
((_A) < (_B)) ? (_A) : (_B), \ |
| 263 |
VOID_0)) |
| 264 |
|
| 265 |
#define MIN3(x, y, z) \ |
| 266 |
({ \ |
| 267 |
const typeof(x) _c = MIN(x, y); \ |
| 268 |
MIN(_c, z); \ |
| 269 |
}) |
| 270 |
|
| 271 |
/* Returns true if the passed integer is a positive power of two */ |
| 272 |
#define CONST_ISPOWEROF2(x) \ |
| 273 |
((x) > 0 && ((x) & ((x) - 1)) == 0) |
| 274 |
|
| 275 |
#define ISPOWEROF2(x) \ |
| 276 |
__builtin_choose_expr( \ |
| 277 |
__builtin_constant_p(x), \ |
| 278 |
CONST_ISPOWEROF2(x), \ |
| 279 |
({ \ |
| 280 |
const typeof(x) _x = (x); \ |
| 281 |
CONST_ISPOWEROF2(_x); \ |
| 282 |
})) |
| 283 |
|
| 284 |
/* Returns the largest power of two that divides x (i.e. x's natural alignment in bytes), or 0 if x is 0. */ |
| 285 |
#define NATURAL_ALIGNMENT(x) \ |
| 286 |
({ \ |
| 287 |
const uint64_t _x = (x); \ |
| 288 |
_x == 0 ? UINT64_C(0) : UINT64_C(1) << __builtin_ctzll(_x); \ |
| 289 |
}) |
| 290 |
|
| 291 |
#define ADD_SAFE(ret, a, b) (!__builtin_add_overflow(a, b, ret)) |
| 292 |
#define INC_SAFE(a, b) __INC_SAFE(UNIQ, a, b) |
| 293 |
#define __INC_SAFE(q, a, b) \ |
| 294 |
({ \ |
| 295 |
const typeof(a) UNIQ_T(A, q) = (a); \ |
| 296 |
ADD_SAFE(UNIQ_T(A, q), *UNIQ_T(A, q), b); \ |
| 297 |
}) |
| 298 |
|
| 299 |
#define SUB_SAFE(ret, a, b) (!__builtin_sub_overflow(a, b, ret)) |
| 300 |
#define DEC_SAFE(a, b) __DEC_SAFE(UNIQ, a, b) |
| 301 |
#define __DEC_SAFE(q, a, b) \ |
| 302 |
({ \ |
| 303 |
const typeof(a) UNIQ_T(A, q) = (a); \ |
| 304 |
SUB_SAFE(UNIQ_T(A, q), *UNIQ_T(A, q), b); \ |
| 305 |
}) |
| 306 |
|
| 307 |
#define MUL_SAFE(ret, a, b) (!__builtin_mul_overflow(a, b, ret)) |
| 308 |
#define MUL_ASSIGN_SAFE(a, b) __MUL_ASSIGN_SAFE(UNIQ, a, b) |
| 309 |
#define __MUL_ASSIGN_SAFE(q, a, b) \ |
| 310 |
({ \ |
| 311 |
const typeof(a) UNIQ_T(A, q) = (a); \ |
| 312 |
MUL_SAFE(UNIQ_T(A, q), *UNIQ_T(A, q), b); \ |
| 313 |
}) |
| 314 |
|
| 315 |
#define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b)) |
| 316 |
#define __LESS_BY(aq, a, bq, b) \ |
| 317 |
({ \ |
| 318 |
const typeof(a) UNIQ_T(A, aq) = (a); \ |
| 319 |
const typeof(b) UNIQ_T(B, bq) = (b); \ |
| 320 |
UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) - UNIQ_T(B, bq) : 0; \ |
| 321 |
}) |
| 322 |
|
| 323 |
#define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b)) |
| 324 |
#define __CMP(aq, a, bq, b) \ |
| 325 |
({ \ |
| 326 |
const typeof(a) UNIQ_T(A, aq) = (a); \ |
| 327 |
const typeof(b) UNIQ_T(B, bq) = (b); \ |
| 328 |
UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 : \ |
| 329 |
UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0; \ |
| 330 |
}) |
| 331 |
|
| 332 |
#undef CLAMP |
| 333 |
#define CLAMP(x, low, high) __CLAMP(UNIQ, x, UNIQ, low, UNIQ, high) |
| 334 |
#define __CLAMP(xq, x, lowq, low, highq, high) \ |
| 335 |
({ \ |
| 336 |
const typeof(x) UNIQ_T(X, xq) = (x); \ |
| 337 |
const typeof(low) UNIQ_T(LOW, lowq) = (low); \ |
| 338 |
const typeof(high) UNIQ_T(HIGH, highq) = (high); \ |
| 339 |
UNIQ_T(X, xq) > UNIQ_T(HIGH, highq) ? \ |
| 340 |
UNIQ_T(HIGH, highq) : \ |
| 341 |
UNIQ_T(X, xq) < UNIQ_T(LOW, lowq) ? \ |
| 342 |
UNIQ_T(LOW, lowq) : \ |
| 343 |
UNIQ_T(X, xq); \ |
| 344 |
}) |
| 345 |
|
| 346 |
/* [(x + y - 1) / y] suffers from an integer overflow, even though the |
| 347 |
* computation should be possible in the given type. Therefore, we use |
| 348 |
* [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the |
| 349 |
* quotient and the remainder, so both should be equally fast. */ |
| 350 |
#define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, x, UNIQ, y) |
| 351 |
#define __DIV_ROUND_UP(xq, x, yq, y) \ |
| 352 |
({ \ |
| 353 |
const typeof(x) UNIQ_T(X, xq) = (x); \ |
| 354 |
const typeof(y) UNIQ_T(Y, yq) = (y); \ |
| 355 |
(UNIQ_T(X, xq) / UNIQ_T(Y, yq) + !!(UNIQ_T(X, xq) % UNIQ_T(Y, yq))); \ |
| 356 |
}) |
| 357 |
|
| 358 |
/* Rounds up x to the next multiple of y. Resolves to typeof(x) -1 in case of overflow */ |
| 359 |
#define __ROUND_UP(q, x, y) \ |
| 360 |
({ \ |
| 361 |
const typeof(y) UNIQ_T(A, q) = (y); \ |
| 362 |
const typeof(x) UNIQ_T(B, q) = DIV_ROUND_UP(x, UNIQ_T(A, q)); \ |
| 363 |
typeof(x) UNIQ_T(C, q); \ |
| 364 |
MUL_SAFE(&UNIQ_T(C, q), UNIQ_T(B, q), UNIQ_T(A, q)) ? UNIQ_T(C, q) : (typeof(x)) -1; \ |
| 365 |
}) |
| 366 |
#define ROUND_UP(x, y) __ROUND_UP(UNIQ, x, y) |
| 367 |
|
| 368 |
#define CASE_F_1(X) case X: |
| 369 |
#define CASE_F_2(X, ...) case X: CASE_F_1( __VA_ARGS__) |
| 370 |
#define CASE_F_3(X, ...) case X: CASE_F_2( __VA_ARGS__) |
| 371 |
#define CASE_F_4(X, ...) case X: CASE_F_3( __VA_ARGS__) |
| 372 |
#define CASE_F_5(X, ...) case X: CASE_F_4( __VA_ARGS__) |
| 373 |
#define CASE_F_6(X, ...) case X: CASE_F_5( __VA_ARGS__) |
| 374 |
#define CASE_F_7(X, ...) case X: CASE_F_6( __VA_ARGS__) |
| 375 |
#define CASE_F_8(X, ...) case X: CASE_F_7( __VA_ARGS__) |
| 376 |
#define CASE_F_9(X, ...) case X: CASE_F_8( __VA_ARGS__) |
| 377 |
#define CASE_F_10(X, ...) case X: CASE_F_9( __VA_ARGS__) |
| 378 |
#define CASE_F_11(X, ...) case X: CASE_F_10( __VA_ARGS__) |
| 379 |
#define CASE_F_12(X, ...) case X: CASE_F_11( __VA_ARGS__) |
| 380 |
#define CASE_F_13(X, ...) case X: CASE_F_12( __VA_ARGS__) |
| 381 |
#define CASE_F_14(X, ...) case X: CASE_F_13( __VA_ARGS__) |
| 382 |
#define CASE_F_15(X, ...) case X: CASE_F_14( __VA_ARGS__) |
| 383 |
#define CASE_F_16(X, ...) case X: CASE_F_15( __VA_ARGS__) |
| 384 |
#define CASE_F_17(X, ...) case X: CASE_F_16( __VA_ARGS__) |
| 385 |
#define CASE_F_18(X, ...) case X: CASE_F_17( __VA_ARGS__) |
| 386 |
#define CASE_F_19(X, ...) case X: CASE_F_18( __VA_ARGS__) |
| 387 |
#define CASE_F_20(X, ...) case X: CASE_F_19( __VA_ARGS__) |
| 388 |
#define CASE_F_21(X, ...) case X: CASE_F_20( __VA_ARGS__) |
| 389 |
#define CASE_F_22(X, ...) case X: CASE_F_21( __VA_ARGS__) |
| 390 |
|
| 391 |
#define GET_CASE_F(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,NAME,...) NAME |
| 392 |
#define FOR_EACH_MAKE_CASE(...) \ |
| 393 |
GET_CASE_F(__VA_ARGS__,CASE_F_22,CASE_F_21,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, \ |
| 394 |
CASE_F_11,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) \ |
| 395 |
(__VA_ARGS__) |
| 396 |
|
| 397 |
#define IN_SET(x, first, ...) \ |
| 398 |
({ \ |
| 399 |
bool _found = false; \ |
| 400 |
/* If the build breaks in the line below, you need to extend the case macros. We use typeof(+x) \ |
| 401 |
* here to widen the type of x if it is a bit-field as this would otherwise be illegal. */ \ |
| 402 |
static const typeof(+x) __assert_in_set[] _unused_ = { first, __VA_ARGS__ }; \ |
| 403 |
assert_cc(ELEMENTSOF(__assert_in_set) <= 22); \ |
| 404 |
switch (x) { \ |
| 405 |
FOR_EACH_MAKE_CASE(first, __VA_ARGS__) \ |
| 406 |
_found = true; \ |
| 407 |
break; \ |
| 408 |
default: \ |
| 409 |
; \ |
| 410 |
} \ |
| 411 |
_found; \ |
| 412 |
}) |
| 413 |
|
| 414 |
/* Takes inspiration from Rust's Option::take() method: reads and returns a pointer, but at the same time |
| 415 |
* resets it to NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */ |
| 416 |
#define TAKE_GENERIC(var, type, nullvalue) \ |
| 417 |
({ \ |
| 418 |
type *_pvar_ = &(var); \ |
| 419 |
type _var_ = *_pvar_; \ |
| 420 |
type _nullvalue_ = nullvalue; \ |
| 421 |
*_pvar_ = _nullvalue_; \ |
| 422 |
_var_; \ |
| 423 |
}) |
| 424 |
#define TAKE_PTR_TYPE(ptr, type) TAKE_GENERIC(ptr, type, NULL) |
| 425 |
#define TAKE_PTR(ptr) TAKE_PTR_TYPE(ptr, typeof(ptr)) |
| 426 |
#define TAKE_STRUCT_TYPE(s, type) TAKE_GENERIC(s, type, {}) |
| 427 |
#define TAKE_STRUCT(s) TAKE_STRUCT_TYPE(s, typeof(s)) |
| 428 |
|
| 429 |
/* |
| 430 |
* STRLEN - return the length of a string literal, minus the trailing NUL byte. |
| 431 |
* Contrary to strlen(), this is a constant expression. |
| 432 |
* @x: a string literal. |
| 433 |
*/ |
| 434 |
#define STRLEN(x) (sizeof(""x"") - sizeof(typeof(x[0]))) |
| 435 |
|
| 436 |
DISABLE_WARNING_REDUNDANT_DECLS; |
| 437 |
void free(void *p); /* NOLINT (readability-redundant-declaration) */ |
| 438 |
REENABLE_WARNING; |
| 439 |
|
| 440 |
#define mfree(memory) \ |
| 441 |
({ \ |
| 442 |
free(memory); \ |
| 443 |
(typeof(memory)) NULL; \ |
| 444 |
}) |
| 445 |
|
| 446 |
#define UPDATE_FLAG(orig, flag, b) \ |
| 447 |
((b) ? ((orig) | (flag)) : ((orig) & ~(flag))) |
| 448 |
#define SET_FLAG(v, flag, b) \ |
| 449 |
(v) = UPDATE_FLAG(v, flag, b) |
| 450 |
#define FLAGS_SET(v, flags) \ |
| 451 |
((~(v) & (flags)) == 0) |
| 452 |
|
| 453 |
typedef struct { |
| 454 |
int _empty[0]; |
| 455 |
} dummy_t; |
| 456 |
|
| 457 |
assert_cc(sizeof(dummy_t) == 0); |
| 458 |
|
| 459 |
/* Restriction/bug (see below) was fixed in GCC 15 and clang 19. */ |
| 460 |
#if __GNUC__ >= 15 || (defined(__clang__) && __clang_major__ >= 19) |
| 461 |
# define DECLARE_FLEX_ARRAY(type, name) type name[] |
| 462 |
#else |
| 463 |
/* Declare a flexible array usable in a union. |
| 464 |
* This is essentially a work-around for a pointless constraint in C99 |
| 465 |
* and might go away in some future version of the standard. |
| 466 |
* |
| 467 |
* See https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=3080ea5553cc909b000d1f1d964a9041962f2c5b |
| 468 |
*/ |
| 469 |
#define DECLARE_FLEX_ARRAY(type, name) \ |
| 470 |
struct { \ |
| 471 |
dummy_t __empty__ ## name; \ |
| 472 |
type name[]; \ |
| 473 |
} |
| 474 |
#endif |
| 475 |
|
| 476 |
/* Declare an ELF read-only string section that does not occupy memory at runtime. */ |
| 477 |
#define DECLARE_NOALLOC_SECTION(name, text) \ |
| 478 |
asm(".pushsection " name ",\"S\"\n\t" \ |
| 479 |
".ascii " STRINGIFY(text) "\n\t" \ |
| 480 |
".popsection\n") |
| 481 |
|
| 482 |
/* Similar to DECLARE_NOALLOC_SECTION, but pad the section with extra 512 bytes. After taking alignment into |
| 483 |
* account, the section has up to 1024 bytes minus the size of the original content of padding, and this |
| 484 |
* extra space can be used to extend the contents. This is intended for the .sbat section. */ |
| 485 |
#define DECLARE_NOALLOC_SECTION_PADDED(name, text) \ |
| 486 |
assert_cc(STRLEN(text) <= 512); \ |
| 487 |
asm(".pushsection " name ",\"S\"\n\t" \ |
| 488 |
".ascii " STRINGIFY(text) "\n\t" \ |
| 489 |
".balign 512\n\t" \ |
| 490 |
".fill 512, 1, 0\n\t" \ |
| 491 |
".popsection\n") |
| 492 |
|
| 493 |
#define typeof_field(struct_type, member) typeof(((struct_type *) 0)->member) |
| 494 |
#define sizeof_field(struct_type, member) sizeof(((struct_type *) 0)->member) |
| 495 |
#define endoffsetof_field(struct_type, member) (offsetof(struct_type, member) + sizeof_field(struct_type, member)) |
| 496 |
#define voffsetof(v, member) offsetof(typeof(v), member) |
| 497 |
|
| 498 |
#define _FOREACH_ARRAY(i, array, num, m, end) \ |
| 499 |
for (typeof(array[0]) *i = (array), *end = ({ \ |
| 500 |
typeof(num) m = (num); \ |
| 501 |
(i && m > 0) ? i + m : NULL; \ |
| 502 |
}); end && i < end; i++) |
| 503 |
|
| 504 |
#define FOREACH_ARRAY(i, array, num) \ |
| 505 |
_FOREACH_ARRAY(i, array, num, UNIQ_T(m, UNIQ), UNIQ_T(end, UNIQ)) |
| 506 |
|
| 507 |
#define FOREACH_ELEMENT(i, array) \ |
| 508 |
FOREACH_ARRAY(i, array, ELEMENTSOF(array)) |
| 509 |
|
| 510 |
#define PTR_TO_SIZE(p) ((size_t) ((uintptr_t) (p))) |
| 511 |
#define SIZE_TO_PTR(u) ((void *) ((uintptr_t) (u))) |
| 512 |
|
| 513 |
assert_cc(STRLEN(__FILE__) > STRLEN(RELATIVE_SOURCE_PATH) + 1); |
| 514 |
#define PROJECT_FILE (&__FILE__[STRLEN(RELATIVE_SOURCE_PATH) + 1]) |
| 515 |
|
| 516 |
/* In GCC 14 (C23) we can force enums to have the right types, and not solely rely on language extensions anymore */ |
| 517 |
#if (__GNUC__ >= 14 || __STDC_VERSION__ >= 202311L) && !defined(__EDG__) |
| 518 |
# define ENUM_TYPE_S64(id) id : int64_t |
| 519 |
#else |
| 520 |
# define ENUM_TYPE_S64(id) id |
| 521 |
#endif |
| 522 |
|
| 523 |
/* This macro is used to have a const-returning and non-const returning version of a function based on |
| 524 |
* whether its first argument is const or not (e.g. strstr()). */ |
| 525 |
#define const_generic(ptr, call) \ |
| 526 |
_Generic(0 ? (ptr) : (void*) 1, \ |
| 527 |
const void*: (const typeof(*call)*) (call), \ |
| 528 |
void*: (call)) |