diff options
author | Rob Landley <rob@landley.net> | 2006-05-08 19:03:07 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-05-08 19:03:07 +0000 |
commit | a6b5b60942b4e28965f10cb107d4f94aaf756bc1 (patch) | |
tree | e97daaa61ca25179f9cfd9251446d00521bb5ddb | |
parent | 712ba85b30426bc8fa093c21ae9a5d0018450cc7 (diff) | |
download | busybox-w32-a6b5b60942b4e28965f10cb107d4f94aaf756bc1.tar.gz busybox-w32-a6b5b60942b4e28965f10cb107d4f94aaf756bc1.tar.bz2 busybox-w32-a6b5b60942b4e28965f10cb107d4f94aaf756bc1.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.
-rw-r--r-- | include/libbb.h | 4 | ||||
-rw-r--r-- | libbb/Makefile.in | 2 | ||||
-rw-r--r-- | libbb/llist.c | 31 | ||||
-rw-r--r-- | procps/pidof.c | 4 | ||||
-rw-r--r-- | util-linux/mount.c | 2 |
5 files changed, 27 insertions, 16 deletions
diff --git a/include/libbb.h b/include/libbb.h index 48239798b..39361a232 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -477,8 +477,8 @@ typedef struct llist_s { | |||
477 | } llist_t; | 477 | } llist_t; |
478 | extern llist_t *llist_add_to(llist_t *old_head, char *new_item); | 478 | extern llist_t *llist_add_to(llist_t *old_head, char *new_item); |
479 | extern llist_t *llist_add_to_end(llist_t *list_head, char *data); | 479 | extern llist_t *llist_add_to_end(llist_t *list_head, char *data); |
480 | extern llist_t *llist_free_one(llist_t *elm); | 480 | extern void *llist_pop(llist_t **elm); |
481 | extern void llist_free(llist_t *elm); | 481 | extern void llist_free(llist_t *elm, void (*freeit)(void *data)); |
482 | 482 | ||
483 | extern void print_login_issue(const char *issue_file, const char *tty); | 483 | extern void print_login_issue(const char *issue_file, const char *tty); |
484 | extern void print_login_prompt(void); | 484 | extern void print_login_prompt(void); |
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 |
diff --git a/procps/pidof.c b/procps/pidof.c index 5b3e53fdf..98008aaf1 100644 --- a/procps/pidof.c +++ b/procps/pidof.c | |||
@@ -65,7 +65,7 @@ int pidof_main(int argc, char **argv) | |||
65 | while (omits_p) { | 65 | while (omits_p) { |
66 | /* are we asked to exclude the parent's process ID? */ | 66 | /* are we asked to exclude the parent's process ID? */ |
67 | if (!strncmp(omits_p->data, "%PPID", 5)) { | 67 | if (!strncmp(omits_p->data, "%PPID", 5)) { |
68 | omits_p = llist_free_one(omits_p); | 68 | llist_pop(&omits_p); |
69 | snprintf(getppid_str, sizeof(getppid_str), "%d", getppid()); | 69 | snprintf(getppid_str, sizeof(getppid_str), "%d", getppid()); |
70 | omits_p = llist_add_to(omits_p, getppid_str); | 70 | omits_p = llist_add_to(omits_p, getppid_str); |
71 | #if 0 | 71 | #if 0 |
@@ -117,7 +117,7 @@ int pidof_main(int argc, char **argv) | |||
117 | 117 | ||
118 | #if ENABLE_FEATURE_PIDOF_OMIT | 118 | #if ENABLE_FEATURE_PIDOF_OMIT |
119 | if (ENABLE_FEATURE_CLEAN_UP) | 119 | if (ENABLE_FEATURE_CLEAN_UP) |
120 | llist_free(omits); | 120 | llist_free(omits, NULL); |
121 | #endif | 121 | #endif |
122 | return fail ? EXIT_FAILURE : EXIT_SUCCESS; | 122 | return fail ? EXIT_FAILURE : EXIT_SUCCESS; |
123 | } | 123 | } |
diff --git a/util-linux/mount.c b/util-linux/mount.c index 61ceba829..4bd6433ca 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c | |||
@@ -182,7 +182,7 @@ llist_t *fslist = 0; | |||
182 | #if ENABLE_FEATURE_CLEAN_UP | 182 | #if ENABLE_FEATURE_CLEAN_UP |
183 | static void delete_block_backed_filesystems(void) | 183 | static void delete_block_backed_filesystems(void) |
184 | { | 184 | { |
185 | llist_free(fslist); | 185 | llist_free(fslist, free); |
186 | } | 186 | } |
187 | #else | 187 | #else |
188 | void delete_block_backed_filesystems(void); | 188 | void delete_block_backed_filesystems(void); |