From c704a8a382c231e066a7fbf0402a33455c40b8f5 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 21 Apr 2025 21:51:54 -0500 Subject: Rework dlinkedlist functions to use VALLOC macro instead of only xalloc --- src/ll.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/ll.c') diff --git a/src/ll.c b/src/ll.c index 285203d..76a79f9 100644 --- a/src/ll.c +++ b/src/ll.c @@ -8,7 +8,10 @@ #include dlinkedlist * dlinkedlist_init(void) { - dlinkedlist *ll = xcalloc(1, sizeof(*ll)); + dlinkedlist *ll = VALLOC(1, sizeof(*ll)); + if(!ll) + return NULL; + ll->end = NULL; ll->start = NULL; ll->size = 0; @@ -50,6 +53,8 @@ int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb f // Need to handle adding the first element differently than other opers, so do it here dllnode *n = dllnode_init(data, fcb); + if(!n) + return -1; // dllnode_init already sets n.prev and n.next to null, so no need to do so again ll->end = n; @@ -64,10 +69,15 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char RETURNWERR(EINVAL, 1); if(op != 'a' && op != 'p') RETURNWERR(EINVAL, 1); - if(dlinkedlist_handlefirstnode(ll, data, fcb) == 0) + + int handleret; + if((handleret = dlinkedlist_handlefirstnode(ll, data, fcb)) == 0) return 0; dllnode *n = dllnode_init(data, fcb); + if(!n || handleret < 0) + return -1; + switch (op) { case 'a': n->prev = (ll->end); @@ -135,6 +145,8 @@ int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int i // If you're inserting at index 2, the new node becomes index 2, and the previous node at index 2 moves to index 3 dllnode *new = dllnode_init(data, fcb); + if(!new) + return -1; dllnode *current = dlinkedlist_getnode(ll, index); if(!current) XALLOC_EXIT(" somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); @@ -156,7 +168,7 @@ int dlinkedlist_remove(dlinkedlist * const ll, int index) { dllnode *current = dlinkedlist_getnode(ll, index); if(!current) - abort(); + return -1; if(index == 0) { ll->start = current->next; -- cgit v1.2.3