Uses less macros. #ifdef'ed out unused functions. Edited slightly to be
[platform/upstream/curl.git] / lib / llist.c
1 /***************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2003, 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$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include "llist.h"
30
31 #ifdef CURLDEBUG
32 /* this must be the last include file */
33 #include "memdebug.h"
34 #endif
35 void 
36 Curl_llist_init(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 curl_llist *
45 Curl_llist_alloc(curl_llist_dtor dtor)
46 {
47   curl_llist *list;
48
49   list = (curl_llist *)malloc(sizeof(curl_llist));
50   if(NULL == list)
51     return NULL;
52
53   Curl_llist_init(list, dtor);
54
55   return list;
56 }
57
58 int
59 Curl_llist_insert_next(curl_llist *list, curl_llist_element *e, const void *p)
60 {
61   curl_llist_element  *ne;
62
63   ne = (curl_llist_element *) malloc(sizeof(curl_llist_element));
64   ne->ptr = (void *) p;
65   if (list->size == 0) {
66     list->head = ne;
67     list->head->prev = NULL;
68     list->head->next = NULL;
69     list->tail = ne;
70   } else {
71     ne->next = e->next;
72     ne->prev = e;
73     if (e->next) {
74       e->next->prev = ne;
75     } else {
76       list->tail = ne;
77     }
78     e->next = ne;
79   }
80
81   ++list->size;
82
83   return 1;
84 }
85
86 int 
87 Curl_llist_insert_prev(curl_llist *list, curl_llist_element *e, const void *p)
88 {
89   curl_llist_element *ne;
90
91   ne = (curl_llist_element *) malloc(sizeof(curl_llist_element));
92   ne->ptr = (void *) p;
93   if (list->size == 0) {
94     list->head = ne;
95     list->head->prev = NULL;
96     list->head->next = NULL;
97     list->tail = ne;
98   } else {
99     ne->next = e;
100     ne->prev = e->prev;
101     if (e->prev)
102       e->prev->next = ne;
103     else
104       list->head = ne;
105     e->prev = ne;
106   }
107
108   ++list->size;
109
110   return 1;
111 }
112
113 int 
114 Curl_llist_remove(curl_llist *list, curl_llist_element *e, void *user)
115 {
116   if (e == NULL || list->size == 0)
117     return 1;
118
119   if (e == list->head) {
120     list->head = e->next;
121
122     if (list->head == NULL)
123       list->tail = NULL;
124     else
125       e->next->prev = NULL;
126   } else {
127     e->prev->next = e->next;
128     if (!e->next)
129       list->tail = e->prev;
130     else
131       e->next->prev = e->prev;
132   }
133
134   list->dtor(user, e->ptr);
135   free(e);
136   --list->size;
137
138   return 1;
139 }
140
141 int 
142 Curl_llist_remove_next(curl_llist *list, curl_llist_element *e, void *user)
143 {
144   return Curl_llist_remove(list, e->next, user);
145 }
146
147 int 
148 Curl_llist_remove_prev(curl_llist *list, curl_llist_element *e, void *user)
149 {
150   return Curl_llist_remove(list, e->prev, user);
151 }
152
153 size_t 
154 Curl_llist_count(curl_llist *list)
155 {
156   return list->size;
157 }
158
159 void 
160 Curl_llist_destroy(curl_llist *list, void *user)
161 {
162   if(list) {
163     while (list->size > 0)
164       Curl_llist_remove(list, list->tail, user);
165
166     free(list);
167   }
168 }