Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * linked list helper functions. |
| 4 | * |
| 5 | * Copyright (C) 2003 Glenn McGrath |
| 6 | * Copyright (C) 2005 Vladimir Oleynik |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 7 | * Copyright (C) 2005 Bernhard Reutner-Fischer |
Rob Landley | cc848dd | 2006-05-11 18:25:24 +0000 | [diff] [blame] | 8 | * Copyright (C) 2006 Rob Landley <rob@landley.net> |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 9 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 10 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 11 | */ |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 12 | |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
| 14 | |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 15 | /* Add data to the start of the linked list. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 16 | void FAST_FUNC llist_add_to(llist_t **old_head, void *data) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 17 | { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 18 | llist_t *new_head = xmalloc(sizeof(llist_t)); |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 19 | |
Rob Landley | 5edc102 | 2006-05-26 23:00:10 +0000 | [diff] [blame] | 20 | new_head->data = data; |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 21 | new_head->link = *old_head; |
| 22 | *old_head = new_head; |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 23 | } |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 24 | |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 25 | /* Add data to the end of the linked list. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 26 | void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 27 | { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 28 | llist_t *new_item = xmalloc(sizeof(llist_t)); |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 29 | |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 30 | new_item->data = data; |
| 31 | new_item->link = NULL; |
| 32 | |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 33 | if (!*list_head) |
| 34 | *list_head = new_item; |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 35 | else { |
| 36 | llist_t *tail = *list_head; |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 37 | |
| 38 | while (tail->link) |
| 39 | tail = tail->link; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 40 | tail->link = new_item; |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 41 | } |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 42 | } |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 43 | |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 44 | /* Remove first element from the list and return it */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 45 | void* FAST_FUNC llist_pop(llist_t **head) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 46 | { |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 47 | void *data, *next; |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 48 | |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 49 | if (!*head) |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 50 | return NULL; |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 52 | data = (*head)->data; |
| 53 | next = (*head)->link; |
| 54 | free(*head); |
| 55 | *head = next; |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 56 | |
| 57 | return data; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 58 | } |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 60 | /* Unlink arbitrary given element from the list */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 61 | void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm) |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 62 | { |
| 63 | llist_t *crt; |
| 64 | |
| 65 | if (!(elm && *head)) |
| 66 | return; |
| 67 | |
| 68 | if (elm == *head) { |
| 69 | *head = (*head)->link; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | for (crt = *head; crt; crt = crt->link) { |
| 74 | if (crt->link == elm) { |
| 75 | crt->link = elm->link; |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 81 | /* Recursively free all elements in the linked list. If freeit != NULL |
| 82 | * call it on each datum in the list */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 83 | void FAST_FUNC llist_free(llist_t *elm, void (*freeit) (void *data)) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 84 | { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 85 | while (elm) { |
| 86 | void *data = llist_pop(&elm); |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 87 | |
| 88 | if (freeit) |
| 89 | freeit(data); |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 90 | } |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 91 | } |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 8d9f495 | 2007-04-08 15:08:42 +0000 | [diff] [blame] | 93 | /* Reverse list order. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 94 | llist_t* FAST_FUNC llist_rev(llist_t *list) |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 95 | { |
Denis Vlasenko | 8d9f495 | 2007-04-08 15:08:42 +0000 | [diff] [blame] | 96 | llist_t *rev = NULL; |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 98 | while (list) { |
| 99 | llist_t *next = list->link; |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 100 | |
Denis Vlasenko | 8d9f495 | 2007-04-08 15:08:42 +0000 | [diff] [blame] | 101 | list->link = rev; |
| 102 | rev = list; |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 103 | list = next; |
| 104 | } |
Denis Vlasenko | 8d9f495 | 2007-04-08 15:08:42 +0000 | [diff] [blame] | 105 | return rev; |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 106 | } |