summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-11-27 20:03:27 -0600
committersanine <sanine.not@pm.me>2022-11-27 20:03:27 -0600
commit6ca18697f9eb332c38f0b46b823089ce80347580 (patch)
tree267d074f74ebfa54f1c90fd3361e6b2dbf793613 /src/util.c
parent21e8b8ad389dda55d92af4af1fd22aac657074b4 (diff)
add dynamic array functions
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..7b8120b
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,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)
+ );
+}