2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023 John Tsiombikas <nuclear@mutantstargoat.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
18 #ifndef DYNAMIC_ARRAY_H_
19 #define DYNAMIC_ARRAY_H_
21 void *darr_alloc(int elem, int szelem);
22 void darr_free(void *da);
23 void *darr_resize_impl(void *da, int elem);
24 #define darr_resize(da, elem) do { (da) = darr_resize_impl(da, elem); } while(0)
26 int darr_empty(void *da);
27 int darr_size(void *da);
29 void *darr_clear_impl(void *da);
30 #define darr_clear(da) do { (da) = darr_clear_impl(da); } while(0)
33 void *darr_push_impl(void *da, void *item);
34 #define darr_push(da, item) do { (da) = darr_push_impl(da, item); } while(0)
35 void *darr_pop_impl(void *da);
36 #define darr_pop(da) do { (da) = darr_pop_impl(da); } while(0)
38 /* Finalize the array. No more resizing is possible after this call.
39 * Use free() instead of dynarr_free() to deallocate a finalized array.
40 * Returns pointer to the finalized array.
43 void *darr_finalize(void *da);
45 /* utility macros to push characters to a string. assumes and maintains
46 * the invariant that the last element is always a zero
48 #define darr_strpush(da, c) \
50 char cnull = 0, ch = (char)(c); \
51 (da) = dynarr_pop_impl(da); \
52 (da) = dynarr_push_impl((da), &ch); \
53 (da) = dynarr_push_impl((da), &cnull); \
56 #define darr_strpop(da) \
59 (da) = dynarr_pop_impl(da); \
60 (da) = dynarr_pop_impl(da); \
61 (da) = dynarr_push_impl((da), &cnull); \
65 #endif /* DYNAMIC_ARRAY_H_ */