2020-09-09 06:56:04 +00:00
|
|
|
#ifndef HEADER_Macros
|
|
|
|
#define HEADER_Macros
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <assert.h> // IWYU pragma: keep
|
|
|
|
|
2020-09-09 06:56:04 +00:00
|
|
|
#ifndef MINIMUM
|
2021-03-21 18:40:56 +00:00
|
|
|
#define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
|
2020-09-09 06:56:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAXIMUM
|
2021-03-21 18:40:56 +00:00
|
|
|
#define MAXIMUM(a, b) ((a) > (b) ? (a) : (b))
|
2020-09-09 06:56:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CLAMP
|
2021-03-21 18:40:56 +00:00
|
|
|
#define CLAMP(x, low, high) (assert((low) <= (high)), ((x) > (high)) ? (high) : MAXIMUM(x, low))
|
2020-09-09 06:56:04 +00:00
|
|
|
#endif
|
|
|
|
|
2020-09-28 19:14:50 +00:00
|
|
|
#ifndef ARRAYSIZE
|
2021-03-21 18:40:56 +00:00
|
|
|
#define ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0]))
|
2020-11-04 16:46:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SPACESHIP_NUMBER
|
2021-03-21 18:40:56 +00:00
|
|
|
#define SPACESHIP_NUMBER(a, b) (((a) > (b)) - ((a) < (b)))
|
2020-11-04 16:46:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SPACESHIP_NULLSTR
|
2021-03-21 18:40:56 +00:00
|
|
|
#define SPACESHIP_NULLSTR(a, b) strcmp((a) ? (a) : "", (b) ? (b) : "")
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SPACESHIP_DEFAULTSTR
|
|
|
|
#define SPACESHIP_DEFAULTSTR(a, b, s) strcmp((a) ? (a) : (s), (b) ? (b) : (s))
|
2020-09-28 19:14:50 +00:00
|
|
|
#endif
|
|
|
|
|
2020-09-09 19:35:15 +00:00
|
|
|
#ifdef __GNUC__ // defined by GCC and Clang
|
|
|
|
|
|
|
|
#define ATTR_FORMAT(type, index, check) __attribute__((format (type, index, check)))
|
|
|
|
#define ATTR_NONNULL __attribute__((nonnull))
|
|
|
|
#define ATTR_NORETURN __attribute__((noreturn))
|
|
|
|
#define ATTR_UNUSED __attribute__((unused))
|
2020-11-20 16:04:19 +00:00
|
|
|
#define ATTR_MALLOC __attribute__((malloc))
|
2020-09-09 19:35:15 +00:00
|
|
|
|
|
|
|
#else /* __GNUC__ */
|
|
|
|
|
|
|
|
#define ATTR_FORMAT(type, index, check)
|
|
|
|
#define ATTR_NONNULL
|
|
|
|
#define ATTR_NORETURN
|
|
|
|
#define ATTR_UNUSED
|
2020-11-20 16:04:19 +00:00
|
|
|
#define ATTR_MALLOC
|
2020-09-09 19:35:15 +00:00
|
|
|
|
|
|
|
#endif /* __GNUC__ */
|
|
|
|
|
2021-06-27 10:44:01 +00:00
|
|
|
#ifdef HAVE_ATTR_ALLOC_SIZE
|
|
|
|
|
|
|
|
#define ATTR_ALLOC_SIZE1(a) __attribute__((alloc_size (a)))
|
|
|
|
#define ATTR_ALLOC_SIZE2(a, b) __attribute__((alloc_size (a, b)))
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define ATTR_ALLOC_SIZE1(a)
|
|
|
|
#define ATTR_ALLOC_SIZE2(a, b)
|
|
|
|
|
|
|
|
#endif /* HAVE_ATTR_ALLOC_SIZE */
|
|
|
|
|
2020-10-04 12:30:35 +00:00
|
|
|
// ignore casts discarding const specifier, e.g.
|
|
|
|
// const char [] -> char * / void *
|
|
|
|
// const char *[2]' -> char *const *
|
2021-03-20 10:21:20 +00:00
|
|
|
#if defined(__clang__)
|
2020-10-04 12:30:35 +00:00
|
|
|
#define IGNORE_WCASTQUAL_BEGIN _Pragma("clang diagnostic push") \
|
|
|
|
_Pragma("clang diagnostic ignored \"-Wcast-qual\"")
|
|
|
|
#define IGNORE_WCASTQUAL_END _Pragma("clang diagnostic pop")
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#define IGNORE_WCASTQUAL_BEGIN _Pragma("GCC diagnostic push") \
|
|
|
|
_Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
|
|
|
|
#define IGNORE_WCASTQUAL_END _Pragma("GCC diagnostic pop")
|
|
|
|
#else
|
|
|
|
#define IGNORE_WCASTQUAL_BEGIN
|
|
|
|
#define IGNORE_WCASTQUAL_END
|
|
|
|
#endif
|
|
|
|
|
2021-06-12 08:52:57 +00:00
|
|
|
/* This subtraction is used by Linux / NetBSD / OpenBSD for calculation of CPU usage items. */
|
2021-04-25 16:14:32 +00:00
|
|
|
static inline unsigned long long saturatingSub(unsigned long long a, unsigned long long b) {
|
|
|
|
return a > b ? a - b : 0;
|
|
|
|
}
|
|
|
|
|
2020-09-09 06:56:04 +00:00
|
|
|
#endif
|