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