Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 13 | * |
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
| 16 | * furnished to do so, under the terms of the COPYING file. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ***************************************************************************/ |
| 22 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 23 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 24 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 25 | #include <curl/curl.h> |
| 26 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 27 | #include "llist.h" |
| 28 | #include "curl_memory.h" |
| 29 | |
| 30 | /* this must be the last include file */ |
| 31 | #include "memdebug.h" |
| 32 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 33 | /* |
| 34 | * @unittest: 1300 |
| 35 | */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 36 | void |
| 37 | Curl_llist_init(struct curl_llist *l, curl_llist_dtor dtor) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 38 | { |
| 39 | l->size = 0; |
| 40 | l->dtor = dtor; |
| 41 | l->head = NULL; |
| 42 | l->tail = NULL; |
| 43 | } |
| 44 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 45 | /* |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 46 | * Curl_llist_insert_next() |
| 47 | * |
| 48 | * Inserts a new list element after the given one 'e'. If the given existing |
| 49 | * entry is NULL and the list already has elements, the new one will be |
| 50 | * inserted first in the list. |
| 51 | * |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 52 | * The 'ne' argument should be a pointer into the object to store. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 53 | * |
| 54 | * @unittest: 1300 |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 55 | */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 56 | void |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 57 | Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e, |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 58 | const void *p, |
| 59 | struct curl_llist_element *ne) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 60 | { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 61 | ne->ptr = (void *) p; |
| 62 | if(list->size == 0) { |
| 63 | list->head = ne; |
| 64 | list->head->prev = NULL; |
| 65 | list->head->next = NULL; |
| 66 | list->tail = ne; |
| 67 | } |
| 68 | else { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 69 | /* if 'e' is NULL here, we insert the new element first in the list */ |
| 70 | ne->next = e?e->next:list->head; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 71 | ne->prev = e; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 72 | if(!e) { |
| 73 | list->head->prev = ne; |
| 74 | list->head = ne; |
| 75 | } |
| 76 | else if(e->next) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 77 | e->next->prev = ne; |
| 78 | } |
| 79 | else { |
| 80 | list->tail = ne; |
| 81 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 82 | if(e) |
| 83 | e->next = ne; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | ++list->size; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 89 | /* |
| 90 | * @unittest: 1300 |
| 91 | */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 92 | void |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 93 | Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e, |
| 94 | void *user) |
| 95 | { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 96 | void *ptr; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 97 | if(e == NULL || list->size == 0) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 98 | return; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 99 | |
| 100 | if(e == list->head) { |
| 101 | list->head = e->next; |
| 102 | |
| 103 | if(list->head == NULL) |
| 104 | list->tail = NULL; |
| 105 | else |
| 106 | e->next->prev = NULL; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 107 | } |
| 108 | else { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 109 | if(!e->prev) |
| 110 | list->head = e->next; |
| 111 | else |
| 112 | e->prev->next = e->next; |
| 113 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 114 | if(!e->next) |
| 115 | list->tail = e->prev; |
| 116 | else |
| 117 | e->next->prev = e->prev; |
| 118 | } |
| 119 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 120 | ptr = e->ptr; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 121 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 122 | e->ptr = NULL; |
| 123 | e->prev = NULL; |
| 124 | e->next = NULL; |
| 125 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 126 | --list->size; |
| 127 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 128 | /* call the dtor() last for when it actually frees the 'e' memory itself */ |
| 129 | if(list->dtor) |
| 130 | list->dtor(user, ptr); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
| 134 | Curl_llist_destroy(struct curl_llist *list, void *user) |
| 135 | { |
| 136 | if(list) { |
| 137 | while(list->size > 0) |
| 138 | Curl_llist_remove(list, list->tail, user); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | size_t |
| 143 | Curl_llist_count(struct curl_llist *list) |
| 144 | { |
| 145 | return list->size; |
| 146 | } |
| 147 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 148 | /* |
| 149 | * @unittest: 1300 |
| 150 | */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 151 | void Curl_llist_move(struct curl_llist *list, struct curl_llist_element *e, |
| 152 | struct curl_llist *to_list, |
| 153 | struct curl_llist_element *to_e) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 154 | { |
| 155 | /* Remove element from list */ |
| 156 | if(e == NULL || list->size == 0) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 157 | return; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 158 | |
| 159 | if(e == list->head) { |
| 160 | list->head = e->next; |
| 161 | |
| 162 | if(list->head == NULL) |
| 163 | list->tail = NULL; |
| 164 | else |
| 165 | e->next->prev = NULL; |
| 166 | } |
| 167 | else { |
| 168 | e->prev->next = e->next; |
| 169 | if(!e->next) |
| 170 | list->tail = e->prev; |
| 171 | else |
| 172 | e->next->prev = e->prev; |
| 173 | } |
| 174 | |
| 175 | --list->size; |
| 176 | |
| 177 | /* Add element to to_list after to_e */ |
| 178 | if(to_list->size == 0) { |
| 179 | to_list->head = e; |
| 180 | to_list->head->prev = NULL; |
| 181 | to_list->head->next = NULL; |
| 182 | to_list->tail = e; |
| 183 | } |
| 184 | else { |
| 185 | e->next = to_e->next; |
| 186 | e->prev = to_e; |
| 187 | if(to_e->next) { |
| 188 | to_e->next->prev = e; |
| 189 | } |
| 190 | else { |
| 191 | to_list->tail = e; |
| 192 | } |
| 193 | to_e->next = e; |
| 194 | } |
| 195 | |
| 196 | ++to_list->size; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 197 | } |