| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef LIBNETDATA_BASE_H |
| 4 | #define LIBNETDATA_BASE_H |
| 5 | |
| 6 | #include "config.h" |
| 7 | #include <stdbool.h> |
| 8 | #include <stddef.h> |
| 9 | #if defined(HAVE_INTTYPES_H) |
| 10 | #include <inttypes.h> |
| 11 | #elif defined(HAVE_STDINT_H) |
| 12 | #include <stdint.h> |
| 13 | #endif |
| 14 | |
| 15 | #if defined(NETDATA_DEV_MODE) && !defined(NETDATA_INTERNAL_CHECKS) |
| 16 | #define NETDATA_INTERNAL_CHECKS 1 |
| 17 | #endif |
| 18 | |
| 19 | #ifndef SIZEOF_VOID_P |
| 20 | #error SIZEOF_VOID_P is not defined |
| 21 | #endif |
| 22 | |
| 23 | #if SIZEOF_VOID_P == 4 |
| 24 | #define ENV32BIT 1 |
| 25 | #else |
| 26 | #define ENV64BIT 1 |
| 27 | #endif |
| 28 | |
| 29 | #ifdef HAVE_LIBDATACHANNEL |
| 30 | #define ENABLE_WEBRTC 1 |
| 31 | #endif |
| 32 | |
| 33 | #define STRINGIFY(x) #x |
| 34 | #define TOSTRING(x) STRINGIFY(x) |
| 35 | |
| 36 | #define _cleanup_(x) __attribute__((__cleanup__(x))) |
| 37 | |
| 38 | #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL |
| 39 | #define NEVERNULL __attribute__((returns_nonnull)) |
| 40 | #else |
| 41 | #define NEVERNULL |
| 42 | #endif |
| 43 | |
| 44 | #ifdef HAVE_FUNC_ATTRIBUTE_NOINLINE |
| 45 | #define NOINLINE __attribute__((noinline)) |
| 46 | #else |
| 47 | #define NOINLINE |
| 48 | #endif |
| 49 | |
| 50 | #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC |
| 51 | #define MALLOCLIKE __attribute__((malloc)) |
| 52 | #else |
| 53 | #define MALLOCLIKE |
| 54 | #endif |
| 55 | |
| 56 | #if defined(HAVE_FUNC_ATTRIBUTE_FORMAT_GNU_PRINTF) |
| 57 | #define PRINTFLIKE(f, a) __attribute__ ((format(gnu_printf, f, a))) |
| 58 | #elif defined(HAVE_FUNC_ATTRIBUTE_FORMAT_PRINTF) |
| 59 | #define PRINTFLIKE(f, a) __attribute__ ((format(printf, f, a))) |
| 60 | #else |
| 61 | #define PRINTFLIKE(f, a) |
| 62 | #endif |
| 63 | |
| 64 | #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN |
| 65 | #define NORETURN __attribute__ ((noreturn)) |
| 66 | #else |
| 67 | #define NORETURN |
| 68 | #endif |
| 69 | |
| 70 | #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT |
| 71 | #define WARNUNUSED __attribute__ ((warn_unused_result)) |
| 72 | #else |
| 73 | #define WARNUNUSED |
| 74 | #endif |
| 75 | |
| 76 | #define UNUSED(x) (void)(x) |
| 77 | |
| 78 | #if defined(__GNUC__) && !defined(FSANITIZE_ADDRESS) |
| 79 | #define UNUSED_FUNCTION(x) __attribute__((unused)) UNUSED_##x |
| 80 | #define ALWAYS_INLINE_ONLY __attribute__((always_inline)) |
| 81 | #define ALWAYS_INLINE inline __attribute__((always_inline)) |
| 82 | #define ALWAYS_INLINE_HOT inline __attribute__((hot, always_inline)) |
| 83 | #define ALWAYS_INLINE_HOT_FLATTEN inline __attribute__((hot, always_inline, flatten)) |
| 84 | #define NOT_INLINE_HOT __attribute__((hot)) |
| 85 | #define NEVER_INLINE __attribute__((noinline)) |
| 86 | #else |
| 87 | #define UNUSED_FUNCTION(x) UNUSED_##x |
| 88 | #define ALWAYS_INLINE_ONLY |
| 89 | #define ALWAYS_INLINE inline |
| 90 | #define ALWAYS_INLINE_HOT inline |
| 91 | #define ALWAYS_INLINE_HOT_FLATTEN inline |
| 92 | #define NOT_INLINE_HOT |
| 93 | #define NEVER_INLINE |
| 94 | #endif |
| 95 | |
| 96 | #define ABS(x) (((x) < 0)? (-(x)) : (x)) |
| 97 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
| 98 | #define MAX(a,b) (((a)>(b))?(a):(b)) |
| 99 | #define SWAP(a, b) do { \ |
| 100 | typeof(a) _tmp = b; \ |
| 101 | b = a; \ |
| 102 | a = _tmp; \ |
| 103 | } while(0) |
| 104 | |
| 105 | #define HOWMANY(total, divider) ({ \ |
| 106 | typeof(total) _t = (total); \ |
| 107 | typeof(total) _d = (divider); \ |
| 108 | _d = _d ? _d : 1; \ |
| 109 | (_t + (_d - 1)) / _d; \ |
| 110 | }) |
| 111 | |
| 112 | #define FIT_IN_RANGE(value, min, max) ({ \ |
| 113 | typeof(value) _v = (value); \ |
| 114 | typeof(min) _min = (min); \ |
| 115 | typeof(max) _max = (max); \ |
| 116 | (_v < _min) ? _min : ((_v > _max) ? _max : _v); \ |
| 117 | }) |
| 118 | |
| 119 | #define PIPE_READ 0 |
| 120 | #define PIPE_WRITE 1 |
| 121 | |
| 122 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
| 123 | |
| 124 | #define CONCAT_INDIRECT(a, b) a##b |
| 125 | #define CONCAT(a, b) CONCAT_INDIRECT(a, b) |
| 126 | #define PAD64(type) uint8_t CONCAT(padding, __COUNTER__)[64 - sizeof(type)]; type |
| 127 | |
| 128 | #endif // LIBNETDATA_BASE_H |