aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2009-01-13 15:22:50 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2009-01-13 15:22:50 +0000
commit1265df1f3194db1da2b9d711772038d6045e2ef4 (patch)
tree95a4c13cf88f4477c723a7b7167a8ac227ca28b2
parent35261159e6ccddcb36fce427d04b93e119c116c0 (diff)
downloadbusybox-w32-1265df1f3194db1da2b9d711772038d6045e2ef4.tar.gz
busybox-w32-1265df1f3194db1da2b9d711772038d6045e2ef4.tar.bz2
busybox-w32-1265df1f3194db1da2b9d711772038d6045e2ef4.zip
libbb: shrink linked list ops (by xmaks AT email.cz)
function old new delta llist_pop 33 29 -4 llist_unlink 47 28 -19 llist_add_to_end 50 31 -19 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-42) Total: -42 bytes
-rw-r--r--libbb/llist.c54
1 files changed, 18 insertions, 36 deletions
diff --git a/libbb/llist.c b/libbb/llist.c
index 7e78f7cb4..5ba7f6047 100644
--- a/libbb/llist.c
+++ b/libbb/llist.c
@@ -25,56 +25,38 @@ void FAST_FUNC llist_add_to(llist_t **old_head, void *data)
25/* Add data to the end of the linked list. */ 25/* Add data to the end of the linked list. */
26void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data) 26void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data)
27{ 27{
28 llist_t *new_item = xmalloc(sizeof(llist_t)); 28 while (*list_head)
29 29 list_head = &(*list_head)->link;
30 new_item->data = data; 30 *list_head = xzalloc(sizeof(llist_t));
31 new_item->link = NULL; 31 (*list_head)->data = data;
32 32 /*(*list_head)->link = NULL;*/
33 if (!*list_head)
34 *list_head = new_item;
35 else {
36 llist_t *tail = *list_head;
37
38 while (tail->link)
39 tail = tail->link;
40 tail->link = new_item;
41 }
42} 33}
43 34
44/* Remove first element from the list and return it */ 35/* Remove first element from the list and return it */
45void* FAST_FUNC llist_pop(llist_t **head) 36void* FAST_FUNC llist_pop(llist_t **head)
46{ 37{
47 void *data, *next; 38 void *data = NULL;
48 39 llist_t *temp = *head;
49 if (!*head)
50 return NULL;
51
52 data = (*head)->data;
53 next = (*head)->link;
54 free(*head);
55 *head = next;
56 40
41 if (temp) {
42 data = temp->data;
43 *head = temp->link;
44 free(temp);
45 }
57 return data; 46 return data;
58} 47}
59 48
60/* Unlink arbitrary given element from the list */ 49/* Unlink arbitrary given element from the list */
61void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm) 50void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm)
62{ 51{
63 llist_t *crt; 52 if (!elm)
64
65 if (!(elm && *head))
66 return;
67
68 if (elm == *head) {
69 *head = (*head)->link;
70 return; 53 return;
71 } 54 while (*head) {
72 55 if (*head == elm) {
73 for (crt = *head; crt; crt = crt->link) { 56 *head = (*head)->link;
74 if (crt->link == elm) { 57 break;
75 crt->link = elm->link;
76 return;
77 } 58 }
59 head = &(*head)->link;
78 } 60 }
79} 61}
80 62