1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 llist_init(struct curl_llist *l, curl_llist_dtor dtor)
46 Curl_llist_alloc(curl_llist_dtor dtor)
48 struct curl_llist *list;
50 list = malloc(sizeof(struct curl_llist));
54 llist_init(list, dtor);
60 * Curl_llist_insert_next()
62 * Inserts a new list element after the given one 'e'. If the given existing
63 * entry is NULL and the list already has elements, the new one will be
64 * inserted first in the list.
66 * Returns: 1 on success and 0 on failure.
71 Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e,
74 struct curl_llist_element *ne = malloc(sizeof(struct curl_llist_element));
81 list->head->prev = NULL;
82 list->head->next = NULL;
86 /* if 'e' is NULL here, we insert the new element first in the list */
87 ne->next = e?e->next:list->head;
90 list->head->prev = ne;
112 Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
115 if(e == NULL || list->size == 0)
118 if(e == list->head) {
119 list->head = e->next;
121 if(list->head == NULL)
124 e->next->prev = NULL;
127 e->prev->next = e->next;
129 list->tail = e->prev;
131 e->next->prev = e->prev;
134 list->dtor(user, e->ptr);
147 Curl_llist_destroy(struct curl_llist *list, void *user)
150 while(list->size > 0)
151 Curl_llist_remove(list, list->tail, user);
158 Curl_llist_count(struct curl_llist *list)
166 int Curl_llist_move(struct curl_llist *list, struct curl_llist_element *e,
167 struct curl_llist *to_list,
168 struct curl_llist_element *to_e)
170 /* Remove element from list */
171 if(e == NULL || list->size == 0)
174 if(e == list->head) {
175 list->head = e->next;
177 if(list->head == NULL)
180 e->next->prev = NULL;
183 e->prev->next = e->next;
185 list->tail = e->prev;
187 e->next->prev = e->prev;
192 /* Add element to to_list after to_e */
193 if(to_list->size == 0) {
195 to_list->head->prev = NULL;
196 to_list->head->next = NULL;
200 e->next = to_e->next;
203 to_e->next->prev = e;