35 # define static_assert _Static_assert
47 #ifdef __clang_analyzer__
48 #define STATIC_ANALYZER 1
51 #define STATIC_ANALYZER 1
59 #ifdef STATIC_ANALYZER
60 # define WITH_VERIFY_PTR 1
67 #ifndef __SANITIZE_ADDRESS__
69 #if __has_feature(address_sanitizer)
70 #define __SANITIZE_ADDRESS__ (1)
90 #if (defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__)) || !defined(__EMSCRIPTEN__) && defined(HAVE_PTHREAD_H)
91 # define HAVE_PTHREADS 1
102 #define CMP_PREFER_SMALLER(_a,_b) (((_a) > (_b)) - ((_a) < (_b)))
106 #define CMP_PREFER_LARGER(_a,_b) (((_a) < (_b)) - ((_a) > (_b)))
110 #define CMP(_a, _b) CMP_PREFER_SMALLER(_a, _b)
119 #define CMP_RETURN(_a, _b, _field) \
121 int8_t _ret = CMP((_a)->_field, (_b)->_field); \
122 if (_ret != 0) return _ret; \
136 static inline int8_t
memcmp_return(
void const *a,
void const *b,
size_t a_len,
size_t b_len)
138 size_t cmp_len = (a_len < b_len) ? a_len : b_len;
139 int8_t l_ret =
CMP(a_len, b_len);
141 ret =
CMP(memcmp(a, b, cmp_len), 0);
142 if (ret != 0)
return ret;
154 #define MEMCMP_RETURN(_a, _b, _field, _len_field) \
156 int8_t _ret = memcmp_return((_a)->_field, (_b)->_field, (_a)->_len_field, (_b)->_len_field); \
157 if (_ret != 0) return _ret; \
165 #define UNCONST(_type, _ptr) ((_type)((uintptr_t)(_ptr)))
172 #define typeof_field(_type, _field) __typeof__(((_type *)NULL)->_field)
178 # define XHEXIFY4(b1,b2,b3,b4) (0x ## b1 ## b2 ## b3 ## b4)
179 # define HEXIFY4(b1,b2,b3,b4) XHEXIFY4(b1, b2, b3, b4)
181 # define XHEXIFY3(b1,b2,b3) (0x ## b1 ## b2 ## b3)
182 # define HEXIFY3(b1,b2,b3) XHEXIFY3(b1, b2, b3)
184 # define XHEXIFY2(b1,b2) (0x ## b1 ## b2)
185 # define HEXIFY2(b1,b2) XHEXIFY2(b1, b2)
187 # define XHEXIFY(b1) (0x ## b1)
188 # define HEXIFY(b1) XHEXIFY(b1)
194 #define XSTRINGIFY(x) #x
195 #define STRINGIFY(x) XSTRINGIFY(x)
196 #define JOINSTR(x,y) XSTRINGIFY(x ## y)
202 #define _JOIN(x,y) x ## y
203 #define JOIN(x,y) _JOIN(x,y)
207 #define L(_str) { _str, sizeof(_str) - 1 }
211 #define F1(_idx, _val) [_idx] = _val
212 #define F2(_idx, _val) F1(_idx, _val), F1(_idx + 1, _val)
213 #define F4(_idx, _val) F2(_idx, _val), F2(_idx + 2, _val)
214 #define F8(_idx, _val) F4(_idx, _val), F4(_idx + 4, _val)
215 #define F16(_idx, _val) F8(_idx, _val), F8(_idx + 8, _val)
216 #define F32(_idx, _val) F16(_idx, _val), F16(_idx + 16, _val)
217 #define F64(_idx, _val) F32(_idx, _val), F32(_idx + 32, _val)
218 #define F128(_idx, _val) F64(_idx, _val), F64(_idx + 64, _val)
219 #define F256(_idx, _val) F128(_idx, _val), F128(_idx + 128, _val)
231 _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
232 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
233 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
234 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
235 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
236 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
239 #define VA_RSEQ_N() \
241 59,58,57,56,55,54,53,52,51,50, \
242 49,48,47,46,45,44,43,42,41,40, \
243 39,38,37,36,35,34,33,32,31,30, \
244 29,28,27,26,25,24,23,22,21,20, \
245 19,18,17,16,15,14,13,12,11,10, \
248 #define _VA_NARG(...) VA_ARG_N(__VA_ARGS__)
254 #define VA_NARG(...) _VA_NARG(__VA_ARGS__, VA_RSEQ_N())
261 # define NDEBUG_LOCATION_ARGS char const *file, int line,
262 # define NDEBUG_LOCATION_VALS file, line,
263 # define NDEBUG_LOCATION_EXP __FILE__, __LINE__,
264 # define NDEBUG_LOCATION_NONNULL(_num) ((_num) + 2)
266 # define NDEBUG_LOCATION_ARGS
267 # define NDEBUG_LOCATION_VALS
268 # define NDEBUG_LOCATION_EXP
269 # define NDEBUG_LOCATION_NONNULL(_num) (_num)
277 #define IS_CONST(_type, _var) \
293 #define IS_TYPE(_type, _var) \
301 #define UNUSED_VAR(_x) ((void)_x)
306 #define PAD(_x, _y) (_y - ((_x) % _y))
311 #define NEVER_RETURNS _Noreturn
312 #define HIDDEN CC_HINT(visibility("hidden"))
313 #define UNUSED CC_HINT(unused)
317 #if (defined(__clang__) && (__clang_major__ >= 10)) || (defined(__GNUC__) && __GNUC__ >= 7)
318 # define FALL_THROUGH CC_HINT(fallthrough)
320 # define FALL_THROUGH ((void)0)
324 # define NDEBUG_UNUSED
326 # define NDEBUG_UNUSED UNUSED
329 #define BLANK_FORMAT " "
334 #define SIZEOF_MEMBER(_t, _m) sizeof(((_t *)0)->_m)
335 #define NUM_ELEMENTS(_t) (sizeof((_t)) / sizeof((_t)[0]))
342 #define NUM_PTR_ELEMENTS(_t) (sizeof((_t)) / sizeof(void *))
355 #define IS_COMPATIBLE(_x, _t) _Generic(_x, _t:1, default: 0)
365 #define IS_FIELD_COMPATIBLE(_s, _f, _t) _Generic(((_s *)0)->_f, _t:1, default: 0)
372 # define CC_HINT(...) __attribute__((__VA_ARGS__))
373 # define likely(_x) __builtin_expect((_x), 1)
374 # define unlikely(_x) __builtin_expect((_x), 0)
375 # define unpredictable(_x) __builtin_unpredictable((_x))
377 # define CC_HINT(...)
378 # define likely(_x) _x
379 # define unlikely(_x) _x
380 # define unpredictable(_x) _x
387 #define __GNUC_PREREQ__(x, y) \
388 ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) || \
391 #define __GNUC_PREREQ__(x, y) 0
398 #define PRAGMA(_x) _Pragma(#_x)
403 #if defined(__clang__) && (__clang_major__ >= 13)
404 # define CC_ACQUIRE_HANDLE(_tag) CC_HINT(acquire_handle(_tag))
405 # define CC_USE_HANDLE(_tag) CC_HINT(use_handle(_tag))
406 # define CC_RELEASE_HANDLE(_tag) CC_HINT(release_handle(_tag))
408 # define CC_ACQUIRE_HANDLE(_tag)
409 # define CC_USE_HANDLE(_tag)
410 # define CC_RELEASE_HANDLE(_tag)
416 #ifndef __has_feature
417 # define __has_feature(_x) 0
419 #if defined(__clang__) && __has_feature(undefined_behavior_sanitizer)
420 # define CC_NO_UBSAN(_sanitize) __attribute__((no_sanitize(STRINGIFY(_sanitize))))
421 #elif __GNUC_PREREQ__(4, 9) && defined(__SANITIZE_UNDEFINED__)
422 # define CC_NO_UBSAN(_sanitize) __attribute__((no_sanitize_undefined))
424 # define CC_NO_UBSAN(_sanitize)
430 #if defined(__clang__)
431 # define CC_NO_SANITIZE_UNDEFINED(_what) CC_HINT(no_sanitize(_what))
433 # define CC_NO_SANITIZE_UNDEFINED(_what)
439 #if defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
440 # define DIAG_UNKNOWN_PRAGMAS unknown-pragmas
441 # define DIAG_PRAGMA(_x) PRAGMA(clang diagnostic _x)
442 # define DIAG_OFF(_x) DIAG_PRAGMA(ignored JOINSTR(-W,_x))
443 # define DIAG_ON(_x) DIAG_PRAGMA(warning JOINSTR(-W,_x))
444 # define DIAG_PUSH() DIAG_PRAGMA(push)
445 # define DIAG_POP() DIAG_PRAGMA(pop)
446 #elif !defined(__clang__) && defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
447 # define DIAG_UNKNOWN_PRAGMAS pragmas
448 # define DIAG_PRAGMA(_x) PRAGMA(GCC diagnostic _x)
449 # define DIAG_OFF(_x) DIAG_PRAGMA(ignored JOINSTR(-W,_x))
450 # define DIAG_ON(_x) DIAG_PRAGMA(warning JOINSTR(-W,_x))
451 # define DIAG_PUSH() DIAG_PRAGMA(push)
452 # define DIAG_POP() DIAG_PRAGMA(pop)
454 # define DIAG_UNKNOWN_PRAGMAS
455 # define DIAG_OFF(_x)
465 # define USES_APPLE_DEPRECATED_API DIAG_OFF(deprecated-declarations)
466 # define USES_APPLE_RST DIAG_ON(deprecated-declarations)
468 # define USES_APPLE_DEPRECATED_API
469 # define USES_APPLE_RST
472 #if defined(__GNUC__)
474 # define RCSID(id) static char const rcsid[] __attribute__ ((used)) = id;
475 # define RCSIDH(h, id) static char const rcsid_ ## h [] __attribute__ ((used)) = id;
476 #elif defined(__SUNPRO_C)
478 # define RCSID(id) PRAGMA(sun ident id)
479 # define RCSIDH(h, id) PRAGMA(sun ident id)
482 # define RCSIDH(h, id)
505 #define IGNORE(_expr, _type) \
507 _type ignored UNUSED = (_expr); \
static int8_t memcmp_return(void const *a, void const *b, size_t a_len, size_t b_len)
memcmp function which has similar behaviour as strncmp
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.