1 #ifndef DYNAMIC_ARRAY_H_
2 #define DYNAMIC_ARRAY_H_
4 void *darr_alloc(int elem, int szelem);
5 void darr_free(void *da);
6 void *darr_resize_impl(void *da, int elem);
7 #define darr_resize(da, elem) do { (da) = darr_resize_impl(da, elem); } while(0)
9 int darr_empty(void *da);
10 int darr_size(void *da);
12 void *darr_clear_impl(void *da);
13 #define darr_clear(da) do { (da) = darr_clear_impl(da); } while(0)
16 void *darr_push_impl(void *da, void *item);
17 #define darr_push(da, item) do { (da) = darr_push_impl(da, item); } while(0)
18 void *darr_pop_impl(void *da);
19 #define darr_pop(da) do { (da) = darr_pop_impl(da); } while(0)
21 /* Finalize the array. No more resizing is possible after this call.
22 * Use free() instead of dynarr_free() to deallocate a finalized array.
23 * Returns pointer to the finalized array.
26 void *darr_finalize(void *da);
28 /* utility macros to push characters to a string. assumes and maintains
29 * the invariant that the last element is always a zero
31 #define darr_strpush(da, c) \
33 char cnull = 0, ch = (char)(c); \
34 (da) = dynarr_pop_impl(da); \
35 (da) = dynarr_push_impl((da), &ch); \
36 (da) = dynarr_push_impl((da), &cnull); \
39 #define darr_strpop(da) \
42 (da) = dynarr_pop_impl(da); \
43 (da) = dynarr_pop_impl(da); \
44 (da) = dynarr_push_impl((da), &cnull); \
48 #endif /* DYNAMIC_ARRAY_H_ */