aboutsummaryrefslogtreecommitdiff
path: root/libbb/llist.c
diff options
context:
space:
mode:
authorlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-05-08 19:03:07 +0000
committerlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-05-08 19:03:07 +0000
commit048ae3483e3ed87f390d42e420c021e6b2fe85fe (patch)
treee97daaa61ca25179f9cfd9251446d00521bb5ddb /libbb/llist.c
parent75b3ad9deb25bb7305f4e4c139991ac715d27238 (diff)
downloadbusybox-w32-048ae3483e3ed87f390d42e420c021e6b2fe85fe.tar.gz
busybox-w32-048ae3483e3ed87f390d42e420c021e6b2fe85fe.tar.bz2
busybox-w32-048ae3483e3ed87f390d42e420c021e6b2fe85fe.zip
Fiddling with llist to make memory management easier. Specifically, the
option to delete the contents of the list when we delete the list is a good thing. git-svn-id: svn://busybox.net/trunk/busybox@15037 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb/llist.c')
-rw-r--r--libbb/llist.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/libbb/llist.c b/libbb/llist.c
index 5b70d6628..0d599db6b 100644
--- a/libbb/llist.c
+++ b/libbb/llist.c
@@ -47,21 +47,32 @@ llist_t *llist_add_to_end(llist_t *list_head, char *data)
47} 47}
48#endif 48#endif
49 49
50#ifdef L_llist_free_one 50#ifdef L_llist_pop
51/* Free the current list element and advance to the next entry in the list. 51/* Remove first element from the list and return it */
52 * Returns a pointer to the next element. */ 52void *llist_pop(llist_t **head)
53llist_t *llist_free_one(llist_t *elm)
54{ 53{
55 llist_t *next = elm ? elm->link : NULL; 54 void *data;
56 free(elm); 55
57 return next; 56 if(!*head) data = *head;
57 else {
58 void *next = (*head)->link;
59 data = (*head)->data;
60 *head = (*head)->link;
61 free(next);
62 }
63
64 return data;
58} 65}
59#endif 66#endif
60 67
61#ifdef L_llist_free 68#ifdef L_llist_free
62/* Recursively free all elements in the linked list. */ 69/* Recursively free all elements in the linked list. If freeit != NULL
63void llist_free(llist_t *elm) 70 * call it on each datum in the list */
71void llist_free(llist_t *elm, void (*freeit)(void *data))
64{ 72{
65 while ((elm = llist_free_one(elm))); 73 while (elm) {
74 void *data = llist_pop(&elm);
75 if (freeit) freeit(data);
76 }
66} 77}
67#endif 78#endif