summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenis Vlasenko2009-01-13 15:22:50 +0000
committerDenis Vlasenko2009-01-13 15:22:50 +0000
commit1265df1f3194db1da2b9d711772038d6045e2ef4 (patch)
tree95a4c13cf88f4477c723a7b7167a8ac227ca28b2 /libbb
parent35261159e6ccddcb36fce427d04b93e119c116c0 (diff)
downloadbusybox-1265df1f3194db1da2b9d711772038d6045e2ef4.zip
busybox-1265df1f3194db1da2b9d711772038d6045e2ef4.tar.gz
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
Diffstat (limited to 'libbb')
-rw-r--r--libbb/llist.c54
1 files changed, 18 insertions, 36 deletions
diff --git a/libbb/llist.c b/libbb/llist.c
index 7e78f7c..5ba7f60 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)
/* Add data to the end of the linked list. */
void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data)
{
- llist_t *new_item = xmalloc(sizeof(llist_t));
-
- new_item->data = data;
- new_item->link = NULL;
-
- if (!*list_head)
- *list_head = new_item;
- else {
- llist_t *tail = *list_head;
-
- while (tail->link)
- tail = tail->link;
- tail->link = new_item;
- }
+ while (*list_head)
+ list_head = &(*list_head)->link;
+ *list_head = xzalloc(sizeof(llist_t));
+ (*list_head)->data = data;
+ /*(*list_head)->link = NULL;*/
}
/* Remove first element from the list and return it */
void* FAST_FUNC llist_pop(llist_t **head)
{
- void *data, *next;
-
- if (!*head)
- return NULL;
-
- data = (*head)->data;
- next = (*head)->link;
- free(*head);
- *head = next;
+ void *data = NULL;
+ llist_t *temp = *head;
+ if (temp) {
+ data = temp->data;
+ *head = temp->link;
+ free(temp);
+ }
return data;
}
/* Unlink arbitrary given element from the list */
void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm)
{
- llist_t *crt;
-
- if (!(elm && *head))
- return;
-
- if (elm == *head) {
- *head = (*head)->link;
+ if (!elm)
return;
- }
-
- for (crt = *head; crt; crt = crt->link) {
- if (crt->link == elm) {
- crt->link = elm->link;
- return;
+ while (*head) {
+ if (*head == elm) {
+ *head = (*head)->link;
+ break;
}
+ head = &(*head)->link;
}
}