aboutsummaryrefslogtreecommitdiff
path: root/util/util.h
blob: ef378ca352d82f2f7f7da116850f30fa165e9536 (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
#ifndef UTIL_H
#define UTIL_H

#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdbool.h>

#if defined(__GNUC__) || defined(__clang__)
# define NORETURN __attribute__((__noreturn__))
#else
# define NORETURN
#endif

#if defined(__GNUC__)
# define UNUSED __attribute__((__unused__))
#else
# define UNUSED
#endif

#define N_ENTRIES(x) (sizeof(x) / sizeof(*x))
#define member_size(type, member) sizeof(((type *)(0))->member)
#define TYPEOF(x) __typeof__(x)

#define PACKED __attribute__((__packed__))

#if defined(__GNUC__)
# define FALLTHROUGH __attribute__ ((fallthrough))
#else
# define FALLTHROUGH
#endif

NORETURN void die(const char *format, ...) __attribute__((format (printf, 1, 2)));
NORETURN void die_errno(const char *format, ...) __attribute__((format (printf, 1, 2)));

void err(const char * format, ...) __attribute__((format (printf, 1, 2)));
void err_errno(const char * format, ...) __attribute__((format (printf, 1, 2)));

#define XREALLOC_ARRAY(x, alloc) ((x) = xrealloc((x), (alloc) * sizeof(*(x))))
#define ALLOC_ARRAY(x, n) (malloc((n) * sizeof(*x)))

#define ALIGN_ADDR_MASK(addr, mask) \
	((addr + ((mask) - 1)) & ~((mask) - 1))

#define ALIGN_ADDR_16(addr) ALIGN_ADDR_MASK(addr, 0x10)
#define ALIGN_ADDR_32(addr) ALIGN_ADDR_MASK(addr, 0x20)
#define ALIGN_ADDR_64(addr) ALIGN_ADDR_MASK(addr, 0x40)
#define ALIGN_ADDR(addr, bytes) ALIGN_ADDR_MASK(addr, (8ULL*(bytes)))

// Scaling: 0, 24, 66, 140, 269, 495, 890
#define ALLOC_NC(x) (((x)+14)*7/4)

#define AM_RESIZE_ALLOC(buf, sz, cap, item_sz, alloc_nc) \
	do {                                                  \
		TYPEOF(sz) _sz = (sz);                             \
		if((cap) < _sz) {                                  \
			void * nb;                                      \
			TYPEOF(cap) nc;                                 \
			if(_sz < alloc_nc((cap))) {                     \
				nc = alloc_nc((cap));                        \
			} else {                                        \
				nc = _sz;                                    \
			}                                               \
			nb = realloc((buf), (item_sz) * nc);            \
			if(nb) {                                        \
				(cap) = nc;                                  \
				(buf) = nb;                                  \
			} else {                                        \
				free((buf));                                 \
				(buf) = NULL;                                \
			}                                               \
		}                                                  \
	} while(0)

#define AM_RESIZE(buf, sz, cap) \
	AM_RESIZE_ALLOC(buf, sz, cap, sizeof(*buf), ALLOC_NC)

#endif