diff options
author | sanine <sanine.not@pm.me> | 2023-01-03 23:31:48 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-01-03 23:31:48 -0600 |
commit | 13a7c902c051fa8da1e476687c17bb5431d258e1 (patch) | |
tree | ec75993aba0b8264f0dffd03eee2191020b206be /src/util/util.c | |
parent | e68e6d4e433fe42a0c6df18b2f2d7990b91b7cd6 (diff) |
add kai_expand_array
Diffstat (limited to 'src/util/util.c')
-rw-r--r-- | src/util/util.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/util/util.c b/src/util/util.c new file mode 100644 index 0000000..c77b3f4 --- /dev/null +++ b/src/util/util.c @@ -0,0 +1,23 @@ +#include <kalmia.h> +#include <stdint.h> +#include <stdio.h> + + +void *kai_expand_array(void **array, size_t *count, size_t element_size) +{ + size_t new_count = *count + 1; + size_t new_size = element_size * new_count; + /* cast to uint8_t so we can do pointer arithmetic */ + uint8_t *new_array = realloc(*array, new_size); + if (new_array == NULL) { + fprintf(stderr, "[kalmia] ERROR: failed to add %ul bytes to %ul-byte array!\n", + element_size, element_size * *count); + return NULL; + } + + void *new_element = new_array + (*count * element_size); + + *array = new_array; + *count = new_count; + return new_element; +} |