1 /* 2 * This file Copyright (C) 2017 Mnemosyne LLC 3 * 4 * It may be used under the GNU GPL versions 2 or 3 5 * or any future license endorsed by Mnemosyne LLC. 6 * 7 */ 8 9 #pragma once 10 11 /*** 12 **** 13 ***/ 14 15 #ifndef __has_feature 16 #define __has_feature(x) 0 17 #endif 18 19 #ifndef __has_extension 20 #define __has_extension __has_feature 21 #endif 22 23 #ifndef __has_attribute 24 #define __has_attribute(x) 0 25 #endif 26 27 #ifndef __has_builtin 28 #define __has_builtin(x) 0 29 #endif 30 31 #ifdef _WIN32 32 #define TR_IF_WIN32(ThenValue, ElseValue) ThenValue 33 #else 34 #define TR_IF_WIN32(ThenValue, ElseValue) ElseValue 35 #endif 36 37 #ifdef __GNUC__ 38 #define TR_GNUC_CHECK_VERSION(major, minor) \ 39 (__GNUC__ > (major) || \ 40 (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) 41 #else 42 #define TR_GNUC_CHECK_VERSION(major, minor) 0 43 #endif 44 45 #ifdef __UCLIBC__ 46 #define TR_UCLIBC_CHECK_VERSION(major, minor, micro) \ 47 (__UCLIBC_MAJOR__ > (major) || \ 48 (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ > (minor)) || \ 49 (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ == (minor) && __UCLIBC_SUBLEVEL__ >= (micro))) 50 #else 51 #define TR_UCLIBC_CHECK_VERSION(major, minor, micro) 0 52 #endif 53 54 /*** 55 **** 56 ***/ 57 58 #ifndef UNUSED 59 #if __has_attribute(__unused__) || TR_GNUC_CHECK_VERSION(2, 7) 60 #define UNUSED __attribute__((__unused__)) 61 #else 62 #define UNUSED 63 #endif 64 #endif 65 66 /*** 67 **** 68 ***/ 69 70 #if __has_builtin(__builtin_expect) || TR_GNUC_CHECK_VERSION(3, 0) 71 #define TR_LIKELY(x) __builtin_expect(!!(x), 1) 72 #define TR_UNLIKELY(x) __builtin_expect(!!(x), 0) 73 #else 74 #define TR_LIKELY(x) (x) 75 #define TR_UNLIKELY(x) (x) 76 #endif 77 78 /*** 79 **** 80 ***/ 81 82 #if __has_attribute(__noreturn__) || TR_GNUC_CHECK_VERSION(2, 5) 83 #define TR_NORETURN __attribute__((__noreturn__)) 84 #elif defined(_MSC_VER) 85 #define TR_NORETURN __declspec(noreturn) 86 #else 87 #define TR_NORETURN 88 #endif 89 90 #if __has_attribute(__deprecated__) || TR_GNUC_CHECK_VERSION(3, 1) 91 #define TR_DEPRECATED __attribute__((__deprecated__)) 92 #elif defined(_MSC_VER) 93 #define TR_DEPRECATED __declspec(deprecated) 94 #else 95 #define TR_DEPRECATED 96 #endif 97 98 #if __has_attribute(__format__) || TR_GNUC_CHECK_VERSION(2, 3) 99 #define TR_GNUC_PRINTF(fmt, args) __attribute__((__format__(printf, fmt, args))) 100 #else 101 #define TR_GNUC_PRINTF(fmt, args) 102 #endif 103 104 #if __has_attribute(__nonnull__) || TR_GNUC_CHECK_VERSION(3, 3) 105 #define TR_GNUC_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__))) 106 #else 107 #define TR_GNUC_NONNULL(...) 108 #endif 109 110 #if __has_attribute(__sentinel__) || TR_GNUC_CHECK_VERSION(4, 3) 111 #define TR_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) 112 #else 113 #define TR_GNUC_NULL_TERMINATED 114 #endif 115 116 #if __has_attribute(__hot__) || TR_GNUC_CHECK_VERSION(4, 3) 117 #define TR_GNUC_HOT __attribute__((__hot__)) 118 #else 119 #define TR_GNUC_HOT 120 #endif 121 122 #if __has_attribute(__malloc__) || TR_GNUC_CHECK_VERSION(2, 96) 123 #define TR_GNUC_MALLOC __attribute__((__malloc__)) 124 #else 125 #define TR_GNUC_MALLOC 126 #endif 127 128 #if __has_attribute(__fallthrough__) || TR_GNUC_CHECK_VERSION(7, 0) 129 #define TR_GNUC_FALLTHROUGH __attribute__((__fallthrough__)) 130 #else 131 #define TR_GNUC_FALLTHROUGH 132 #endif 133 134 /*** 135 **** 136 ***/ 137 138 /** 139 * @def TR_STATIC_ASSERT 140 * @brief This helper allows to perform static checks at compile time 141 */ 142 #if defined(static_assert) 143 #define TR_STATIC_ASSERT static_assert 144 #elif __has_feature(c_static_assert) || __has_extension(c_static_assert) 145 #define TR_STATIC_ASSERT _Static_assert 146 #else 147 #define TR_STATIC_ASSERT(x, msg) \ 148 { \ 149 typedef char __tr_static_check__ [(x) ? 1 : -1] UNUSED; \ 150 } 151 #endif 152 153 /* Sometimes the system defines MAX/MIN, sometimes not. 154 In the latter case, define those here since we will use them */ 155 156 #ifndef MAX 157 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 158 #endif 159 160 #ifndef MIN 161 #define MIN(a, b) ((a) > (b) ? (b) : (a)) 162 #endif 163 164 /*** 165 **** 166 ***/ 167 168 /* Only use this macro to suppress false-positive alignment warnings */ 169 #define TR_DISCARD_ALIGN(ptr, type) ((type)(void*)(ptr)) 170 171 #define SHA_DIGEST_LENGTH 20 172 173 #define TR_INET6_ADDRSTRLEN 46 174 175 #define TR_BAD_SIZE ((size_t)-1) 176