1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 http://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"
26 #include "curl_memory.h"
28 /* this must be the last include file */
35 llist_init(struct curl_llist *l, curl_llist_dtor dtor)
44 Curl_llist_alloc(curl_llist_dtor dtor)
46 struct curl_llist *list;
48 list = malloc(sizeof(struct curl_llist));
52 llist_init(list, dtor);
58 * Curl_llist_insert_next()
60 * Inserts a new list element after the given one 'e'. If the given existing
61 * entry is NULL and the list already has elements, the new one will be
62 * inserted first in the list.
64 * Returns: 1 on success and 0 on failure.
69 Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e,
72 struct curl_llist_element *ne = malloc(sizeof(struct curl_llist_element));
79 list->head->prev = NULL;
80 list->head->next = NULL;
84 /* if 'e' is NULL here, we insert the new element first in the list */
85 ne->next = e?e->next:list->head;
88 list->head->prev = ne;
110 Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
113 if(e == NULL || list->size == 0)
116 if(e == list->head) {
117 list->head = e->next;
119 if(list->head == NULL)
122 e->next->prev = NULL;
125 e->prev->next = e->next;
127 list->tail = e->prev;
129 e->next->prev = e->prev;
132 list->dtor(user, e->ptr);
145 Curl_llist_destroy(struct curl_llist *list, void *user)
148 while(list->size > 0)
149 Curl_llist_remove(list, list->tail, user);
156 Curl_llist_count(struct curl_llist *list)
164 int Curl_llist_move(struct curl_llist *list, struct curl_llist_element *e,
165 struct curl_llist *to_list,
166 struct curl_llist_element *to_e)
168 /* Remove element from list */
169 if(e == NULL || list->size == 0)
172 if(e == list->head) {
173 list->head = e->next;
175 if(list->head == NULL)
178 e->next->prev = NULL;
181 e->prev->next = e->next;
183 list->tail = e->prev;
185 e->next->prev = e->prev;
190 /* Add element to to_list after to_e */
191 if(to_list->size == 0) {
193 to_list->head->prev = NULL;
194 to_list->head->next = NULL;
198 e->next = to_e->next;
201 to_e->next->prev = e;