Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl / llist.c
blob99048d6c57da8cb4f41ac721ca6fa61bd3b11c8f
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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.2 2007/03/15 19:22:13 andy 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;