summaryrefslogtreecommitdiff
path: root/src/util.c
blob: 7b8120b914c72a61b592ca3cdfe3c8e464e649b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdlib.h>

#include <kalmia.h>
#include "util.h"

void * kai_next_array_element(void **array, size_t *count, size_t *max, size_t sz)
{
	void *array_new = NULL;
	if (*max == 0) {
		array_new = malloc(sz);
		*count = 1;
		*max = 1;
	}
	else if (*count == *max) {
		array_new = realloc(*array, 2*(*max)*sz);
		*count += 1;
		*max *= 2;
	}
	else {
		array_new = *array;
		*count += 1;
	}

	if (array_new == NULL)
		return NULL;
	
	*array = array_new;
	return array_new + ((*count-1) * sz);
}


ka_node_t * kai_next_node(kalmia_t *k)
{
	return kai_next_array_element(
		(void**) &(k->node), 
		&(k->n_nodes), &(k->n_nodes_max), 
		sizeof(ka_node_t)
	);
}