BOARD_TRAIT_SAFE: Introduce, making btraits.safe optional (currently unused, expensive)
[pachi/t.git] / util.h
blob297a0f93d1cb2d76102da8aaea0bae5fc0880d82
1 #ifndef PACHI_UTIL_H
2 #define PACHI_UTIL_H
4 #include <stdlib.h>
6 /* Misc. definitions. */
8 /* Use make DOUBLE=1 in large configurations with counts > 1M
9 * where 24 bits of floating_t mantissa become insufficient. */
10 #ifdef DOUBLE
11 # define floating_t double
12 # define PRIfloating "%lf"
13 #else
14 # define floating_t float
15 # define PRIfloating "%f"
16 #endif
18 #define likely(x) __builtin_expect(!!(x), 1)
19 #define unlikely(x) __builtin_expect((x), 0)
21 static inline void *
22 checked_malloc(size_t size, char *filename, unsigned int line, const char *func)
24 void *p = malloc(size);
25 if (!p) {
26 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY malloc(%zu)\n",
27 filename, line, func, size);
28 exit(1);
30 return p;
33 static inline void *
34 checked_calloc(size_t nmemb, size_t size, char *filename, unsigned int line, const char *func)
36 void *p = calloc(nmemb, size);
37 if (!p) {
38 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY calloc(%zu, %zu)\n",
39 filename, line, func, nmemb, size);
40 exit(1);
42 return p;
45 #define malloc2(size) checked_malloc((size), __FILE__, __LINE__, __func__)
46 #define calloc2(nmemb, size) checked_calloc((nmemb), (size), __FILE__, __LINE__, __func__)
48 #endif