summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRob Landley2006-05-08 19:03:07 +0000
committerRob Landley2006-05-08 19:03:07 +0000
commita6b5b60942b4e28965f10cb107d4f94aaf756bc1 (patch)
treee97daaa61ca25179f9cfd9251446d00521bb5ddb /libbb
parent712ba85b30426bc8fa093c21ae9a5d0018450cc7 (diff)
downloadbusybox-a6b5b60942b4e28965f10cb107d4f94aaf756bc1.zip
busybox-a6b5b60942b4e28965f10cb107d4f94aaf756bc1.tar.gz
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.
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Makefile.in2
-rw-r--r--libbb/llist.c31
2 files changed, 22 insertions, 11 deletions
diff --git a/libbb/Makefile.in b/libbb/Makefile.in
index e7725b8..865b7e7 100644
--- a/libbb/Makefile.in
+++ b/libbb/Makefile.in
@@ -101,7 +101,7 @@ $(LIBBB_MOBJ5):$(LIBBB_MSRC5)
$(compile.c) -DL_$(notdir $*)
LIBBB_MSRC6:=$(srcdir)/llist.c
-LIBBB_MOBJ6:=llist_add_to.o llist_add_to_end.o llist_free_one.o llist_free.o
+LIBBB_MOBJ6:=llist_add_to.o llist_add_to_end.o llist_pop.o llist_free.o
LIBBB_MOBJ6:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ6))
$(LIBBB_MOBJ6):$(LIBBB_MSRC6)
$(compile.c) -DL_$(notdir $*)
diff --git a/libbb/llist.c b/libbb/llist.c
index 5b70d66..0d599db 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)
}
#endif
-#ifdef L_llist_free_one
-/* Free the current list element and advance to the next entry in the list.
- * Returns a pointer to the next element. */
-llist_t *llist_free_one(llist_t *elm)
+#ifdef L_llist_pop
+/* Remove first element from the list and return it */
+void *llist_pop(llist_t **head)
{
- llist_t *next = elm ? elm->link : NULL;
- free(elm);
- return next;
+ void *data;
+
+ if(!*head) data = *head;
+ else {
+ void *next = (*head)->link;
+ data = (*head)->data;
+ *head = (*head)->link;
+ free(next);
+ }
+
+ return data;
}
#endif
#ifdef L_llist_free
-/* Recursively free all elements in the linked list. */
-void llist_free(llist_t *elm)
+/* Recursively free all elements in the linked list. If freeit != NULL
+ * call it on each datum in the list */
+void llist_free(llist_t *elm, void (*freeit)(void *data))
{
- while ((elm = llist_free_one(elm)));
+ while (elm) {
+ void *data = llist_pop(&elm);
+ if (freeit) freeit(data);
+ }
}
#endif