willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | This File is copied from |
| 3 | |
| 4 | http://www.oreilly.com/catalog/masteralgoc/index.html |
| 5 | Mastering Algorithms with C |
| 6 | By Kyle Loudon |
| 7 | ISBN: 1-56592-453-3 |
| 8 | Publishd by O'Reilly |
| 9 | |
| 10 | */ |
| 11 | |
| 12 | /***************************************************************************** |
| 13 | * * |
| 14 | * -------------------------------- list.c -------------------------------- * |
| 15 | * * |
| 16 | *****************************************************************************/ |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame^] | 21 | #include <common/list.h> |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 22 | |
| 23 | /***************************************************************************** |
| 24 | * * |
| 25 | * ------------------------------- list_init ------------------------------ * |
| 26 | * * |
| 27 | *****************************************************************************/ |
| 28 | |
| 29 | void list_init(List *list, void (*destroy)(void *data)) { |
| 30 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | /********************************************************************* |
| 32 | * * |
| 33 | * Initialize the list. * |
| 34 | * * |
| 35 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 36 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | list->size = 0; |
| 38 | list->destroy = destroy; |
| 39 | list->head = NULL; |
| 40 | list->tail = NULL; |
| 41 | return; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 42 | } /* end list_init() */ |
| 43 | |
| 44 | /***************************************************************************** |
| 45 | * * |
| 46 | * ----------------------------- list_destroy ----------------------------- * |
| 47 | * * |
| 48 | *****************************************************************************/ |
| 49 | |
| 50 | void list_destroy(List *list) { |
| 51 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | void *data; |
| 53 | int rc; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 54 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | /********************************************************************* |
| 56 | * * |
| 57 | * Remove each element. * |
| 58 | * * |
| 59 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 60 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | while (list_size(list) > 0) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 62 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | rc = list_rem_next(list, NULL, (void **)&data); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 64 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | if (( rc == 0) && (list->destroy != NULL)) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 66 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | /******************************************************************* |
| 68 | * * |
| 69 | * Call a user-defined function to free dynamically allocated data.* |
| 70 | * * |
| 71 | *******************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 72 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 73 | list->destroy(data); |
| 74 | }/* end if() */ |
| 75 | }/* end while() */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 76 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | /************************************************************************** |
| 78 | * * |
| 79 | * No operations are allowed now, but clear the structure as a precaution.* |
| 80 | * * |
| 81 | **************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 82 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | memset(list, 0, sizeof(List)); |
| 84 | return; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 85 | } /* void list_destroy(List *list) */ |
| 86 | |
| 87 | /***************************************************************************** |
| 88 | * * |
| 89 | * ----------------------------- list_ins_next ---------------------------- * |
| 90 | * * |
| 91 | *****************************************************************************/ |
| 92 | |
| 93 | int list_ins_next(List *list, ListElmt *element, const void *data) { |
| 94 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | ListElmt *new_element; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 96 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | /********************************************************************* |
| 98 | * * |
| 99 | * Allocate storage for the element. * |
| 100 | * * |
| 101 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 102 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL) |
| 104 | return -1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 105 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | /********************************************************************* |
| 107 | * * |
| 108 | * Insert the element into the list. * |
| 109 | * * |
| 110 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 111 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | new_element->data = (void *)data; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 113 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | if (element == NULL) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 115 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | /************************************************************* |
| 117 | * * |
| 118 | * Handle insertion at the head of the list. * |
| 119 | * * |
| 120 | *************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 121 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | if (list_size(list) == 0) |
| 123 | list->tail = new_element; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 125 | new_element->next = list->head; |
| 126 | list->head = new_element; |
| 127 | }/* end if (element == NULL) */ |
| 128 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 129 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 130 | /************************************************************* |
| 131 | * * |
| 132 | * Handle insertion somewhere other than at the head. * |
| 133 | * * |
| 134 | *************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 135 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 136 | if (element->next == NULL) |
| 137 | list->tail = new_element; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 138 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 139 | new_element->next = element->next; |
| 140 | element->next = new_element; |
| 141 | }/* end else */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 142 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 143 | /********************************************************************* |
| 144 | * * |
| 145 | * Adjust the size of the list to account for the inserted element. * |
| 146 | * * |
| 147 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 148 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 149 | list->size++; |
| 150 | return 0; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 151 | } /* end list_ins_next() */ |
| 152 | |
| 153 | /***************************************************************************** |
| 154 | * * |
| 155 | * ----------------------------- list_rem_next ---------------------------- * |
| 156 | * * |
| 157 | *****************************************************************************/ |
| 158 | |
| 159 | int list_rem_next(List *list, ListElmt *element, void **data) { |
| 160 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 161 | ListElmt *old_element; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 162 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 163 | /********************************************************************* |
| 164 | * * |
| 165 | * Do not allow removal from an empty list. * |
| 166 | * * |
| 167 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 168 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | if (list_size(list) == 0) |
| 170 | return -1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 171 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 172 | /********************************************************************* |
| 173 | * * |
| 174 | * Remove the element from the list. * |
| 175 | * * |
| 176 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 177 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 178 | if (element == NULL) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 179 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 180 | /************************************************************* |
| 181 | * * |
| 182 | * Handle removal from the head of the list. * |
| 183 | * * |
| 184 | *************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 185 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 186 | *data = list->head->data; |
| 187 | old_element = list->head; |
| 188 | list->head = list->head->next; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 189 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 190 | if (list_size(list) == 1) |
| 191 | list->tail = NULL; |
| 192 | }/* end if (element == NULL) */ |
| 193 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 194 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 195 | /************************************************************* |
| 196 | * * |
| 197 | * Handle removal from somewhere other than the head. * |
| 198 | * * |
| 199 | *************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 200 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 201 | if (element->next == NULL) |
| 202 | return -1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 203 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 204 | *data = element->next->data; |
| 205 | old_element = element->next; |
| 206 | element->next = element->next->next; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 207 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 208 | if (element->next == NULL) |
| 209 | list->tail = element; |
| 210 | }/* end else */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 211 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 212 | /********************************************************************* |
| 213 | * * |
| 214 | * Free the storage allocated by the abstract data type. * |
| 215 | * * |
| 216 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 217 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 218 | free(old_element); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 219 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 220 | /********************************************************************* |
| 221 | * * |
| 222 | * Adjust the size of the list to account for the removed element. * |
| 223 | * * |
| 224 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 225 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 226 | list->size--; |
| 227 | return 0; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 228 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * Local variables: |
| 232 | * c-indent-level: 8 |
| 233 | * c-basic-offset: 8 |
| 234 | * End: |
| 235 | */ |