summaryrefslogtreecommitdiff
path: root/macros.h
blob: eb514b5293a5cb3ae73a2b927a12aca3c3e653e1 (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
#ifndef macros_h
#define macros_h

#undef assert
#define assert(expr)		if (!(expr)) { *(int *)0 = 0; }

#define array_count(array)	(sizeof(array)/sizeof(*(array)))

#undef offsetof
#define offsetof(type, element)	((u64)(&(((type *)0)->element)))

#define min(a, b)	(((a) < (b)) ? (a) : (b))
#define max(a, b)	(((a) > (b)) ? (a) : (b))

#define clamp(a, x, b) \
	(((x) < (a)) ? (a) : \
	(((x) > (b)) ? (b) : (x)))

#define swap(type, a, b)	{ type tmp = (a); (a) = (b); (b) = tmp; }

#define kilobytes(n) n*1024
#define megabytes(n) kilobytes(n)*1024
#define gigabytes(n) megabytes(n)*1024

/* NOTE(pryazha): Singly linked list */
#define sllpush(first, last, node) \
	((first) == 0 ? \
	((first) = (last) = (node), (node)->next = 0) : \
	((last)->next = (node), (last) = (node), (node)->next = 0))
#define sllpop(first, last) \
	((first) == (last) ? \
	((first) = (last) = 0) : \
	((first) = (first)->next))

/* NOTE(pryazha): Doubly linked list */
#define dllpush(first, last, node, next, prev) \
	((first) == 0 ? \
	((first) = (last) = (node), (node)->next = (node)->prev = 0) : \
	((node)->prev = (last), (last)->next = (node), (last) = (node), (node)->next = 0))
#define dllpushback(first, last, node)	dllpush(first, last, node, next, prev)
#define dllpushfront(first, last, node)	dllpush(last, first, node, prev, next)

#define dllremove(first, last, node) \
	((first) == (node) ? \
	((first) == (last) ? (first) = (last) = 0 : \
	((first) = (first)->next, (first)->prev = 0)) : \
	((last) == (node) ? ((last) = (last)->prev, (last)->next = 0) : \
	((node)->next->prev = (node)->prev, (node)->prev->next = (node)->next)))

#endif