Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / llist.c
blob1650c9c8e21c33b677e00552030fad13d2da9d5a
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, 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 * $Id: llist.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #include <string.h>
27 #include <stdlib.h>
29 #include "llist.h"
30 #include "memory.h"
32 /* this must be the last include file */
33 #include "memdebug.h"
35 void
36 Curl_llist_init(struct curl_llist *l, curl_llist_dtor dtor)
38 l->size = 0;
39 l->dtor = dtor;
40 l->head = NULL;
41 l->tail = NULL;
44 struct curl_llist *
45 Curl_llist_alloc(curl_llist_dtor dtor)
47 struct curl_llist *list;
49 list = (struct curl_llist *)malloc(sizeof(struct curl_llist));
50 if(NULL == list)
51 return NULL;
53 Curl_llist_init(list, dtor);
55 return list;
59 * Curl_llist_insert_next() returns 1 on success and 0 on failure.
61 int
62 Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e,
63 const void *p)
65 struct curl_llist_element *ne =
66 (struct curl_llist_element *) malloc(sizeof(struct curl_llist_element));
67 if(!ne)
68 return 0;
70 ne->ptr = (void *) p;
71 if(list->size == 0) {
72 list->head = ne;
73 list->head->prev = NULL;
74 list->head->next = NULL;
75 list->tail = ne;
77 else {
78 ne->next = e->next;
79 ne->prev = e;
80 if(e->next) {
81 e->next->prev = ne;
83 else {
84 list->tail = ne;
86 e->next = ne;
89 ++list->size;
91 return 1;
94 int
95 Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
96 void *user)
98 if(e == NULL || list->size == 0)
99 return 1;
101 if(e == list->head) {
102 list->head = e->next;
104 if(list->head == NULL)
105 list->tail = NULL;
106 else
107 e->next->prev = NULL;
108 } else {
109 e->prev->next = e->next;
110 if(!e->next)
111 list->tail = e->prev;
112 else
113 e->next->prev = e->prev;
116 list->dtor(user, e->ptr);
117 free(e);
118 --list->size;
120 return 1;
123 void
124 Curl_llist_destroy(struct curl_llist *list, void *user)
126 if(list) {
127 while(list->size > 0)
128 Curl_llist_remove(list, list->tail, user);
130 free(list);
134 size_t
135 Curl_llist_count(struct curl_llist *list)
137 return list->size;
140 int Curl_llist_move(struct curl_llist *list, struct curl_llist_element *e,
141 struct curl_llist *to_list, struct curl_llist_element *to_e)
143 /* Remove element from list */
144 if(e == NULL || list->size == 0)
145 return 0;
147 if(e == list->head) {
148 list->head = e->next;
150 if(list->head == NULL)
151 list->tail = NULL;
152 else
153 e->next->prev = NULL;
155 else {
156 e->prev->next = e->next;
157 if(!e->next)
158 list->tail = e->prev;
159 else
160 e->next->prev = e->prev;
163 --list->size;
165 /* Add element to to_list after to_e */
166 if(to_list->size == 0) {
167 to_list->head = e;
168 to_list->head->prev = NULL;
169 to_list->head->next = NULL;
170 to_list->tail = e;
172 else {
173 e->next = to_e->next;
174 e->prev = to_e;
175 if(to_e->next) {
176 to_e->next->prev = e;
178 else {
179 to_list->tail = e;
181 to_e->next = e;
184 ++to_list->size;
186 return 1;