~ubuntu-branches/ubuntu/quantal/curl/quantal

« back to all changes in this revision

Viewing changes to lib/llist.c

  • Committer: Bazaar Package Importer
  • Author(s): Domenico Andreoli
  • Date: 2002-03-12 19:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20020312190621-iqx7k9cipo5d0ifr
Tags: upstream-7.9.5
ImportĀ upstreamĀ versionĀ 7.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 *                                  _   _ ____  _     
 
3
 *  Project                     ___| | | |  _ \| |    
 
4
 *                             / __| | | | |_) | |    
 
5
 *                            | (__| |_| |  _ <| |___ 
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 2002, Daniel Stenberg, <daniel@haxx.se>, et al
 
9
 *
 
10
 * In order to be useful for every potential user, curl and libcurl are
 
11
 * dual-licensed under the MPL and the MIT/X-derivate licenses.
 
12
 *
 
13
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 
14
 * copies of the Software, and permit persons to whom the Software is
 
15
 * furnished to do so, under the terms of the MPL or the MIT/X-derivate
 
16
 * licenses. You may pick one of these licenses.
 
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.3 2002/02/17 14:55:35 bagder Exp $
 
22
 *****************************************************************************/
 
23
 
 
24
#include "setup.h"
 
25
 
 
26
#include <string.h>
 
27
#include <stdlib.h>
 
28
 
 
29
#include "llist.h"
 
30
 
 
31
#ifdef MALLOCDEBUG
 
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
  while (list->size > 0) {
 
163
    curl_llist_remove(list, CURL_LLIST_TAIL(list), user);
 
164
  }
 
165
 
 
166
  free(list);
 
167
  list = NULL;
 
168
}