/home/travis/build/MoarVM/MoarVM/src/core/vector.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* An as-small-as-possible dynamic array implementation. */ |
2 | | #define MVM_VECTOR_DECL(type, x) type *x; \ |
3 | | size_t x ## _num; \ |
4 | | size_t x ## _alloc |
5 | | |
6 | | |
7 | 3.90M | #define MVM_VECTOR_INIT(x, size) do { \ |
8 | 3.90M | size_t _s = (size); \ |
9 | 3.47M | x = (_s > 0) ? MVM_calloc(_s, sizeof(*x)) : NULL; \ |
10 | 3.90M | x ## _num = 0; \ |
11 | 3.90M | x ## _alloc = _s; \ |
12 | 3.90M | } while (0) |
13 | | |
14 | 10.7k | #define MVM_VECTOR_DESTROY(x) do { \ |
15 | 10.7k | MVM_free(x); \ |
16 | 10.7k | x = NULL; \ |
17 | 10.7k | x ## _num = 0; \ |
18 | 10.7k | x ## _alloc = 0; \ |
19 | 10.7k | } while (0) |
20 | | |
21 | | #define MVM_VECTOR_SIZE(x) \ |
22 | | (sizeof(*x) * (x ## _alloc)) |
23 | | |
24 | | #define MVM_VECTOR_TOP(x) \ |
25 | 3.77M | ((x) + (x ## _num)) |
26 | | |
27 | | |
28 | 768k | #define MVM_VECTOR_GROW(x, size) do {\ |
29 | 768k | size_t _s = (size); \ |
30 | 768k | x = MVM_realloc(x, _s*sizeof(*x)); \ |
31 | 768k | memset(x + (x ## _alloc), 0, (_s - (x ## _alloc)) * sizeof(*x)); \ |
32 | 768k | x ## _alloc = _s; \ |
33 | 768k | } while (0) |
34 | | |
35 | | |
36 | 25.5M | #define MVM_VECTOR_ENSURE_SIZE(x, size) do {\ |
37 | 25.5M | size_t _s = (size); \ |
38 | 25.5M | if (_s >= (x ## _alloc)) { \ |
39 | 768k | size_t newsize = (x ## _alloc) * 2 + 2; \ |
40 | 768k | while (_s >= newsize) newsize *= 2; \ |
41 | 768k | MVM_VECTOR_GROW(x, newsize); \ |
42 | 768k | } \ |
43 | 25.5M | } while (0) |
44 | | |
45 | | #define MVM_VECTOR_ENSURE_SPACE(x, space) \ |
46 | 19.1M | MVM_VECTOR_ENSURE_SIZE(x, (x ## _num) + (space)) |
47 | | |
48 | 13.2M | #define MVM_VECTOR_PUSH(x, value) do { \ |
49 | 13.2M | MVM_VECTOR_ENSURE_SPACE(x, 1); \ |
50 | 13.2M | x[x ## _num++] = (value); \ |
51 | 13.2M | } while(0) |
52 | | |
53 | | #define MVM_VECTOR_POP(x) \ |
54 | | (x)[--(x ## _num)] |
55 | | |
56 | | |
57 | 3.77M | #define MVM_VECTOR_APPEND(x, ar, len) do { \ |
58 | 3.77M | size_t _l = (len); \ |
59 | 3.77M | MVM_VECTOR_ENSURE_SPACE(x, _l); \ |
60 | 3.77M | memcpy(MVM_VECTOR_TOP(x), ar, _l * sizeof(x[0])); \ |
61 | 3.77M | x ## _num += _l; \ |
62 | 3.77M | } while(0) |
63 | | |
64 | | #define MVM_VECTOR_SPLICE(x, ofs, len, out) do { \ |
65 | | size_t _l = (len), _o = (ofs); \ |
66 | | void * buf = (out); \ |
67 | | if (buf != NULL) { memcpy(buf, (x) + _o, _l * sizeof(x[0])); } \ |
68 | | memmove((x) + _o, (x) + _o + _l, ((x ## _num) - _l - _o) * sizeof(x[0])); \ |
69 | | x ## _num -= _l; \ |
70 | | } while (0) |
71 | | |
72 | | #define MVM_VECTOR_ASSIGN(a, b) do { \ |
73 | | a = b; \ |
74 | | a ## _alloc = b ## _alloc; \ |
75 | | a ## _num = b ## _num; \ |
76 | | } while (0) |