UCT Plugins: Act dummy on WIN32
[pachi/t.git] / util.h
blobb815a7de201c0392d641d7c0e02563a9da6dbe4b
1 #ifndef PACHI_UTIL_H
2 #define PACHI_UTIL_H
4 #include <stdlib.h>
6 /* Portability definitions. */
8 #ifdef _WIN32
9 #include <windows.h>
11 #define sleep(seconds) Sleep((seconds) * 1000)
12 #define __sync_fetch_and_add(ap, b) InterlockedExchangeAdd((unsigned long *) (ap), (b));
13 #define __sync_fetch_and_sub(ap, b) InterlockedExchangeAdd((unsigned long *) (ap), -(b));
14 #endif
16 /* Misc. definitions. */
18 /* Use make DOUBLE=1 in large configurations with counts > 1M
19 * where 24 bits of floating_t mantissa become insufficient. */
20 #ifdef DOUBLE
21 # define floating_t double
22 # define PRIfloating "%lf"
23 #else
24 # define floating_t float
25 # define PRIfloating "%f"
26 #endif
28 #define likely(x) __builtin_expect(!!(x), 1)
29 #define unlikely(x) __builtin_expect((x), 0)
31 static inline void *
32 checked_malloc(size_t size, char *filename, unsigned int line, const char *func)
34 void *p = malloc(size);
35 if (!p) {
36 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY malloc(%zu)\n",
37 filename, line, func, size);
38 exit(1);
40 return p;
43 static inline void *
44 checked_calloc(size_t nmemb, size_t size, char *filename, unsigned int line, const char *func)
46 void *p = calloc(nmemb, size);
47 if (!p) {
48 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY calloc(%zu, %zu)\n",
49 filename, line, func, nmemb, size);
50 exit(1);
52 return p;
55 #define malloc2(size) checked_malloc((size), __FILE__, __LINE__, __func__)
56 #define calloc2(nmemb, size) checked_calloc((nmemb), (size), __FILE__, __LINE__, __func__)
58 #endif