Merge branch 'master' of git+ssh://repo.or.cz/srv/git/pachi
[pachi/derm.git] / util.h
blob6edbaa2ef69c8ad03c395f1e91277e865728e595
1 #ifndef ZZGO_UTIL_H
2 #define ZZGO_UTIL_H
4 #include <stdlib.h>
6 /* Misc. definitions. */
8 #define likely(x) __builtin_expect(!!(x), 1)
9 #define unlikely(x) __builtin_expect((x), 0)
11 static inline void *
12 checked_malloc(size_t size, char *filename, unsigned int line, const char *func)
14 void *p = malloc(size);
15 if (!p) {
16 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY malloc(%zu)\n",
17 filename, line, func, size);
18 exit(1);
20 return p;
23 static inline void *
24 checked_calloc(size_t nmemb, size_t size, char *filename, unsigned int line, const char *func)
26 void *p = calloc(nmemb, size);
27 if (!p) {
28 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY calloc(%zu, %zu)\n",
29 filename, line, func, nmemb, size);
30 exit(1);
32 return p;
35 #define malloc2(size) checked_malloc((size), __FILE__, __LINE__, __func__)
36 #define calloc2(nmemb, size) checked_calloc((nmemb), (size), __FILE__, __LINE__, __func__)
38 #endif