diff options
| author | 2026-11-06 02:40:45 +0000 | |
|---|---|---|
| committer | 2026-11-06 02:40:45 +0000 | |
| commit | 029b0416cdafa1cf71016abfec6cf05b6861c871 (patch) | |
| tree | 67c386a8690924edba4739e0889d6d01482b498e /include/macros.h | |
| parent | docs(legal): add MIT license (diff) | |
| download | codebase-029b0416cdafa1cf71016abfec6cf05b6861c871.tar.gz | |
feat(macros): add macro utilities
Diffstat (limited to 'include/macros.h')
| -rw-r--r-- | include/macros.h | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/include/macros.h b/include/macros.h new file mode 100644 index 0000000..d85d017 --- /dev/null +++ b/include/macros.h | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | // macros.h --- macro utilities -*- C -*- | ||
| 2 | // | ||
| 3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | // This file is part of COSMOS Codebase. | ||
| 8 | // | ||
| 9 | // Commentary: | ||
| 10 | // | ||
| 11 | // Code: | ||
| 12 | |||
| 13 | #ifndef INCLUDED_COSMOS_MACROS_H | ||
| 14 | #define INCLUDED_COSMOS_MACROS_H | ||
| 15 | |||
| 16 | #define NOP(...) ((void) 0) | ||
| 17 | #define IDENTITY(x) x | ||
| 18 | |||
| 19 | #define NARG(...) _NARG (__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0) | ||
| 20 | #define _NARG(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N | ||
| 21 | |||
| 22 | #define _CONCAT(a, b) a ## b | ||
| 23 | #define CONCAT(a, b) _CONCAT (a, b) | ||
| 24 | |||
| 25 | #define ARRAY_LENGTH(arr) (sizeof (arr)/sizeof (*(arr))) | ||
| 26 | #define OFFSET_OF(type, field) ((usz) &(((typeof (type) *) 0)->field)) | ||
| 27 | #define CONTAINER_OF(ptr, type, field) ((typeof (type) *) ((char *) ptr - OFFSET_OF (type, field))) | ||
| 28 | #define ALIGN_OF(sym) _Alignof (typeof (sym)) | ||
| 29 | |||
| 30 | #define UNUSED(exp) ((void) exp) | ||
| 31 | |||
| 32 | #if defined(__GNUC__) || defined(__clang__) || defined(__TINYC__) | ||
| 33 | #define BEFORE_MAIN __attribute__ ((constructor)) | ||
| 34 | #else | ||
| 35 | #warning "functions marked BEFORE_MAIN won't be run automatically" | ||
| 36 | #define BEFORE_MAIN | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #define NO_RETURN __attribute__ ((noreturn)) | ||
| 40 | #define FALLTHROUGH __attribute__ ((__fallthrough__)) | ||
| 41 | |||
| 42 | #if defined(__TINYC__) | ||
| 43 | // #warning "THREAD_LOCAL won't have any effect" | ||
| 44 | #define THREAD_LOCAL | ||
| 45 | #else | ||
| 46 | #define THREAD_LOCAL _Thread_local | ||
| 47 | #endif // defined(__TINYC__) | ||
| 48 | |||
| 49 | #define DEPAREN_IMPL(...) __VA_ARGS__ | ||
| 50 | #define DEPAREN(x) DEPAREN_IMPL x | ||
| 51 | #define EXPAND_IMPL(x) x | ||
| 52 | #define EXPAND(x) EXPAND_IMPL (x) | ||
| 53 | |||
| 54 | #define STRINGIFY(arg) #arg | ||
| 55 | #define TO_STRING(arg) STRINGIFY (arg) | ||
| 56 | |||
| 57 | #define LIKELY(x) __builtin_expect (!!(x), 1) | ||
| 58 | #define UNLIKELY(x) __builtin_expect (!!(x), 0) | ||
| 59 | |||
| 60 | #define MAX(a, b) ((a) >= (b) ? (a) : (b)) | ||
| 61 | #define MIN(a, b) ((a) <= (b) ? (a) : (b)) | ||
| 62 | #define MIN3(a, b, c) MIN (MIN ((a), (b)), (c)) | ||
| 63 | |||
| 64 | #define _PIPE_1(v, f) (f (v)) | ||
| 65 | #define _PIPE_2(v, f, ...) _PIPE_1 (_PIPE_1 (v, f), __VA_ARGS__) | ||
| 66 | #define _PIPE_3(v, f, ...) _PIPE_2 (_PIPE_1 (v, f), __VA_ARGS__) | ||
| 67 | #define _PIPE_4(v, f, ...) _PIPE_3 (_PIPE_1 (v, f), __VA_ARGS__) | ||
| 68 | #define _PIPE_5(v, f, ...) _PIPE_4 (_PIPE_1 (v, f), __VA_ARGS__) | ||
| 69 | #define _PIPE_6(v, f, ...) _PIPE_5 (_PIPE_1 (v, f), __VA_ARGS__) | ||
| 70 | #define _PIPE_7(v, f, ...) _PIPE_6 (_PIPE_1 (v, f), __VA_ARGS__) | ||
| 71 | #define _PIPE_8(v, f, ...) _PIPE_7 (_PIPE_1 (v, f), __VA_ARGS__) | ||
| 72 | #define PIPE( v, ...) \ | ||
| 73 | CONCAT (_PIPE_, NARG (__VA_ARGS__)) (v, __VA_ARGS__) | ||
| 74 | |||
| 75 | #define _CHAIN_2(f, l, r) f (l, r) | ||
| 76 | #define _CHAIN_3(f, l, r, ...) _CHAIN_2 (f, _CHAIN_2 (f, r, l), __VA_ARGS__) | ||
| 77 | #define _CHAIN_4(f, l, r, ...) _CHAIN_3 (f, _CHAIN_2 (f, r, l), __VA_ARGS__) | ||
| 78 | #define _CHAIN_5(f, l, r, ...) _CHAIN_4 (f, _CHAIN_2 (f, r, l), __VA_ARGS__) | ||
| 79 | #define _CHAIN_6(f, l, r, ...) _CHAIN_5 (f, _CHAIN_2 (f, r, l), __VA_ARGS__) | ||
| 80 | #define _CHAIN_7(f, l, r, ...) _CHAIN_6 (f, _CHAIN_2 (f, r, l), __VA_ARGS__) | ||
| 81 | #define _CHAIN_8(f, l, r, ...) _CHAIN_7 (f, _CHAIN_2 (f, r, l), __VA_ARGS__) | ||
| 82 | #define CHAIN( f, ...) \ | ||
| 83 | CONCAT (_CHAIN_, NARG (__VA_ARGS__)) (f, __VA_ARGS__) | ||
| 84 | |||
| 85 | #endif // INCLUDED_COSMOS_MACROS_H | ||
| 86 | |||
| 87 | // macros.h ends here | ||
