1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
28 #include "curl_memory.h"
30 /* this must be the last include file */
37 Curl_llist_init(struct curl_llist *l, curl_llist_dtor dtor)
46 * Curl_llist_insert_next()
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.
52 * The 'ne' argument should be a pointer into the object to store.
57 Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e,
59 struct curl_llist_element *ne)
64 list->head->prev = NULL;
65 list->head->next = NULL;
69 /* if 'e' is NULL here, we insert the new element first in the list */
70 ne->next = e?e->next:list->head;
73 list->head->prev = ne;
93 Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
97 if(e == NULL || list->size == 0)
100 if(e == list->head) {
101 list->head = e->next;
103 if(list->head == NULL)
106 e->next->prev = NULL;
110 list->head = e->next;
112 e->prev->next = e->next;
115 list->tail = e->prev;
117 e->next->prev = e->prev;
128 /* call the dtor() last for when it actually frees the 'e' memory itself */
130 list->dtor(user, ptr);
134 Curl_llist_destroy(struct curl_llist *list, void *user)
137 while(list->size > 0)
138 Curl_llist_remove(list, list->tail, user);
143 Curl_llist_count(struct curl_llist *list)
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)
155 /* Remove element from list */
156 if(e == NULL || list->size == 0)
159 if(e == list->head) {
160 list->head = e->next;
162 if(list->head == NULL)
165 e->next->prev = NULL;
168 e->prev->next = e->next;
170 list->tail = e->prev;
172 e->next->prev = e->prev;
177 /* Add element to to_list after to_e */
178 if(to_list->size == 0) {
180 to_list->head->prev = NULL;
181 to_list->head->next = NULL;
185 e->next = to_e->next;
188 to_e->next->prev = e;