diff options
Diffstat (limited to 'src/ll.c')
| -rw-r--r-- | src/ll.c | 18 |
1 files changed, 15 insertions, 3 deletions
| @@ -8,7 +8,10 @@ | |||
| 8 | #include <error.h> | 8 | #include <error.h> |
| 9 | 9 | ||
| 10 | dlinkedlist * dlinkedlist_init(void) { | 10 | dlinkedlist * dlinkedlist_init(void) { |
| 11 | dlinkedlist *ll = xcalloc(1, sizeof(*ll)); | 11 | dlinkedlist *ll = VALLOC(1, sizeof(*ll)); |
| 12 | if(!ll) | ||
| 13 | return NULL; | ||
| 14 | |||
| 12 | ll->end = NULL; | 15 | ll->end = NULL; |
| 13 | ll->start = NULL; | 16 | ll->start = NULL; |
| 14 | ll->size = 0; | 17 | ll->size = 0; |
| @@ -50,6 +53,8 @@ int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb f | |||
| 50 | 53 | ||
| 51 | // Need to handle adding the first element differently than other opers, so do it here | 54 | // Need to handle adding the first element differently than other opers, so do it here |
| 52 | dllnode *n = dllnode_init(data, fcb); | 55 | dllnode *n = dllnode_init(data, fcb); |
| 56 | if(!n) | ||
| 57 | return -1; | ||
| 53 | // dllnode_init already sets n.prev and n.next to null, so no need to do so again | 58 | // dllnode_init already sets n.prev and n.next to null, so no need to do so again |
| 54 | 59 | ||
| 55 | ll->end = n; | 60 | ll->end = n; |
| @@ -64,10 +69,15 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char | |||
| 64 | RETURNWERR(EINVAL, 1); | 69 | RETURNWERR(EINVAL, 1); |
| 65 | if(op != 'a' && op != 'p') | 70 | if(op != 'a' && op != 'p') |
| 66 | RETURNWERR(EINVAL, 1); | 71 | RETURNWERR(EINVAL, 1); |
| 67 | if(dlinkedlist_handlefirstnode(ll, data, fcb) == 0) | 72 | |
| 73 | int handleret; | ||
| 74 | if((handleret = dlinkedlist_handlefirstnode(ll, data, fcb)) == 0) | ||
| 68 | return 0; | 75 | return 0; |
| 69 | 76 | ||
| 70 | dllnode *n = dllnode_init(data, fcb); | 77 | dllnode *n = dllnode_init(data, fcb); |
| 78 | if(!n || handleret < 0) | ||
| 79 | return -1; | ||
| 80 | |||
| 71 | switch (op) { | 81 | switch (op) { |
| 72 | case 'a': | 82 | case 'a': |
| 73 | n->prev = (ll->end); | 83 | n->prev = (ll->end); |
| @@ -135,6 +145,8 @@ int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int i | |||
| 135 | // If you're inserting at index 2, the new node becomes index 2, and the previous node at index 2 moves to index 3 | 145 | // If you're inserting at index 2, the new node becomes index 2, and the previous node at index 2 moves to index 3 |
| 136 | 146 | ||
| 137 | dllnode *new = dllnode_init(data, fcb); | 147 | dllnode *new = dllnode_init(data, fcb); |
| 148 | if(!new) | ||
| 149 | return -1; | ||
| 138 | dllnode *current = dlinkedlist_getnode(ll, index); | 150 | dllnode *current = dlinkedlist_getnode(ll, index); |
| 139 | if(!current) | 151 | if(!current) |
| 140 | XALLOC_EXIT("<dlinkedlist_insert> somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); | 152 | XALLOC_EXIT("<dlinkedlist_insert> somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); |
| @@ -156,7 +168,7 @@ int dlinkedlist_remove(dlinkedlist * const ll, int index) { | |||
| 156 | 168 | ||
| 157 | dllnode *current = dlinkedlist_getnode(ll, index); | 169 | dllnode *current = dlinkedlist_getnode(ll, index); |
| 158 | if(!current) | 170 | if(!current) |
| 159 | abort(); | 171 | return -1; |
| 160 | 172 | ||
| 161 | if(index == 0) { | 173 | if(index == 0) { |
| 162 | ll->start = current->next; | 174 | ll->start = current->next; |
