| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _USUAL_BASE_H_ |
| 22 |
#define _USUAL_BASE_H_ |
| 23 |
|
| 24 |
#ifdef USUAL_TEST_CONFIG |
| 25 |
#include "test_config.h" |
| 26 |
#elif defined(_MSC_VER) |
| 27 |
#include <usual/config_msvc.h> |
| 28 |
#else |
| 29 |
#include <usual/config.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
#if defined(__sun) |
| 34 |
#define _XPG4_2 |
| 35 |
#define __EXTENSIONS__ |
| 36 |
#endif |
| 37 |
|
| 38 |
|
| 39 |
#ifndef __STDC_WANT_LIB_EXT1__ |
| 40 |
#define __STDC_WANT_LIB_EXT1__ 1 |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <sys/types.h> |
| 44 |
#ifdef HAVE_SYS_PARAM_H |
| 45 |
#include <sys/param.h> |
| 46 |
#endif |
| 47 |
#include <stddef.h> |
| 48 |
#include <stdint.h> |
| 49 |
#ifdef HAVE_INTTYPES_H |
| 50 |
#include <inttypes.h> |
| 51 |
#endif |
| 52 |
#include <stdio.h> |
| 53 |
#include <stdlib.h> |
| 54 |
#include <stdarg.h> |
| 55 |
#include <errno.h> |
| 56 |
#include <assert.h> |
| 57 |
#ifdef HAVE_UNISTD_H |
| 58 |
#include <unistd.h> |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifdef HAVE_STDBOOL_H |
| 62 |
#include <stdbool.h> |
| 63 |
#else |
| 64 |
|
| 65 |
typedef enum { true=1, false=0 } bool; |
| 66 |
#endif |
| 67 |
|
| 68 |
#ifdef WIN32 |
| 69 |
#include <usual/base_win32.h> |
| 70 |
#define DLLEXPORT __declspec(dllexport) |
| 71 |
#define DLLIMPORT __declspec(dllimport) |
| 72 |
#else |
| 73 |
#define DLLEXPORT |
| 74 |
#define DLLIMPORT |
| 75 |
#endif |
| 76 |
|
| 77 |
|
| 78 |
#ifndef offsetof |
| 79 |
#define offsetof(type, field) ((unsigned long)&(((type *)0)->field)) |
| 80 |
#endif |
| 81 |
|
| 82 |
|
| 83 |
#ifndef container_of |
| 84 |
#define container_of(ptr, type, field) ((type *)((char *)(ptr) - offsetof(type, field))) |
| 85 |
#endif |
| 86 |
|
| 87 |
|
| 88 |
#ifndef alignof |
| 89 |
#define alignof(type) offsetof(struct { char c; type t; }, t) |
| 90 |
#endif |
| 91 |
|
| 92 |
|
| 93 |
#ifndef CUSTOM_ALIGN |
| 94 |
#define CUSTOM_ALIGN(x, a) (((uintptr_t)(x) + (uintptr_t)(a) - 1) & ~((uintptr_t)(a) - 1)) |
| 95 |
#endif |
| 96 |
|
| 97 |
|
| 98 |
#ifndef ALIGN |
| 99 |
#define ALIGN(x) CUSTOM_ALIGN(x, sizeof(long)) |
| 100 |
#endif |
| 101 |
|
| 102 |
|
| 103 |
#define ARRAY_NELEM(a) (sizeof(a) / sizeof((a)[0])) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) |
| 115 |
#define FLEX_ARRAY |
| 116 |
#elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 117 |
#define FLEX_ARRAY |
| 118 |
#else |
| 119 |
#define FLEX_ARRAY 1 |
| 120 |
#endif |
| 121 |
|
| 122 |
|
| 123 |
#define STR(x) _STR_(x) |
| 124 |
#define _STR_(x) #x |
| 125 |
|
| 126 |
|
| 127 |
#define CONCAT(a, b) _CONCAT_(a, b) |
| 128 |
#define _CONCAT_(a, b) a ## b |
| 129 |
|
| 130 |
|
| 131 |
#define CONCAT3(a, b, c) _CONCAT3_(a, b, c) |
| 132 |
#define _CONCAT3_(a, b, c) a ## b ## c |
| 133 |
|
| 134 |
|
| 135 |
#define CONCAT4(a, b, c, d) _CONCAT4_(a, b, c, d) |
| 136 |
#define _CONCAT4_(a, b, c, d) a ## b ## c ## d |
| 137 |
|
| 138 |
|
| 139 |
#ifndef HAVE_FUNCNAME__FUNC |
| 140 |
#define __func__ __FUNCTION__ |
| 141 |
#endif |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
#if defined(__GNUC__) |
| 151 |
#define _COMPILER_GNUC(maj,min) ((__GNUC__ > (maj)) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min))) |
| 152 |
#else |
| 153 |
#define _COMPILER_GNUC(maj,min) (0) |
| 154 |
#endif |
| 155 |
|
| 156 |
#if defined(__clang__) |
| 157 |
#define _COMPILER_CLANG(maj,min) ((__clang_major__ > (maj)) || (__clang_major__ == (maj) && __clang_minor__ >= (min))) |
| 158 |
#else |
| 159 |
#define _COMPILER_CLANG(maj,min) (0) |
| 160 |
#endif |
| 161 |
|
| 162 |
#if defined(_MSC_VER) |
| 163 |
#define _COMPILER_MSC(ver) (_MSC_VER >= (ver)) |
| 164 |
#else |
| 165 |
#define _COMPILER_MSC(ver) (0) |
| 166 |
#endif |
| 167 |
|
| 168 |
#if defined(__INTEL_COMPILER) |
| 169 |
#define _COMPILER_ICC(ver) (__INTEL_COMPILER >= (ver)) |
| 170 |
#else |
| 171 |
#define _COMPILER_ICC(ver) (0) |
| 172 |
#endif |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
#ifndef __has_builtin |
| 182 |
#define __has_builtin(x) (0) |
| 183 |
#endif |
| 184 |
#ifndef __has_feature |
| 185 |
#define __has_feature(x) (0) |
| 186 |
#endif |
| 187 |
#ifndef __has_extension |
| 188 |
#define __has_extension(x) __has_feature(x) |
| 189 |
#endif |
| 190 |
#ifndef __has_attribute |
| 191 |
#define __has_attribute(x) (0) |
| 192 |
#endif |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
#ifndef _MSC_VER |
| 212 |
#define _PACKED __attribute__((packed)) |
| 213 |
#endif |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#if _COMPILER_GNUC(4,0) || __has_attribute(warn_unused_result) |
| 221 |
#define _MUSTCHECK __attribute__((warn_unused_result)) |
| 222 |
#else |
| 223 |
#define _MUSTCHECK |
| 224 |
#endif |
| 225 |
|
| 226 |
|
| 227 |
#if _COMPILER_GNUC(4,0) || __has_attribute(deprecated) |
| 228 |
#define _DEPRECATED __attribute__((deprecated)) |
| 229 |
#else |
| 230 |
#define _DEPRECATED |
| 231 |
#endif |
| 232 |
|
| 233 |
|
| 234 |
#if _COMPILER_GNUC(4,0) || __has_attribute(printf) |
| 235 |
#ifdef __MINGW32__ |
| 236 |
#define _PRINTF(fmtpos, argpos) __attribute__((format(__MINGW_PRINTF_FORMAT, fmtpos, argpos))) |
| 237 |
#else |
| 238 |
#define _PRINTF(fmtpos, argpos) __attribute__((format(printf, fmtpos, argpos))) |
| 239 |
#endif |
| 240 |
#else |
| 241 |
#define _PRINTF(fmtpos, argpos) |
| 242 |
#endif |
| 243 |
|
| 244 |
|
| 245 |
#if _COMPILER_GNUC(4,0) || __has_attribute(malloc) |
| 246 |
#define _MALLOC __attribute__((malloc)) |
| 247 |
#else |
| 248 |
#define _MALLOC |
| 249 |
#endif |
| 250 |
|
| 251 |
|
| 252 |
#if _COMPILER_GNUC(4,0) || __has_attribute(unused) |
| 253 |
#define _UNUSED __attribute__((unused)) |
| 254 |
#else |
| 255 |
#define _UNUSED |
| 256 |
#endif |
| 257 |
|
| 258 |
|
| 259 |
#if _COMPILER_GNUC(4,0) || __has_attribute(noinline) |
| 260 |
#define _NOINLINE __attribute__((noinline)) |
| 261 |
#else |
| 262 |
#define _NOINLINE |
| 263 |
#endif |
| 264 |
|
| 265 |
|
| 266 |
#if _COMPILER_GNUC(4,0) || __has_attribute(noreturn) |
| 267 |
#define _NORETURN __attribute__((noreturn)) |
| 268 |
#else |
| 269 |
#define _NORETURN |
| 270 |
#endif |
| 271 |
|
| 272 |
|
| 273 |
#if _COMPILER_GNUC(4,0) || __has_builtin(__builtin_expect) |
| 274 |
#define likely(x) __builtin_expect(!!(x), 1) |
| 275 |
#else |
| 276 |
#define likely(x) (x) |
| 277 |
#endif |
| 278 |
|
| 279 |
|
| 280 |
#if _COMPILER_GNUC(4,0) || __has_builtin(__builtin_expect) |
| 281 |
#define unlikely(x) __builtin_expect(!!(x), 0) |
| 282 |
#else |
| 283 |
#define unlikely(x) (x) |
| 284 |
#endif |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
#ifndef static_assert |
| 298 |
#if _COMPILER_GNUC(4,6) || _COMPILER_MSC(1600) || __has_feature(c_static_assert) |
| 299 |
|
| 300 |
#define static_assert(expr, msg) _Static_assert(expr, msg) |
| 301 |
#else |
| 302 |
|
| 303 |
#define static_assert(expr, msg) enum { CONCAT4(static_assert_failure_, __LINE__, _, __COUNTER__) = 1/(1 != (1 + (expr))) } |
| 304 |
#endif |
| 305 |
#endif |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
#ifndef Assert |
| 310 |
#ifdef CASSERT |
| 311 |
void log_fatal(const char *file, int line, const char *func, bool show_perror, void *ctx, const char *s, ...) _PRINTF(6, 7); |
| 312 |
#define Assert(e) \ |
| 313 |
do { \ |
| 314 |
if (unlikely(!(e))) { \ |
| 315 |
log_fatal(__FILE__, __LINE__, __func__, false, NULL, \ |
| 316 |
"Assert(%s) failed", #e); \ |
| 317 |
abort(); \ |
| 318 |
} \ |
| 319 |
} while (0) |
| 320 |
#else |
| 321 |
#define Assert(e) |
| 322 |
#endif |
| 323 |
#endif |
| 324 |
|
| 325 |
|
| 326 |
_MUSTCHECK |
| 327 |
static inline void *zmalloc(size_t len) |
| 328 |
{ |
| 329 |
return calloc(1, len); |
| 330 |
} |
| 331 |
|
| 332 |
#ifndef HAVE_POSIX_MEMALIGN |
| 333 |
#define posix_memalign(a,b,c) usual_memalign(a,b,c) |
| 334 |
|
| 335 |
int posix_memalign(void **ptr_p, size_t align, size_t len); |
| 336 |
#endif |
| 337 |
|
| 338 |
#ifndef HAVE_REALLOCARRAY |
| 339 |
#define reallocarray(a,b,c) usual_reallocarray(a,b,c) |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
void *reallocarray(void *p, size_t count, size_t size); |
| 345 |
|
| 346 |
#endif |
| 347 |
|
| 348 |
#endif |