diff options
Diffstat (limited to 'libq/xarray.c')
-rw-r--r-- | libq/xarray.c | 42 |
1 files changed, 14 insertions, 28 deletions
diff --git a/libq/xarray.c b/libq/xarray.c index 0ab1c5ae..b8cbec95 100644 --- a/libq/xarray.c +++ b/libq/xarray.c @@ -7,29 +7,15 @@ * Copyright 2018- Fabian Groffen - <grobian@gentoo.org> */ -typedef struct { - void **eles; - size_t num; - size_t len; -} array_t; +#include <stdlib.h> +#include <string.h> + +#include "xmalloc.h" +#include "xarray.h" -#define xrealloc_array(ptr, size, ele_size) xrealloc(ptr, (size) * (ele_size)) -/* The assignment after the check is unfortunate as we do a non-NULL check (we - * already do not permit pushing of NULL pointers), but we can't put it in the - * increment phase as that will cause a load beyond the bounds of valid memory. - */ -/* TODO: remove ele = NULL after checking all consumers don't rely on this */ -#define array_for_each(arr, n, ele) \ - for (n = 0, ele = NULL; n < array_cnt(arr) && (ele = arr->eles[n]); n++) -#define array_for_each_rev(arr, n, ele) \ - for (n = array_cnt(arr); n-- > 0 && (ele = arr->eles[n]); /*nothing*/) -#define array_get_elem(arr, n) (arr->eles[n]) -#define array_init_decl { .eles = NULL, .num = 0, } -#define array_cnt(arr) (arr)->num -#define DECLARE_ARRAY(arr) array_t _##arr = array_init_decl, *arr = &_##arr #define ARRAY_INC_SIZE 32 -static void *xarrayget(array_t *arr, size_t idx) +void *xarrayget(array_t *arr, size_t idx) { if (idx >= arr->num) return NULL; @@ -40,7 +26,7 @@ static void *xarrayget(array_t *arr, size_t idx) * mix xarraypush_ptr usage with the other push funcs which duplicate memory. * The free stage won't know which pointers to release directly. */ -static void *xarraypush_ptr(array_t *arr, void *ele) +void *xarraypush_ptr(array_t *arr, void *ele) { size_t n = arr->num++; if (arr->num > arr->len) { @@ -50,14 +36,13 @@ static void *xarraypush_ptr(array_t *arr, void *ele) arr->eles[n] = ele; return ele; } -static void *xarraypush(array_t *arr, const void *ele, size_t ele_len) + +void *xarraypush(array_t *arr, const void *ele, size_t ele_len) { return xarraypush_ptr(arr, xmemdup(ele, ele_len)); } -#define xarraypush_str(arr, ele) xarraypush(arr, ele, strlen(ele) + 1 /*NUL*/) -#define xarraypush_struct(arr, ele) xarraypush(arr, ele, sizeof(*(ele))) -static void xarraydelete_ptr(array_t *arr, size_t elem) +void xarraydelete_ptr(array_t *arr, size_t elem) { arr->num--; if (elem < arr->num) @@ -66,7 +51,7 @@ static void xarraydelete_ptr(array_t *arr, size_t elem) arr->eles[arr->num] = NULL; } -static void xarraydelete(array_t *arr, size_t elem) +void xarraydelete(array_t *arr, size_t elem) { free(arr->eles[elem]); xarraydelete_ptr(arr, elem); @@ -75,13 +60,14 @@ static void xarraydelete(array_t *arr, size_t elem) /* Useful for people who call xarraypush_ptr as it does not free any of the * pointers in the eles list. */ -static void xarrayfree_int(array_t *arr) +void xarrayfree_int(array_t *arr) { array_t blank = array_init_decl; free(arr->eles); *arr = blank; } -static void xarrayfree(array_t *arr) + +void xarrayfree(array_t *arr) { size_t n; for (n = 0; n < arr->num; ++n) |