diff options
Diffstat (limited to 'arr.c')
-rw-r--r-- | arr.c | 22 |
1 files changed, 20 insertions, 2 deletions
@@ -16,10 +16,20 @@ static inline void arr_reserve(Array *arr, size_t n) { arr->data = err_realloc(arr->data, arr->item_sz * arr->cap); } +static inline void arr_reservea(Array *arr, size_t n, Allocator *a) { + arr->cap = n; + arr->data = allocr_realloc(a, arr->data, n); +} + /* like arr_reserve, but sets the length of the array too */ static inline void arr_set_len(Array *arr, size_t n) { - arr->len = arr->cap = n; - arr->data = err_realloc(arr->data, arr->item_sz * arr->cap); + arr_reserve(arr, n); + arr->len = n; +} + +static inline void arr_set_lena(Array *arr, size_t n, Allocator *a) { + arr_reservea(arr, n, a); + arr->len = n; } static inline void *arr_last(Array *arr) { @@ -37,6 +47,14 @@ static void *arr_add(Array *arr) { return (void*)((char*)arr->data + arr->item_sz * (arr->len - 1)); } +static void *arr_adda(Array *arr, Allocator *a) { + if (arr->len >= arr->cap) { + arr_reservea(arr, (arr->cap + 1) * 2, a); + } + arr->len++; + return (void*)((char*)arr->data + arr->item_sz * (arr->len - 1)); +} + static void arr_clear(Array *arr) { free(arr->data); arr->len = arr->cap = 0; |