-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.h
63 lines (53 loc) · 1.44 KB
/
macros.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// macros.h
// PegasusEar
//
// Created by Kevin Colley on 11/1/2020.
//
#ifndef PEG_MACROS_H
#define PEG_MACROS_H
#include <assert.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#ifndef UNIQUIFY
#define UNIQUIFY(macro, args...) UNIQUIFY_(macro, __COUNTER__, ##args)
#define UNIQUIFY_(macro, id, args...) UNIQUIFY__(macro, id, ##args)
#define UNIQUIFY__(macro, id, args...) macro(id, ##args)
#endif
#define STRINGIFY(arg) STRINGIFY_(arg)
#define STRINGIFY_(arg) #arg
/* On release builds, ASSERT(cond) tells the compiler that cond must be true */
#if NDEBUG
#define ASSERT(expr) do { \
if(!(expr)) { \
__builtin_unreachable(); \
} \
} while(0)
#else /* NDEBUG */
#define ASSERT(expr) assert(expr)
#endif /* NDEBUG */
#define CMP(op, a, b) ({ \
__typeof__((a)+(b)) _a = (a); \
__typeof__(_a) _b = (b); \
(_a op _b) ? _a : _b; \
})
#define MIN(a, b) CMP(<, a, b)
#define MAX(a, b) CMP(>, a, b)
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
/*! Frees the pointer pointed to by the parameter and then sets it to NULL */
#define destroy(pptr) do { \
__typeof__(*(pptr)) volatile* _pptr = (pptr); \
if(!_pptr) { \
break; \
} \
free(*_pptr); \
*_pptr = NULL; \
} while(0)
#define fail() fail_(__LINE__)
static inline void fail_(int line) {
printf("Uh oh, something went wrong. Contact the admin for help and provide this failure code: %d.%d\n", line, errno);
exit(EXIT_FAILURE);
}
#endif /* PEG_MACROS_H */