-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
204 lines (161 loc) · 5.17 KB
/
util.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef UTIL_H
#define UTIL_H
// TODO: seperate bool/noreturn and other version-dependent stuff into its own
// header
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __has_feature
#define HAS_FEATURE(name) __has_feature(name)
#else
#define HAS_FEATURE(name) 0
#endif
#if HAS_FEATURE(memory_sanitizer) || defined(MEMORY_SANITIZER) || \
defined(__SANITIZE_MEMORY__)
#define MSAN 1
#else
#define MSAN 0
#endif
#if HAS_FEATURE(address_sanitizer) || defined(ADDRESS_SANITIZER) || \
defined(__SANITIZE_ADDRESS__)
#define ASAN 1
#else
#define ASAN 0
#endif
#if HAS_FEATURE(hwaddress_sanitizer) || defined(HWADDRESS_SANITIZER) || \
defined(__SANITIZE_HWADDRESS__)
#define HWASAN 1
#else
#define HWASAN 0
#endif
#if HAS_FEATURE(thread_sanitizer) || defined(THREAD_SANITIZER) || \
defined(__SANITIZE_THREAD__)
#define TSAN 1
#else
#define TSAN 0
#endif
#if HAS_FEATURE(undefined_behavior_sanitizer) || \
defined(UNDEFINED_BEHAVIOR_SANITIZER)
#define UBSAN 1
#else
#define UBSAN 0
#endif
#if ASAN || MSAN || TSAN || UBSAN
#define SANITIZER_PRINT_STACK_TRACE
#include "sanitizer/common_interface_defs.h" // __sanitizer_print_stack_trace
#endif
#ifndef __STDC_VERSION__
#error "JCC Requires at least C99"
#endif
#define NORETURN
#if __STDC_VERSION__ >= 201112L
#undef NORETURN
#include <stdnoreturn.h>
#if __STDC_VERSION__ >= 202311L
#define NORETURN [[noreturn]]
#else
#define NORETURN noreturn
#endif
#endif
#include <string.h>
#define ROUND_UP(value, pow2) (((value) + ((pow2) - 1ull)) & ~((pow2) - 1ull))
#define UNUSED_ARG(arg) (void)(arg);
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ARR_LENGTH(a) (sizeof((a)) / sizeof((a)[0]))
static inline size_t num_digits(size_t num) {
return (num ? (size_t)log10(num) : 0) + 1;
}
static inline void debug_print_stack_trace() {
#ifdef SANITIZER_PRINT_STACK_TRACE
__sanitizer_print_stack_trace();
#endif
}
static inline unsigned long popcntl(unsigned long long l) {
#if defined(__has_builtin) && __has_builtin(__builtin_popcountll)
return __builtin_popcountll(l);
#else
todo("lzcnt not implemented outside of `__builtin_popcountll`");
#endif
}
static inline unsigned long tzcnt(unsigned long long l) {
#if defined(__has_builtin) && __has_builtin(__builtin_clzll)
return __builtin_ctzll(l);
#else
todo("lzcnt not implemented outside of `__builtin_clzll`");
#endif
}
static inline unsigned long lzcnt(unsigned long long l) {
#if defined(__has_builtin) && __has_builtin(__builtin_clzll)
return __builtin_clzll(l);
#else
todo("lzcnt not implemented outside of `__builtin_clzll`");
#endif
}
#define FMTPRINT(file, message, format) \
do { \
va_list v; \
va_start(v, format); \
fprintf(file, message); \
vfprintf(file, format, v); \
fprintf(file, "\n"); \
va_end(v); \
} while (0);
#define EXIT_FAIL(code) \
debug_print_stack_trace(); \
raise(SIGINT); \
exit(code);
NORETURN static inline void todo(const char *msg, ...) {
FMTPRINT(stderr, "`todo` hit, program exiting: ", msg);
EXIT_FAIL(-2);
}
NORETURN static inline void unreachable(const char *msg, ...) {
FMTPRINT(stderr, "`unreachable` hit, program exiting: ", msg);
EXIT_FAIL(-2);
}
NORETURN static inline void bug(const char *msg, ...) {
FMTPRINT(stderr, "`bug` hit, program exiting: ", msg);
EXIT_FAIL(-2);
}
// present in all mode, always causes program exit if fails
static inline void invariant_assert(bool b, const char *msg, ...) {
if (!b) {
FMTPRINT(stderr, "invariant_assertion failed, program exiting: ", msg);
EXIT_FAIL(-1);
}
}
static inline void breakpoint(void) { raise(SIGINT); }
#if NDEBUG
static inline void debug_assert(bool, const char *, ...) {}
#else
static inline void debug_assert(bool b, const char *msg, ...) {
if (!b) {
FMTPRINT(stderr, "debug_assertion failed, program exiting: ", msg);
EXIT_FAIL(-1);
}
}
#endif
static inline void *nonnull_malloc(size_t size) {
if (size == 0) {
return NULL;
}
void *ptr = malloc(size);
invariant_assert(ptr, "`malloc` returned NULL (out-of-memory likely)");
return ptr;
}
static inline void *nonnull_realloc(void *p, size_t size) {
void *ptr = realloc(p, size);
invariant_assert(ptr, "`realloc` returned NULL (out-of-memory likely)");
return ptr;
}
static inline char *malloc_strcpy(const char *s) {
int len = strlen(s);
char *cp = nonnull_malloc(len * sizeof(*s));
// use memcpy as we already have len
memcpy(cp, s, len * sizeof(*s));
return cp;
}
#endif