-
Notifications
You must be signed in to change notification settings - Fork 0
/
survival.h
112 lines (90 loc) · 4 KB
/
survival.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef _SURVIVAL_H_
#define _SURVIVAL_H_
/// \defgroup basic functions
/// \{
/// Minimum of a and b (must be comparable).
#define MIN(a, b) ((a) > (b)) ? (b) : (a)
/// Maximum of a and b (must be comparable).
#define MAX(a, b) ((a) < (b)) ? (b) : (a)
/// Absolute value of x
#define ABS(x) (((x) < 0) ? -(x) : (x))
/// Returns a non-zero value if x is a power of two.
#define is_power_of_two(x) ((x) && !((x) & ((x)-1)))
/// Retrieve the number of items in a array.
#define COUNT_OF(x) \
((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x])))))
/// \}
/// \defgroup minimal logging
/// \{
/// Supported log-levels, you can easily add a new one if needed.
enum
{
SURVIVAL_LOG_DEBUG,
SURVIVAL_LOG_ERROR
};
#ifndef SURVIVAL_LOG_BASENAME_ONLY
/// If non-zero, only the basename of source files will be included (more
/// readable logs).
#define SURVIVAL_LOG_BASENAME_ONLY 1
#endif
#ifdef SURVIVAL_DEBUG
#define EPRINTF(...) \
do { \
survival_log( \
SURVIVAL_LOG_ERROR, __FILE__, __func__, __LINE__, __VA_ARGS__); \
} while (0)
#define DPRINTF(...) \
do { \
survival_log( \
SURVIVAL_LOG_DEBUG, __FILE__, __func__, __LINE__, __VA_ARGS__); \
} while (0)
#else
// When SURVIVAL_DEBUG is false, we keep only EPRINT with a minimal format (no
// file and function).
#define EPRINTF(...) \
do { \
survival_log(SURVIVAL_LOG_ERROR, "", "", __LINE__, __VA_ARGS__); \
} while (0)
#define DPRINTF(fmt, args...)
#endif
/// Callbacks invoked by logging macros (EPRINTF, DPRINTF).
int
survival_log(
long level,
const char* file,
const char* func,
long line,
const char* fmt,
...) __attribute__((format(printf, 5, 6)));
/// \}
#if defined(__GNUC__) || defined(__clang__)
/// Indicate to the compiler that a given predicate is
/// very likely to be true.
#define LIKELY(x) __builtin_expect((x), 1)
/// Indicate to the compiler that a given predicate is
/// very likely to be false.
#define UNLIKELY(x) __builtin_expect((x), 0)
/// Specify alignment of structures or variables.
#define ALIGNED(x) __attribute__((aligned(x)))
#define ASSUME(cond) \
do { \
if (!(cond)) \
__builtin_unreachable(); \
} while (0)
/// Define a scope cleanup function.
#define SURVIVAL_CLEANUP(func) __attribute__((cleanup(func)))
#else
#error "Only clang or GCC are supported currently."
#endif // __clang__, __GNUC__
#define DEFINE_TRIVIAL_CLEANUP_FUNC(name, type, func) \
static void name(type* p) \
{ \
if (*p) { \
func(*p); \
} \
} \
struct __useless_struct_to_allow_trailing_semicolon__
#define DEFINE_CLEANUP_FREE() \
static void cleanup_free(void* p) { free(*(void**)p); } \
struct __useless_struct_to_allow_trailing_semicolon__
#endif