Line data Source code
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 : /* Temporarily disable some warnings */
88 : #define DISABLE_WARNING_FORMAT_NONLITERAL \
89 : _Pragma("GCC diagnostic push"); \
90 : _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
91 :
92 : #define DISABLE_WARNING_MISSING_PROTOTYPES \
93 : _Pragma("GCC diagnostic push"); \
94 : _Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"")
95 :
96 : #define DISABLE_WARNING_NONNULL \
97 : _Pragma("GCC diagnostic push"); \
98 : _Pragma("GCC diagnostic ignored \"-Wnonnull\"")
99 :
100 : #define DISABLE_WARNING_SHADOW \
101 : _Pragma("GCC diagnostic push"); \
102 : _Pragma("GCC diagnostic ignored \"-Wshadow\"")
103 :
104 : #define DISABLE_WARNING_INCOMPATIBLE_POINTER_TYPES \
105 : _Pragma("GCC diagnostic push"); \
106 : _Pragma("GCC diagnostic ignored \"-Wincompatible-pointer-types\"")
107 :
108 : #if HAVE_WSTRINGOP_TRUNCATION
109 : # define DISABLE_WARNING_STRINGOP_TRUNCATION \
110 : _Pragma("GCC diagnostic push"); \
111 : _Pragma("GCC diagnostic ignored \"-Wstringop-truncation\"")
112 : #else
113 : # define DISABLE_WARNING_STRINGOP_TRUNCATION \
114 : _Pragma("GCC diagnostic push")
115 : #endif
116 :
117 : #define REENABLE_WARNING \
118 : _Pragma("GCC diagnostic pop")
119 :
120 : /* automake test harness */
121 : #define EXIT_TEST_SKIP 77
122 :
123 : #define XSTRINGIFY(x) #x
124 : #define STRINGIFY(x) XSTRINGIFY(x)
125 :
126 : #define XCONCATENATE(x, y) x ## y
127 : #define CONCATENATE(x, y) XCONCATENATE(x, y)
128 :
129 : #define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
130 : #define UNIQ __COUNTER__
131 :
132 : /* builtins */
133 : #if __SIZEOF_INT__ == 4
134 : #define BUILTIN_FFS_U32(x) __builtin_ffs(x);
135 : #elif __SIZEOF_LONG__ == 4
136 : #define BUILTIN_FFS_U32(x) __builtin_ffsl(x);
137 : #else
138 : #error "neither int nor long are four bytes long?!?"
139 : #endif
140 :
141 : /* Rounds up */
142 :
143 : #define ALIGN4(l) (((l) + 3) & ~3)
144 : #define ALIGN8(l) (((l) + 7) & ~7)
145 :
146 : #if __SIZEOF_POINTER__ == 8
147 : #define ALIGN(l) ALIGN8(l)
148 : #elif __SIZEOF_POINTER__ == 4
149 : #define ALIGN(l) ALIGN4(l)
150 : #else
151 : #error "Wut? Pointers are neither 4 nor 8 bytes long?"
152 : #endif
153 :
154 : #define ALIGN_PTR(p) ((void*) ALIGN((unsigned long) (p)))
155 : #define ALIGN4_PTR(p) ((void*) ALIGN4((unsigned long) (p)))
156 : #define ALIGN8_PTR(p) ((void*) ALIGN8((unsigned long) (p)))
157 :
158 91576 : static inline size_t ALIGN_TO(size_t l, size_t ali) {
159 91576 : return ((l + ali - 1) & ~(ali - 1));
160 : }
161 :
162 : #define ALIGN_TO_PTR(p, ali) ((void*) ALIGN_TO((unsigned long) (p), (ali)))
163 :
164 : /* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */
165 132104 : static inline unsigned long ALIGN_POWER2(unsigned long u) {
166 : /* clz(0) is undefined */
167 132104 : if (u == 1)
168 2 : return 1;
169 :
170 : /* left-shift overflow is undefined */
171 132102 : if (__builtin_clzl(u - 1UL) < 1)
172 1029 : return 0;
173 :
174 131073 : return 1UL << (sizeof(u) * 8 - __builtin_clzl(u - 1UL));
175 : }
176 :
177 : #ifndef __COVERITY__
178 : # define VOID_0 ((void)0)
179 : #else
180 : # define VOID_0 ((void*)0)
181 : #endif
182 :
183 : #define ELEMENTSOF(x) \
184 : (__builtin_choose_expr( \
185 : !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
186 : sizeof(x)/sizeof((x)[0]), \
187 : VOID_0))
188 :
189 : /*
190 : * STRLEN - return the length of a string literal, minus the trailing NUL byte.
191 : * Contrary to strlen(), this is a constant expression.
192 : * @x: a string literal.
193 : */
194 : #define STRLEN(x) (sizeof(""x"") - 1)
195 :
196 : /*
197 : * container_of - cast a member of a structure out to the containing structure
198 : * @ptr: the pointer to the member.
199 : * @type: the type of the container struct this is embedded in.
200 : * @member: the name of the member within the struct.
201 : */
202 : #define container_of(ptr, type, member) __container_of(UNIQ, (ptr), type, member)
203 : #define __container_of(uniq, ptr, type, member) \
204 : ({ \
205 : const typeof( ((type*)0)->member ) *UNIQ_T(A, uniq) = (ptr); \
206 : (type*)( (char *)UNIQ_T(A, uniq) - offsetof(type, member) ); \
207 : })
208 :
209 : #undef MAX
210 : #define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
211 : #define __MAX(aq, a, bq, b) \
212 : ({ \
213 : const typeof(a) UNIQ_T(A, aq) = (a); \
214 : const typeof(b) UNIQ_T(B, bq) = (b); \
215 : UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
216 : })
217 :
218 : /* evaluates to (void) if _A or _B are not constant or of different types */
219 : #define CONST_MAX(_A, _B) \
220 : (__builtin_choose_expr( \
221 : __builtin_constant_p(_A) && \
222 : __builtin_constant_p(_B) && \
223 : __builtin_types_compatible_p(typeof(_A), typeof(_B)), \
224 : ((_A) > (_B)) ? (_A) : (_B), \
225 : VOID_0))
226 :
227 : /* takes two types and returns the size of the larger one */
228 : #define MAXSIZE(A, B) (sizeof(union _packed_ { typeof(A) a; typeof(B) b; }))
229 :
230 : #define MAX3(x, y, z) \
231 : ({ \
232 : const typeof(x) _c = MAX(x, y); \
233 : MAX(_c, z); \
234 : })
235 :
236 : #undef MIN
237 : #define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
238 : #define __MIN(aq, a, bq, b) \
239 : ({ \
240 : const typeof(a) UNIQ_T(A, aq) = (a); \
241 : const typeof(b) UNIQ_T(B, bq) = (b); \
242 : UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
243 : })
244 :
245 : #define MIN3(x, y, z) \
246 : ({ \
247 : const typeof(x) _c = MIN(x, y); \
248 : MIN(_c, z); \
249 : })
250 :
251 : #define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b))
252 : #define __LESS_BY(aq, a, bq, b) \
253 : ({ \
254 : const typeof(a) UNIQ_T(A, aq) = (a); \
255 : const typeof(b) UNIQ_T(B, bq) = (b); \
256 : UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) - UNIQ_T(B, bq) : 0; \
257 : })
258 :
259 : #define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
260 : #define __CMP(aq, a, bq, b) \
261 : ({ \
262 : const typeof(a) UNIQ_T(A, aq) = (a); \
263 : const typeof(b) UNIQ_T(B, bq) = (b); \
264 : UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 : \
265 : UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0; \
266 : })
267 :
268 : #undef CLAMP
269 : #define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high))
270 : #define __CLAMP(xq, x, lowq, low, highq, high) \
271 : ({ \
272 : const typeof(x) UNIQ_T(X, xq) = (x); \
273 : const typeof(low) UNIQ_T(LOW, lowq) = (low); \
274 : const typeof(high) UNIQ_T(HIGH, highq) = (high); \
275 : UNIQ_T(X, xq) > UNIQ_T(HIGH, highq) ? \
276 : UNIQ_T(HIGH, highq) : \
277 : UNIQ_T(X, xq) < UNIQ_T(LOW, lowq) ? \
278 : UNIQ_T(LOW, lowq) : \
279 : UNIQ_T(X, xq); \
280 : })
281 :
282 : /* [(x + y - 1) / y] suffers from an integer overflow, even though the
283 : * computation should be possible in the given type. Therefore, we use
284 : * [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
285 : * quotient and the remainder, so both should be equally fast. */
286 : #define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, (x), UNIQ, (y))
287 : #define __DIV_ROUND_UP(xq, x, yq, y) \
288 : ({ \
289 : const typeof(x) UNIQ_T(X, xq) = (x); \
290 : const typeof(y) UNIQ_T(Y, yq) = (y); \
291 : (UNIQ_T(X, xq) / UNIQ_T(Y, yq) + !!(UNIQ_T(X, xq) % UNIQ_T(Y, yq))); \
292 : })
293 :
294 : #ifdef __COVERITY__
295 :
296 : /* Use special definitions of assertion macros in order to prevent
297 : * false positives of ASSERT_SIDE_EFFECT on Coverity static analyzer
298 : * for uses of assert_se() and assert_return().
299 : *
300 : * These definitions make expression go through a (trivial) function
301 : * call to ensure they are not discarded. Also use ! or !! to ensure
302 : * the boolean expressions are seen as such.
303 : *
304 : * This technique has been described and recommended in:
305 : * https://community.synopsys.com/s/question/0D534000046Yuzb/suppressing-assertsideeffect-for-functions-that-allow-for-sideeffects
306 : */
307 :
308 : extern void __coverity_panic__(void);
309 :
310 : static inline int __coverity_check__(int condition) {
311 : return condition;
312 : }
313 :
314 : #define assert_message_se(expr, message) \
315 : do { \
316 : if (__coverity_check__(!(expr))) \
317 : __coverity_panic__(); \
318 : } while (false)
319 :
320 : #define assert_log(expr, message) __coverity_check__(!!(expr))
321 :
322 : #else /* ! __COVERITY__ */
323 :
324 : #define assert_message_se(expr, message) \
325 : do { \
326 : if (_unlikely_(!(expr))) \
327 : log_assert_failed(message, PROJECT_FILE, __LINE__, __PRETTY_FUNCTION__); \
328 : } while (false)
329 :
330 : #define assert_log(expr, message) ((_likely_(expr)) \
331 : ? (true) \
332 : : (log_assert_failed_return(message, PROJECT_FILE, __LINE__, __PRETTY_FUNCTION__), false))
333 :
334 : #endif /* __COVERITY__ */
335 :
336 : #define assert_se(expr) assert_message_se(expr, #expr)
337 :
338 : /* We override the glibc assert() here. */
339 : #undef assert
340 : #ifdef NDEBUG
341 : #define assert(expr) do {} while (false)
342 : #else
343 : #define assert(expr) assert_message_se(expr, #expr)
344 : #endif
345 :
346 : #define assert_not_reached(t) \
347 : log_assert_failed_unreachable(t, PROJECT_FILE, __LINE__, __PRETTY_FUNCTION__)
348 :
349 : #if defined(static_assert)
350 : #define assert_cc(expr) \
351 : static_assert(expr, #expr)
352 : #else
353 : #define assert_cc(expr) \
354 : struct CONCATENATE(_assert_struct_, __COUNTER__) { \
355 : char x[(expr) ? 0 : -1]; \
356 : }
357 : #endif
358 :
359 : #define assert_return(expr, r) \
360 : do { \
361 : if (!assert_log(expr, #expr)) \
362 : return (r); \
363 : } while (false)
364 :
365 : #define assert_return_errno(expr, r, err) \
366 : do { \
367 : if (!assert_log(expr, #expr)) { \
368 : errno = err; \
369 : return (r); \
370 : } \
371 : } while (false)
372 :
373 : #define return_with_errno(r, err) \
374 : do { \
375 : errno = abs(err); \
376 : return r; \
377 : } while (false)
378 :
379 : #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
380 : #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
381 : #define PTR_TO_UINT(p) ((unsigned) ((uintptr_t) (p)))
382 : #define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
383 :
384 : #define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
385 : #define LONG_TO_PTR(u) ((void *) ((intptr_t) (u)))
386 : #define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p)))
387 : #define ULONG_TO_PTR(u) ((void *) ((uintptr_t) (u)))
388 :
389 : #define PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
390 : #define INT32_TO_PTR(u) ((void *) ((intptr_t) (u)))
391 : #define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
392 : #define UINT32_TO_PTR(u) ((void *) ((uintptr_t) (u)))
393 :
394 : #define PTR_TO_INT64(p) ((int64_t) ((intptr_t) (p)))
395 : #define INT64_TO_PTR(u) ((void *) ((intptr_t) (u)))
396 : #define PTR_TO_UINT64(p) ((uint64_t) ((uintptr_t) (p)))
397 : #define UINT64_TO_PTR(u) ((void *) ((uintptr_t) (u)))
398 :
399 : #define PTR_TO_SIZE(p) ((size_t) ((uintptr_t) (p)))
400 : #define SIZE_TO_PTR(u) ((void *) ((uintptr_t) (u)))
401 :
402 : #define CHAR_TO_STR(x) ((char[2]) { x, 0 })
403 :
404 : #define char_array_0(x) x[sizeof(x)-1] = 0;
405 :
406 : /* Returns the number of chars needed to format variables of the
407 : * specified type as a decimal string. Adds in extra space for a
408 : * negative '-' prefix (hence works correctly on signed
409 : * types). Includes space for the trailing NUL. */
410 : #define DECIMAL_STR_MAX(type) \
411 : (2+(sizeof(type) <= 1 ? 3 : \
412 : sizeof(type) <= 2 ? 5 : \
413 : sizeof(type) <= 4 ? 10 : \
414 : sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
415 :
416 : #define DECIMAL_STR_WIDTH(x) \
417 : ({ \
418 : typeof(x) _x_ = (x); \
419 : unsigned ans = 1; \
420 : while ((_x_ /= 10) != 0) \
421 : ans++; \
422 : ans; \
423 : })
424 :
425 : #define SET_FLAG(v, flag, b) \
426 : (v) = (b) ? ((v) | (flag)) : ((v) & ~(flag))
427 : #define FLAGS_SET(v, flags) \
428 : ((~(v) & (flags)) == 0)
429 :
430 : #define CASE_F(X) case X:
431 : #define CASE_F_1(CASE, X) CASE_F(X)
432 : #define CASE_F_2(CASE, X, ...) CASE(X) CASE_F_1(CASE, __VA_ARGS__)
433 : #define CASE_F_3(CASE, X, ...) CASE(X) CASE_F_2(CASE, __VA_ARGS__)
434 : #define CASE_F_4(CASE, X, ...) CASE(X) CASE_F_3(CASE, __VA_ARGS__)
435 : #define CASE_F_5(CASE, X, ...) CASE(X) CASE_F_4(CASE, __VA_ARGS__)
436 : #define CASE_F_6(CASE, X, ...) CASE(X) CASE_F_5(CASE, __VA_ARGS__)
437 : #define CASE_F_7(CASE, X, ...) CASE(X) CASE_F_6(CASE, __VA_ARGS__)
438 : #define CASE_F_8(CASE, X, ...) CASE(X) CASE_F_7(CASE, __VA_ARGS__)
439 : #define CASE_F_9(CASE, X, ...) CASE(X) CASE_F_8(CASE, __VA_ARGS__)
440 : #define CASE_F_10(CASE, X, ...) CASE(X) CASE_F_9(CASE, __VA_ARGS__)
441 : #define CASE_F_11(CASE, X, ...) CASE(X) CASE_F_10(CASE, __VA_ARGS__)
442 : #define CASE_F_12(CASE, X, ...) CASE(X) CASE_F_11(CASE, __VA_ARGS__)
443 : #define CASE_F_13(CASE, X, ...) CASE(X) CASE_F_12(CASE, __VA_ARGS__)
444 : #define CASE_F_14(CASE, X, ...) CASE(X) CASE_F_13(CASE, __VA_ARGS__)
445 : #define CASE_F_15(CASE, X, ...) CASE(X) CASE_F_14(CASE, __VA_ARGS__)
446 : #define CASE_F_16(CASE, X, ...) CASE(X) CASE_F_15(CASE, __VA_ARGS__)
447 : #define CASE_F_17(CASE, X, ...) CASE(X) CASE_F_16(CASE, __VA_ARGS__)
448 : #define CASE_F_18(CASE, X, ...) CASE(X) CASE_F_17(CASE, __VA_ARGS__)
449 : #define CASE_F_19(CASE, X, ...) CASE(X) CASE_F_18(CASE, __VA_ARGS__)
450 : #define CASE_F_20(CASE, X, ...) CASE(X) CASE_F_19(CASE, __VA_ARGS__)
451 :
452 : #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
453 : #define FOR_EACH_MAKE_CASE(...) \
454 : 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, \
455 : 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) \
456 : (CASE_F,__VA_ARGS__)
457 :
458 : #define IN_SET(x, ...) \
459 : ({ \
460 : bool _found = false; \
461 : /* If the build breaks in the line below, you need to extend the case macros. (We use "long double" as \
462 : * type for the array, in the hope that checkers such as ubsan don't complain that the initializers for \
463 : * the array are not representable by the base type. Ideally we'd use typeof(x) as base type, but that \
464 : * doesn't work, as we want to use this on bitfields and gcc refuses typeof() on bitfields.) */ \
465 : static const long double __assert_in_set[] _unused_ = { __VA_ARGS__ }; \
466 : assert_cc(ELEMENTSOF(__assert_in_set) <= 20); \
467 : switch(x) { \
468 : FOR_EACH_MAKE_CASE(__VA_ARGS__) \
469 : _found = true; \
470 : break; \
471 : default: \
472 : break; \
473 : } \
474 : _found; \
475 : })
476 :
477 : #define SWAP_TWO(x, y) do { \
478 : typeof(x) _t = (x); \
479 : (x) = (y); \
480 : (y) = (_t); \
481 : } while (false)
482 :
483 : /* Define C11 thread_local attribute even on older gcc compiler
484 : * version */
485 : #ifndef thread_local
486 : /*
487 : * Don't break on glibc < 2.16 that doesn't define __STDC_NO_THREADS__
488 : * see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53769
489 : */
490 : #if __STDC_VERSION__ >= 201112L && !(defined(__STDC_NO_THREADS__) || (defined(__GNU_LIBRARY__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16))
491 : #define thread_local _Thread_local
492 : #else
493 : #define thread_local __thread
494 : #endif
495 : #endif
496 :
497 : #define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func) \
498 : static inline void name(type *p) { \
499 : func(p); \
500 : }
501 :
502 : #define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
503 : static inline void func##p(type *p) { \
504 : if (*p) \
505 : func(*p); \
506 : }
507 :
508 : #define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \
509 : scope type *name##_ref(type *p) { \
510 : if (!p) \
511 : return NULL; \
512 : \
513 : assert(p->n_ref > 0); \
514 : p->n_ref++; \
515 : return p; \
516 : }
517 :
518 : #define _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, scope) \
519 : scope type *name##_unref(type *p) { \
520 : if (!p) \
521 : return NULL; \
522 : \
523 : assert(p->n_ref > 0); \
524 : p->n_ref--; \
525 : if (p->n_ref > 0) \
526 : return NULL; \
527 : \
528 : return free_func(p); \
529 : }
530 :
531 : #define DEFINE_TRIVIAL_REF_FUNC(type, name) \
532 : _DEFINE_TRIVIAL_REF_FUNC(type, name,)
533 : #define DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name) \
534 : _DEFINE_TRIVIAL_REF_FUNC(type, name, static)
535 : #define DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name) \
536 : _DEFINE_TRIVIAL_REF_FUNC(type, name, _public_)
537 :
538 : #define DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
539 : _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func,)
540 : #define DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
541 : _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, static)
542 : #define DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func) \
543 : _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, _public_)
544 :
545 : #define DEFINE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
546 : DEFINE_TRIVIAL_REF_FUNC(type, name); \
547 : DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func);
548 :
549 : #define DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
550 : DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name); \
551 : DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func);
552 :
553 : #define DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
554 : DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \
555 : DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func);
556 :
557 : #include "log.h"
|