aboutsummaryrefslogtreecommitdiff
path: root/util.h
blob: 56e36171f154eb2e1fd3322a46a7a5d081ccd11a (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
struct list {
	struct list *prev, *next;
};

struct array {
	void *val;
	size_t len, cap;
};

struct treenode {
	unsigned long long key;
	void *child[2];
	int height;
	bool new;  /* set by treeinsert if this node was newly allocated */
};

extern char *argv0;

#define LEN(a) (sizeof(a) / sizeof((a)[0]))
#define ALIGNDOWN(x, n) ((x) & -(n))
#define ALIGNUP(x, n) ALIGNDOWN((x) + (n) - 1, n)

void warn(const char *, ...);
void fatal(const char *fmt, ...);

void *reallocarray(void *, size_t, size_t);
void *xreallocarray(void *, size_t, size_t);
void *xmalloc(size_t);

char *progname(char *, char *);

void listinsert(struct list *, struct list *);
void listremove(struct list *);
#define listelement(list, type, member) (type *)((char *)list - offsetof(type, member))

void *arrayadd(struct array *, size_t);
void arrayaddptr(struct array *, void *);
void arrayaddbuf(struct array *, const void *, size_t);
void *arraylast(struct array *, size_t);
#define arrayforeach(a, m) for (m = (a)->val; m != (void *)((char *)(a)->val + (a)->len); ++m)

/* map */

struct map {
	size_t len, cap;
	struct mapkey *keys;
	void **vals;
};

struct mapkey {
	unsigned long hash;
	const void *str;
	size_t len;
};

void mapkey(struct mapkey *, const void *, size_t);
void mapinit(struct map *, size_t);
void mapfree(struct map *, void(void *));
void **mapput(struct map *, struct mapkey *);
void *mapget(struct map *, struct mapkey *);

/* tree */

void *treeinsert(void **, unsigned long long, size_t);