forked from nsf/bmpanel2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
139 lines (121 loc) · 3.63 KB
/
array.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
#pragma once
#include "util.h"
/*
XXX: evil macros, but it shows the arrays concept, so.. leaving it here
#define DECLARE_ARRAY(type, name) \
type *name; \
size_t name##_n; \
size_t name##_alloc
*/
/* Init functions, at least one of them should be called on uninitialized
* array. */
#define INIT_EMPTY_ARRAY(array) \
do { \
array = 0; \
array##_n = 0; \
array##_alloc = 0; \
} while (0)
#define INIT_ARRAY(array, size) \
do { \
array = xmalloc(size * sizeof(array[0])); \
array##_n = 0; \
array##_alloc = size; \
} while (0)
/* ----------------------------------------------------------------------- */
#define ALLOC_NR(x) (((x) + 16) * 3 / 2)
/* this one can be omitted in non-debug version */
#ifdef NDEBUG
#define CHECK_ARRAY_BOUNDS(array, index) ((void)0);
#else
#define CHECK_ARRAY_BOUNDS(array, index) \
if (index >= array##_n) { \
XWARNING("Array: bounds were broken: (r: %u, n: %u)", \
index, array##_n); \
break; \
}
#endif
/* ----------------------------------------------------------------------- */
#define ENSURE_ARRAY_CAPACITY(array, capacity) \
do { \
if (capacity > array##_alloc) { \
size_t newsize = ALLOC_NR(array##_alloc); \
if (newsize < capacity) \
newsize = capacity; \
\
void *newmem = xmalloc(newsize * sizeof(array[0])); \
if (array##_n) { \
memcpy(newmem, array, array##_n * sizeof(array[0])); \
} \
if (array) \
xfree(array); \
array = newmem; \
array##_alloc = newsize; \
} \
} while (0)
#define FREE_ARRAY(array) \
do { \
if (array) \
xfree(array); \
array = 0; \
array##_n = 0; \
array##_alloc = 0; \
} while (0)
#define CLEAR_ARRAY(array) \
do { \
array##_n = 0; \
} while (0)
#define ARRAY_INSERT_AFTER(array, index, elt) \
do { \
CHECK_ARRAY_BOUNDS(array, index) \
ENSURE_ARRAY_CAPACITY(array, array##_n + 1); \
if (index == array##_n - 1) { \
array[array##_n++] = elt; \
} else { \
memmove(&array[index+2], &array[index+1], \
(array##_n-index-1) * sizeof(array[0])); \
array[index+1] = elt; \
array##_n++; \
} \
} while (0)
#define ARRAY_INSERT_BEFORE(array, index, elt) \
do { \
CHECK_ARRAY_BOUNDS(array, index) \
ENSURE_ARRAY_CAPACITY(array, array##_n + 1); \
memmove(&array[index+1], &array[index], \
(array##_n-index) * sizeof(array[0])); \
array[index] = elt; \
array##_n++; \
} while (0)
#define ARRAY_APPEND(array, elt) \
do { \
ENSURE_ARRAY_CAPACITY(array, array##_n + 1); \
array[array##_n++] = elt; \
} while (0)
#define ARRAY_PREPEND(array, elt) \
do { \
ENSURE_ARRAY_CAPACITY(array, array##_n + 1); \
memmove(array+1, array, array##_n * sizeof(array[0])); \
array[0] = elt; \
array##_n++; \
} while (0)
#define ARRAY_REMOVE(array, index) \
do { \
CHECK_ARRAY_BOUNDS(array, index) \
memmove(&array[index], &array[index+1], \
(array##_n-index-1) * sizeof(array[0])); \
array##_n--; \
} while (0)
#define SHRINK_ARRAY(array) \
do { \
if (array##_n == 0) { \
xfree(array); \
array = 0; \
array##_alloc = 0; \
} else if (array##_n < array##_alloc) { \
void *newmem = xmalloc(array##_n * sizeof(array[0])); \
memcpy(newmem, array, array##_n * sizeof(array[0])); \
xfree(array); \
array = newmem; \
array##_alloc = array##_n; \
} \
} while (0)