aboutsummaryrefslogtreecommitdiffstats
path: root/sandbox/utils.h
blob: 89198e088643fa133e21fd928373afcfb192e462 (plain)
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
#ifndef UTILS_H
#define UTILS_H

#define numberof(ary) (sizeof(ary) / sizeof(ary[0]))

// errors
static inline int verror(const char *prefix, const char *fmt, va_list ap)
{
	int pref_len = prefix ? strlen(prefix) : 0;
	int fmt_len = strlen(fmt);

	char buf[pref_len + fmt_len + 2];
	if (prefix)
		strcpy(buf, prefix);
	strcpy(buf + pref_len, fmt);
	buf[pref_len + fmt_len] = '\n';
	buf[pref_len + fmt_len + 1] = '\0';

	vfprintf(stderr, buf, ap);
	return -1;
}

static inline int error(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	verror(NULL, fmt, args);
	va_end(args);
	return -1;
}

static inline noreturn void die(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	verror(NULL, fmt, args);
	va_end(args);
	exit(EXIT_FAILURE);
}

static inline noreturn void bug(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	verror("[BUG] ", fmt, args);
	va_end(args);
	exit(EXIT_FAILURE);
}

// memory utils
static inline void *_mem_nonnull(void *val)
{
	if (!val)
		die("not enough memory");
	return val;
}

static inline void *xmalloc(size_t size)
{
	return _mem_nonnull(malloc(size));
}

static inline void *xcalloc(size_t nmemb, size_t size)
{
	return _mem_nonnull(calloc(nmemb, size));
}

static inline void *xrealloc(void *ptr, size_t size)
{
	void *new = realloc(ptr, size);

	if (!new)
		die("not enough memory");
	return new;
}

static inline char *xstrdup(const char *orig)
{
	return _mem_nonnull(strdup(orig));
}

static inline char *xformat(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	char *new = NULL;
	if (vasprintf(&new, fmt, args) == -1)
		_mem_nonnull(NULL);
	va_end(args);
	return new;
}

/*
 * list(type): A variable-length array implementation.
 */

#define list(type) struct { size_t len, capa; type *ptr; }

#define LIST_INIT { 0, 0, NULL }

#define list_grow(l) do { \
	(l)->capa = (l)->capa ? (l)->capa * 2 : 8; \
	(l)->ptr = xrealloc((l)->ptr, sizeof(*(l)->ptr) * (l)->capa); \
} while (0)

#define list_append(l, i) do { \
	if ((l)->capa <= (l)->len) \
		list_grow(l); \
	(l)->ptr[(l)->len++] = (i); \
} while (0)

#define list_fetch(l, i) ((l)->ptr[i])

#define list_for_each(l, it, in) \
	for (in = 0; in < (l)->len ? (it = list_fetch(l, in)) || 1 : 0; in++)

#define list_free0(l) do { \
	free((l)->ptr); \
	memset(l, 0, sizeof(*l)); \
} while (0)

#define list_free(l, f) do { \
	if (f) \
		for (size_t i = 0; i < (l)->len; i++) \
			f((l)->ptr[i]); \
	list_free0(l); \
} while (0)

/*
 * CHECK(), CHECK_MSG() and UNREACHABLE(): Run-time assertion macros
 */
static inline void IR(int x)
{
	/* Suppress -Wunused-result */
	(void)x;
}
#define CHECK_MSG(x, m) do {						\
	if (!(x)) {							\
		/* Hopefully no errors occur... */			\
		char _b[512] = { 0 };					\
		int _p = (int)syscall(__NR_getpid);			\
		IR(snprintf(_b, sizeof(_b), "[PID:%d] %s:%d (%s): %s\n",\
			 _p, __FILE__, __LINE__, __func__, m));		\
		IR(write(2, _b, strlen(_b)));				\
		abort();						\
	}								\
} while (0)
#define CHECK(x) CHECK_MSG(x, #x)
#define UNREACHABLE() CHECK_MSG(0, "unreachable")

#endif