1 #ifndef HEADER_Macros
2 #define HEADER_Macros
3
4 #include <assert.h> // IWYU pragma: keep
5
6 #ifndef MINIMUM
7 #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
8 #endif
9
10 #ifndef MAXIMUM
11 #define MAXIMUM(a, b) ((a) > (b) ? (a) : (b))
12 #endif
13
14 #ifndef CLAMP
15 #define CLAMP(x, low, high) (assert((low) <= (high)), ((x) > (high)) ? (high) : MAXIMUM(x, low))
16 #endif
17
18 #ifndef ARRAYSIZE
19 #define ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0]))
20 #endif
21
22 #ifndef SPACESHIP_NUMBER
23 #define SPACESHIP_NUMBER(a, b) (((a) > (b)) - ((a) < (b)))
24 #endif
25
26 #ifndef SPACESHIP_NULLSTR
27 #define SPACESHIP_NULLSTR(a, b) strcmp((a) ? (a) : "", (b) ? (b) : "")
28 #endif
29
30 #ifndef SPACESHIP_DEFAULTSTR
31 #define SPACESHIP_DEFAULTSTR(a, b, s) strcmp((a) ? (a) : (s), (b) ? (b) : (s))
32 #endif
33
34 #ifdef __GNUC__ // defined by GCC and Clang
35
36 #define ATTR_FORMAT(type, index, check) __attribute__((format (type, index, check)))
37 #define ATTR_NONNULL __attribute__((nonnull))
38 #define ATTR_NORETURN __attribute__((noreturn))
39 #define ATTR_UNUSED __attribute__((unused))
40 #define ATTR_MALLOC __attribute__((malloc))
41
42 #else /* __GNUC__ */
43
44 #define ATTR_FORMAT(type, index, check)
45 #define ATTR_NONNULL
46 #define ATTR_NORETURN
47 #define ATTR_UNUSED
48 #define ATTR_MALLOC
49
50 #endif /* __GNUC__ */
51
52 #ifdef HAVE_ATTR_ALLOC_SIZE
53
54 #define ATTR_ALLOC_SIZE1(a) __attribute__((alloc_size (a)))
55 #define ATTR_ALLOC_SIZE2(a, b) __attribute__((alloc_size (a, b)))
56
57 #else
58
59 #define ATTR_ALLOC_SIZE1(a)
60 #define ATTR_ALLOC_SIZE2(a, b)
61
62 #endif /* HAVE_ATTR_ALLOC_SIZE */
63
64 // ignore casts discarding const specifier, e.g.
65 // const char [] -> char * / void *
66 // const char *[2]' -> char *const *
67 #if defined(__clang__)
68 #define IGNORE_WCASTQUAL_BEGIN _Pragma("clang diagnostic push") \
69 _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
70 #define IGNORE_WCASTQUAL_END _Pragma("clang diagnostic pop")
71 #elif defined(__GNUC__)
72 #define IGNORE_WCASTQUAL_BEGIN _Pragma("GCC diagnostic push") \
73 _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
74 #define IGNORE_WCASTQUAL_END _Pragma("GCC diagnostic pop")
75 #else
76 #define IGNORE_WCASTQUAL_BEGIN
77 #define IGNORE_WCASTQUAL_END
78 #endif
79
80 /* This subtraction is used by Linux / NetBSD / OpenBSD for calculation of CPU usage items. */
saturatingSub(unsigned long long a,unsigned long long b)81 static inline unsigned long long saturatingSub(unsigned long long a, unsigned long long b) {
82 return a > b ? a - b : 0;
83 }
84
85 #endif
86