diff options
| author | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-05-08 19:03:07 +0000 |
|---|---|---|
| committer | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-05-08 19:03:07 +0000 |
| commit | 048ae3483e3ed87f390d42e420c021e6b2fe85fe (patch) | |
| tree | e97daaa61ca25179f9cfd9251446d00521bb5ddb /libbb | |
| parent | 75b3ad9deb25bb7305f4e4c139991ac715d27238 (diff) | |
| download | busybox-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')
| -rw-r--r-- | libbb/Makefile.in | 2 | ||||
| -rw-r--r-- | libbb/llist.c | 31 |
2 files changed, 22 insertions, 11 deletions
diff --git a/libbb/Makefile.in b/libbb/Makefile.in index e7725b8f0..865b7e726 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in | |||
| @@ -101,7 +101,7 @@ $(LIBBB_MOBJ5):$(LIBBB_MSRC5) | |||
| 101 | $(compile.c) -DL_$(notdir $*) | 101 | $(compile.c) -DL_$(notdir $*) |
| 102 | 102 | ||
| 103 | LIBBB_MSRC6:=$(srcdir)/llist.c | 103 | LIBBB_MSRC6:=$(srcdir)/llist.c |
| 104 | LIBBB_MOBJ6:=llist_add_to.o llist_add_to_end.o llist_free_one.o llist_free.o | 104 | LIBBB_MOBJ6:=llist_add_to.o llist_add_to_end.o llist_pop.o llist_free.o |
| 105 | LIBBB_MOBJ6:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ6)) | 105 | LIBBB_MOBJ6:=$(patsubst %,$(LIBBB_DIR)/%, $(LIBBB_MOBJ6)) |
| 106 | $(LIBBB_MOBJ6):$(LIBBB_MSRC6) | 106 | $(LIBBB_MOBJ6):$(LIBBB_MSRC6) |
| 107 | $(compile.c) -DL_$(notdir $*) | 107 | $(compile.c) -DL_$(notdir $*) |
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. */ | 52 | void *llist_pop(llist_t **head) |
| 53 | llist_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 |
| 63 | void llist_free(llist_t *elm) | 70 | * call it on each datum in the list */ |
| 71 | void 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 |
