]>
Commit | Line | Data |
---|---|---|
e5bc5f1f YW |
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | #pragma once | |
3 | ||
4 | #ifndef SD_BOOT | |
5 | #include <assert.h> | |
6 | #endif | |
7 | ||
8 | #include "type.h" | |
9 | ||
9137c03c | 10 | #define _align_(x) __attribute__((__aligned__(x))) |
e5bc5f1f YW |
11 | #define _const_ __attribute__((__const__)) |
12 | #define _pure_ __attribute__((__pure__)) | |
9137c03c DJL |
13 | #define _section_(x) __attribute__((__section__(x))) |
14 | #define _used_ __attribute__((__used__)) | |
e5bc5f1f YW |
15 | #define _unused_ __attribute__((__unused__)) |
16 | #define _cleanup_(x) __attribute__((__cleanup__(x))) | |
17 | ||
9137c03c DJL |
18 | #define XSTRINGIFY(x) #x |
19 | #define STRINGIFY(x) XSTRINGIFY(x) | |
20 | ||
e5bc5f1f YW |
21 | #ifndef __COVERITY__ |
22 | # define VOID_0 ((void)0) | |
23 | #else | |
24 | # define VOID_0 ((void*)0) | |
25 | #endif | |
26 | ||
27 | #define ELEMENTSOF(x) \ | |
28 | (__builtin_choose_expr( \ | |
29 | !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \ | |
30 | sizeof(x)/sizeof((x)[0]), \ | |
31 | VOID_0)) | |
32 | ||
33 | #define XCONCATENATE(x, y) x ## y | |
34 | #define CONCATENATE(x, y) XCONCATENATE(x, y) | |
35 | ||
36 | #ifdef SD_BOOT | |
37 | #define assert(expr) do {} while (false) | |
38 | #endif | |
39 | ||
40 | #if defined(static_assert) | |
41 | #define assert_cc(expr) \ | |
42 | static_assert(expr, #expr) | |
43 | #else | |
44 | #define assert_cc(expr) \ | |
45 | struct CONCATENATE(_assert_struct_, __COUNTER__) { \ | |
46 | char x[(expr) ? 0 : -1]; \ | |
47 | } | |
48 | #endif | |
49 | ||
50 | #define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq)) | |
51 | #define UNIQ __COUNTER__ | |
52 | ||
53 | #undef MAX | |
54 | #define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b)) | |
55 | #define __MAX(aq, a, bq, b) \ | |
56 | ({ \ | |
57 | const typeof(a) UNIQ_T(A, aq) = (a); \ | |
58 | const typeof(b) UNIQ_T(B, bq) = (b); \ | |
59 | UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \ | |
60 | }) | |
61 | ||
62 | /* evaluates to (void) if _A or _B are not constant or of different types */ | |
63 | #define CONST_MAX(_A, _B) \ | |
64 | (__builtin_choose_expr( \ | |
65 | __builtin_constant_p(_A) && \ | |
66 | __builtin_constant_p(_B) && \ | |
67 | __builtin_types_compatible_p(typeof(_A), typeof(_B)), \ | |
68 | ((_A) > (_B)) ? (_A) : (_B), \ | |
69 | VOID_0)) | |
70 | ||
71 | /* takes two types and returns the size of the larger one */ | |
72 | #define MAXSIZE(A, B) (sizeof(union _packed_ { typeof(A) a; typeof(B) b; })) | |
73 | ||
74 | #define MAX3(x, y, z) \ | |
75 | ({ \ | |
76 | const typeof(x) _c = MAX(x, y); \ | |
77 | MAX(_c, z); \ | |
78 | }) | |
79 | ||
80 | #define MAX4(x, y, z, a) \ | |
81 | ({ \ | |
82 | const typeof(x) _d = MAX3(x, y, z); \ | |
83 | MAX(_d, a); \ | |
84 | }) | |
85 | ||
86 | #undef MIN | |
87 | #define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b)) | |
88 | #define __MIN(aq, a, bq, b) \ | |
89 | ({ \ | |
90 | const typeof(a) UNIQ_T(A, aq) = (a); \ | |
91 | const typeof(b) UNIQ_T(B, bq) = (b); \ | |
92 | UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \ | |
93 | }) | |
94 | ||
95 | /* evaluates to (void) if _A or _B are not constant or of different types */ | |
96 | #define CONST_MIN(_A, _B) \ | |
97 | (__builtin_choose_expr( \ | |
98 | __builtin_constant_p(_A) && \ | |
99 | __builtin_constant_p(_B) && \ | |
100 | __builtin_types_compatible_p(typeof(_A), typeof(_B)), \ | |
101 | ((_A) < (_B)) ? (_A) : (_B), \ | |
102 | VOID_0)) | |
103 | ||
104 | #define MIN3(x, y, z) \ | |
105 | ({ \ | |
106 | const typeof(x) _c = MIN(x, y); \ | |
107 | MIN(_c, z); \ | |
108 | }) | |
109 | ||
110 | #define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b)) | |
111 | #define __LESS_BY(aq, a, bq, b) \ | |
112 | ({ \ | |
113 | const typeof(a) UNIQ_T(A, aq) = (a); \ | |
114 | const typeof(b) UNIQ_T(B, bq) = (b); \ | |
115 | UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) - UNIQ_T(B, bq) : 0; \ | |
116 | }) | |
117 | ||
118 | #define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b)) | |
119 | #define __CMP(aq, a, bq, b) \ | |
120 | ({ \ | |
121 | const typeof(a) UNIQ_T(A, aq) = (a); \ | |
122 | const typeof(b) UNIQ_T(B, bq) = (b); \ | |
123 | UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 : \ | |
124 | UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0; \ | |
125 | }) | |
126 | ||
127 | #undef CLAMP | |
128 | #define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high)) | |
129 | #define __CLAMP(xq, x, lowq, low, highq, high) \ | |
130 | ({ \ | |
131 | const typeof(x) UNIQ_T(X, xq) = (x); \ | |
132 | const typeof(low) UNIQ_T(LOW, lowq) = (low); \ | |
133 | const typeof(high) UNIQ_T(HIGH, highq) = (high); \ | |
134 | UNIQ_T(X, xq) > UNIQ_T(HIGH, highq) ? \ | |
135 | UNIQ_T(HIGH, highq) : \ | |
136 | UNIQ_T(X, xq) < UNIQ_T(LOW, lowq) ? \ | |
137 | UNIQ_T(LOW, lowq) : \ | |
138 | UNIQ_T(X, xq); \ | |
139 | }) | |
140 | ||
141 | /* [(x + y - 1) / y] suffers from an integer overflow, even though the | |
142 | * computation should be possible in the given type. Therefore, we use | |
143 | * [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the | |
144 | * quotient and the remainder, so both should be equally fast. */ | |
145 | #define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, (x), UNIQ, (y)) | |
146 | #define __DIV_ROUND_UP(xq, x, yq, y) \ | |
147 | ({ \ | |
148 | const typeof(x) UNIQ_T(X, xq) = (x); \ | |
149 | const typeof(y) UNIQ_T(Y, yq) = (y); \ | |
150 | (UNIQ_T(X, xq) / UNIQ_T(Y, yq) + !!(UNIQ_T(X, xq) % UNIQ_T(Y, yq))); \ | |
151 | }) | |
152 | ||
153 | #define CASE_F(X) case X: | |
154 | #define CASE_F_1(CASE, X) CASE_F(X) | |
155 | #define CASE_F_2(CASE, X, ...) CASE(X) CASE_F_1(CASE, __VA_ARGS__) | |
156 | #define CASE_F_3(CASE, X, ...) CASE(X) CASE_F_2(CASE, __VA_ARGS__) | |
157 | #define CASE_F_4(CASE, X, ...) CASE(X) CASE_F_3(CASE, __VA_ARGS__) | |
158 | #define CASE_F_5(CASE, X, ...) CASE(X) CASE_F_4(CASE, __VA_ARGS__) | |
159 | #define CASE_F_6(CASE, X, ...) CASE(X) CASE_F_5(CASE, __VA_ARGS__) | |
160 | #define CASE_F_7(CASE, X, ...) CASE(X) CASE_F_6(CASE, __VA_ARGS__) | |
161 | #define CASE_F_8(CASE, X, ...) CASE(X) CASE_F_7(CASE, __VA_ARGS__) | |
162 | #define CASE_F_9(CASE, X, ...) CASE(X) CASE_F_8(CASE, __VA_ARGS__) | |
163 | #define CASE_F_10(CASE, X, ...) CASE(X) CASE_F_9(CASE, __VA_ARGS__) | |
164 | #define CASE_F_11(CASE, X, ...) CASE(X) CASE_F_10(CASE, __VA_ARGS__) | |
165 | #define CASE_F_12(CASE, X, ...) CASE(X) CASE_F_11(CASE, __VA_ARGS__) | |
166 | #define CASE_F_13(CASE, X, ...) CASE(X) CASE_F_12(CASE, __VA_ARGS__) | |
167 | #define CASE_F_14(CASE, X, ...) CASE(X) CASE_F_13(CASE, __VA_ARGS__) | |
168 | #define CASE_F_15(CASE, X, ...) CASE(X) CASE_F_14(CASE, __VA_ARGS__) | |
169 | #define CASE_F_16(CASE, X, ...) CASE(X) CASE_F_15(CASE, __VA_ARGS__) | |
170 | #define CASE_F_17(CASE, X, ...) CASE(X) CASE_F_16(CASE, __VA_ARGS__) | |
171 | #define CASE_F_18(CASE, X, ...) CASE(X) CASE_F_17(CASE, __VA_ARGS__) | |
172 | #define CASE_F_19(CASE, X, ...) CASE(X) CASE_F_18(CASE, __VA_ARGS__) | |
173 | #define CASE_F_20(CASE, X, ...) CASE(X) CASE_F_19(CASE, __VA_ARGS__) | |
174 | ||
175 | #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 | |
176 | #define FOR_EACH_MAKE_CASE(...) \ | |
177 | 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, \ | |
178 | 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) \ | |
179 | (CASE_F,__VA_ARGS__) | |
180 | ||
181 | #define IN_SET(x, ...) \ | |
182 | ({ \ | |
183 | sd_bool _found = false; \ | |
184 | /* If the build breaks in the line below, you need to extend the case macros. (We use "long double" as \ | |
185 | * type for the array, in the hope that checkers such as ubsan don't complain that the initializers for \ | |
186 | * the array are not representable by the base type. Ideally we'd use typeof(x) as base type, but that \ | |
187 | * doesn't work, as we want to use this on bitfields and gcc refuses typeof() on bitfields.) */ \ | |
188 | static const long double __assert_in_set[] _unused_ = { __VA_ARGS__ }; \ | |
189 | assert_cc(ELEMENTSOF(__assert_in_set) <= 20); \ | |
190 | switch(x) { \ | |
191 | FOR_EACH_MAKE_CASE(__VA_ARGS__) \ | |
192 | _found = true; \ | |
193 | break; \ | |
194 | default: \ | |
195 | break; \ | |
196 | } \ | |
197 | _found; \ | |
198 | }) | |
199 | ||
200 | /* Takes inspiration from Rust's Option::take() method: reads and returns a pointer, but at the same time | |
201 | * resets it to NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */ | |
202 | #define TAKE_PTR(ptr) \ | |
203 | ({ \ | |
204 | typeof(ptr) _ptr_ = (ptr); \ | |
205 | (ptr) = NULL; \ | |
206 | _ptr_; \ | |
207 | }) |