diff options
| author | @syxhe <https://t.me/syxhe> | 2025-04-15 18:02:06 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-04-15 18:02:06 -0500 |
| commit | 54b82706a01049e4ed5e4d417f50f642c701105b (patch) | |
| tree | e0749b503a5cf5351fd46db1990bdd266f02c22d /src/ll.c | |
| parent | fce48bb669691b6d3690b032f7a88e22c6f5614a (diff) | |
Finish prototype scanning code
Diffstat (limited to 'src/ll.c')
| -rw-r--r-- | src/ll.c | 68 |
1 files changed, 36 insertions, 32 deletions
| @@ -1,6 +1,7 @@ | |||
| 1 | #include "ll.h" | 1 | #include "ll.h" |
| 2 | #include "shared.h" | 2 | #include "shared.h" |
| 3 | 3 | ||
| 4 | #include <asm-generic/errno-base.h> | ||
| 4 | #include <stddef.h> | 5 | #include <stddef.h> |
| 5 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 6 | #include <errno.h> | 7 | #include <errno.h> |
| @@ -32,20 +33,6 @@ dllnode * dllnode_init(void *data, dll_freecb fcb) { | |||
| 32 | return n; | 33 | return n; |
| 33 | } | 34 | } |
| 34 | 35 | ||
| 35 | int dllnode_free(dllnode **n) { | ||
| 36 | if(!n) | ||
| 37 | RETURNWERR(EINVAL, 1); | ||
| 38 | if(!(*n)) | ||
| 39 | RETURNWERR(EINVAL, 1); | ||
| 40 | |||
| 41 | if((*n)->freecb != NULL) | ||
| 42 | (*n)->freecb((*n)->data); | ||
| 43 | free(*n); | ||
| 44 | *n = NULL; | ||
| 45 | |||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | dlinkedlist * dlinkedlist_init(void) { | 36 | dlinkedlist * dlinkedlist_init(void) { |
| 50 | dlinkedlist *ll = xcalloc(1, sizeof(*ll)); | 37 | dlinkedlist *ll = xcalloc(1, sizeof(*ll)); |
| 51 | ll->end = NULL; | 38 | ll->end = NULL; |
| @@ -55,21 +42,23 @@ dlinkedlist * dlinkedlist_init(void) { | |||
| 55 | return ll; | 42 | return ll; |
| 56 | } | 43 | } |
| 57 | 44 | ||
| 58 | int dlinkedlist_free(dlinkedlist **ll) { | 45 | void dlinkedlist_free(dlinkedlist *ll) { |
| 59 | if(!ll) | 46 | if(!ll) |
| 60 | RETURNWERR(EINVAL, 1); | 47 | return; |
| 61 | if(!(*ll)) | 48 | |
| 62 | RETURNWERR(EINVAL, 1); | 49 | for(dllnode *current = ll->start, *next; current != NULL; ) { |
| 50 | next = current->next; | ||
| 63 | 51 | ||
| 64 | for(dllnode *n; (*ll)->start != NULL;) { | 52 | // This used to be a function but was causing issues for some inscrutable reason |
| 65 | n = (*ll)->start->next; | 53 | if(current->freecb != NULL) |
| 66 | dllnode_free(&((*ll)->start)); | 54 | current->freecb(current->data); |
| 67 | (*ll)->start = n; | 55 | free(current); |
| 56 | |||
| 57 | current = next; | ||
| 68 | } | 58 | } |
| 69 | free(*ll); | 59 | free(ll); |
| 70 | *ll = NULL; | ||
| 71 | 60 | ||
| 72 | return 0; | 61 | return; |
| 73 | } | 62 | } |
| 74 | 63 | ||
| 75 | int dlinkedlist_size(const dlinkedlist * const ll) { | 64 | int dlinkedlist_size(const dlinkedlist * const ll) { |
| @@ -107,14 +96,14 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char | |||
| 107 | dllnode *n = dllnode_init(data, fcb); | 96 | dllnode *n = dllnode_init(data, fcb); |
| 108 | switch (op) { | 97 | switch (op) { |
| 109 | case 'a': | 98 | case 'a': |
| 110 | n->prev = ll->end; | 99 | n->prev = (ll->end); |
| 111 | ll->end->next = n; | 100 | (ll->end)->next = n; |
| 112 | ll->end = n; | 101 | ll->end = n; |
| 113 | break; | 102 | break; |
| 114 | 103 | ||
| 115 | case 'p': | 104 | case 'p': |
| 116 | n->next = ll->start; | 105 | n->next = (ll->start); |
| 117 | ll->start->prev = n; | 106 | (ll->start)->prev = n; |
| 118 | ll->start = n; | 107 | ll->start = n; |
| 119 | break; | 108 | break; |
| 120 | 109 | ||
| @@ -176,8 +165,8 @@ int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int i | |||
| 176 | if(!current) | 165 | if(!current) |
| 177 | XALLOC_EXIT("<dlinkedlist_insert> somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); | 166 | XALLOC_EXIT("<dlinkedlist_insert> somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); |
| 178 | 167 | ||
| 179 | current->prev->next = new; | 168 | (current->prev)->next = new; |
| 180 | new->prev = current->prev; | 169 | new->prev = (current->prev); |
| 181 | new->next = current; | 170 | new->next = current; |
| 182 | current->prev = new; | 171 | current->prev = new; |
| 183 | 172 | ||
| @@ -206,7 +195,10 @@ int dlinkedlist_remove(dlinkedlist * const ll, int index) { | |||
| 206 | current->next->prev = current->prev; | 195 | current->next->prev = current->prev; |
| 207 | } | 196 | } |
| 208 | 197 | ||
| 209 | dllnode_free(¤t); | 198 | if(current->freecb != NULL) |
| 199 | current->freecb(current->data); | ||
| 200 | free(current); | ||
| 201 | |||
| 210 | ll->size--; | 202 | ll->size--; |
| 211 | 203 | ||
| 212 | return 0; | 204 | return 0; |
| @@ -218,4 +210,16 @@ void* dlinkedlist_get(const dlinkedlist * const ll, int index) { | |||
| 218 | return NULL; | 210 | return NULL; |
| 219 | 211 | ||
| 220 | return n->data; | 212 | return n->data; |
| 213 | } | ||
| 214 | |||
| 215 | int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)) { | ||
| 216 | if(!ll) | ||
| 217 | RETURNWERR(EINVAL, -1); | ||
| 218 | if(!callback) | ||
| 219 | RETURNWERR(EINVAL, -1); | ||
| 220 | |||
| 221 | for(dllnode *p = ll->start; p != NULL; p = p->next) | ||
| 222 | callback(p->data); | ||
| 223 | |||
| 224 | return 0; | ||
| 221 | } \ No newline at end of file | 225 | } \ No newline at end of file |
