summaryrefslogtreecommitdiff
path: root/src/util/util.c
blob: c77b3f4bdaf807fffe15f8e80e5b1452c69f716c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
}