1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
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
12  * are also available at http://curl.haxx.se/docs/copyright.html.
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  * $Id: llist.c,v 1.23 2008-10-20 23:24:37 yangtse Exp $
22  ***************************************************************************/
23 
24 #include "setup.h"
25 
26 #include <string.h>
27 #include <stdlib.h>
28 
29 #include "llist.h"
30 #include "memory.h"
31 
32 /* this must be the last include file */
33 #include "memdebug.h"
34 
35 void
Curl_llist_init(struct curl_llist * l,curl_llist_dtor dtor)36 Curl_llist_init(struct curl_llist *l, curl_llist_dtor dtor)
37 {
38   l->size = 0;
39   l->dtor = dtor;
40   l->head = NULL;
41   l->tail = NULL;
42 }
43 
44 struct curl_llist *
Curl_llist_alloc(curl_llist_dtor dtor)45 Curl_llist_alloc(curl_llist_dtor dtor)
46 {
47   struct curl_llist *list;
48 
49   list = malloc(sizeof(struct curl_llist));
50   if(NULL == list)
51     return NULL;
52 
53   Curl_llist_init(list, dtor);
54 
55   return list;
56 }
57 
58 /*
59  * Curl_llist_insert_next() returns 1 on success and 0 on failure.
60  */
61 int
Curl_llist_insert_next(struct curl_llist * list,struct curl_llist_element * e,const void * p)62 Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e,
63                        const void *p)
64 {
65   struct curl_llist_element *ne = malloc(sizeof(struct curl_llist_element));
66   if(!ne)
67     return 0;
68 
69   ne->ptr = (void *) p;
70   if(list->size == 0) {
71     list->head = ne;
72     list->head->prev = NULL;
73     list->head->next = NULL;
74     list->tail = ne;
75   }
76   else {
77     ne->next = e->next;
78     ne->prev = e;
79     if(e->next) {
80       e->next->prev = ne;
81     }
82     else {
83       list->tail = ne;
84     }
85     e->next = ne;
86   }
87 
88   ++list->size;
89 
90   return 1;
91 }
92 
93 int
Curl_llist_remove(struct curl_llist * list,struct curl_llist_element * e,void * user)94 Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
95                   void *user)
96 {
97   if(e == NULL || list->size == 0)
98     return 1;
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;
107   } else {
108     e->prev->next = e->next;
109     if(!e->next)
110       list->tail = e->prev;
111     else
112       e->next->prev = e->prev;
113   }
114 
115   list->dtor(user, e->ptr);
116 
117   free(e);
118   --list->size;
119 
120   return 1;
121 }
122 
123 void
Curl_llist_destroy(struct curl_llist * list,void * user)124 Curl_llist_destroy(struct curl_llist *list, void *user)
125 {
126   if(list) {
127     while(list->size > 0)
128       Curl_llist_remove(list, list->tail, user);
129 
130     free(list);
131   }
132 }
133 
134 size_t
Curl_llist_count(struct curl_llist * list)135 Curl_llist_count(struct curl_llist *list)
136 {
137   return list->size;
138 }
139 
Curl_llist_move(struct curl_llist * list,struct curl_llist_element * e,struct curl_llist * to_list,struct curl_llist_element * to_e)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)
142 {
143   /* Remove element from list */
144   if(e == NULL || list->size == 0)
145     return 0;
146 
147   if(e == list->head) {
148     list->head = e->next;
149 
150     if(list->head == NULL)
151       list->tail = NULL;
152     else
153       e->next->prev = NULL;
154   }
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;
161   }
162 
163   --list->size;
164 
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;
171   }
172   else {
173     e->next = to_e->next;
174     e->prev = to_e;
175     if(to_e->next) {
176       to_e->next->prev = e;
177     }
178     else {
179       to_list->tail = e;
180     }
181     to_e->next = e;
182   }
183 
184   ++to_list->size;
185 
186   return 1;
187 }
188