From 30efa292790dfdc9eedefa97a9f82e1cac2aeaa0 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 24 Oct 2025 12:44:06 -0500 Subject: add alloc/dealloc functions for generic linked list --- lichen.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lichen.c') diff --git a/lichen.c b/lichen.c index f20c1b3..e8c8e19 100644 --- a/lichen.c +++ b/lichen.c @@ -1,4 +1,6 @@ #include "lichen.h" +#include +#include void li_xorshift32_seed(struct li_xorshift32_t *xor, uint32_t seed) { @@ -18,3 +20,35 @@ uint32_t li_xorshift32(void *s) { // return just top 32 bits return x >> 32; } + + + +li_llnode_t * li_alloc_llnode() { + li_llnode_t *n = malloc(sizeof(li_llnode_t)); + if (n == NULL) { + fprintf(stderr, "failed to allocate linked-list node\n"); + return NULL; + } else { + n->tag = 0; + n->data = 0; + n->next = NULL; + return n; + } +} + + +li_llnode_t * li_copy_llnode(li_llnode_t *n) { + li_llnode_t *m = li_alloc_llnode(); + if (m == NULL) { + return NULL; + } + memcpy(m, n, sizeof(li_llnode_t)); + return m; +} + +void li_free_llnode(li_llnode_t *n, void (*free_data)(void*)) { + if (n != NULL) { + free_data(n->data); + } + free(n); +} -- cgit v1.2.1