-
Notifications
You must be signed in to change notification settings - Fork 1
/
vlist.c
250 lines (208 loc) · 4.29 KB
/
vlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* vlist.c
*
* "void linked list" structure and helper functions
*
* (C)1999 Stefano Busti
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#if defined(__WIN32__)
# include <windows.h>
#endif
#include "vlist.h"
/*
* Initializes a list, 2nd parameter is a pointer to
* a function which will be used to free the items
* if the list is an owner list.
* Call this once just after creating the list.
*/
int vlist_init(vlist_s *st, vlist_destroy_func_t destroyitem)
{
st->head = NULL;
st->tail = NULL;
st->count = 0;
st->owner = 1;
st->destroyitem = destroyitem;
return 0;
}
/*
* Cleans up a list - call this when you've finished
* with the list.
*/
void vlist_cleanup(vlist_s *st)
{
vlist_clear(st);
}
/*
* Removes all the items in the list. If the list is
* an owner list, item data is also freed.
*/
void vlist_clear(vlist_s *st)
{
vlist_i *it = st->head;
while(it)
{
vlist_delete(st, it);
it = st->head;
}
}
vlist_i *vlist_addfirst(vlist_s *st, void *data)
{
vlist_i *it = malloc(sizeof(vlist_i));
if (!it)
return NULL;
it->data = data;
it->prev = NULL;
it->next = NULL;
st->head = it;
st->tail = it;
st->count = 1;
return it;
}
/*
* Adds a new item after item it, using the data 'data'
*/
vlist_i *vlist_add(vlist_s *st, vlist_i *it, void *data)
{
vlist_i *nextit;
/* handle special case where list is empty */
if (st->count == 0)
{
return vlist_addfirst(st, data);
}
/* grab a pointer to the next item */
nextit = it->next;
/* create our new item */
it->next = malloc(sizeof(vlist_i));
if (!it->next)
return NULL;
/* set up the new item */
it->next->data = data;
it->next->prev = it;
it->next->next = nextit;
if (nextit)
{
/* link the next item to the new one */
nextit->prev = it->next;
}
else
{
/* update tail */
st->tail = it->next;
}
/* add one to item count */
st->count++;
return it->next;
}
/*
* Inserts a new item before item it, using the data 'data'
*/
vlist_i *vlist_insert(vlist_s *st, vlist_i *it, void *data)
{
vlist_i *previt;
/* handle special case where list is empty */
if (st->count == 0)
{
return vlist_addfirst(st, data);
}
/* grab a pointer to the previous item */
previt = it->prev;
/* create our new item */
it->prev = malloc(sizeof(vlist_i));
if (!it->prev)
return NULL;
/* set up the new item */
it->prev->data = data;
it->prev->prev = previt;
it->prev->next = it;
if (previt)
{
/* link the previous item to the new one */
previt->next = it->prev;
}
else
{
/* update head */
st->head = it->prev;
}
/* add one to item count */
st->count++;
return it->prev;
}
/*
* Removes the item 'it' from the list. If the list is
* an owner list, item data is also freed.
*/
void vlist_delete(vlist_s *st, vlist_i *it)
{
/* call destroyitem() if we're the owner */
if (st->owner) {
st->destroyitem(it->data);
it->data = NULL;
}
if (it->next)
it->next->prev = it->prev;
else
st->tail = it->prev;
if (it->prev)
it->prev->next = it->next;
else
st->head = it->next;
free(it);
st->count--;
}
/*
* Returns the item data for item it
*/
void *vlist_get(vlist_i *it)
{
return it->data;
}
/*
* Searches the list forwards for 'data',
* starting at item 'it'.
* Returns NULL if not found.
*/
vlist_i *vlist_findfwd(vlist_i *it, void *data)
{
vlist_i *item;
/* iterate forwards searching for data */
for (item = it; item; item = item->next)
if (item->data == data)
return item;
/* not found */
return NULL;
}
/*
* Searches the list backwards for 'data',
* starting at item 'it'.
* Returns NULL if not found.
*/
vlist_i *vlist_findrev(vlist_i *it, void *data)
{
vlist_i *item;
/* iterate backwards searching for data */
for (item = it; item; item = item->prev)
if (item->data == data)
return item;
/* not found */
return NULL;
}
void vlist_debug(vlist_s *st, FILE *stream)
{
vlist_i *it;
fprintf(stream, "list with %d items\n", st->count);
fprintf(stream, "head = 0x%08lx\n", (u_long)st->head);
fprintf(stream, "tail = 0x%08lx\n", (u_long)st->tail);
fprintf(stream, "items are:\n{\n");
for (it = st->head; it; it = it->next)
{
fprintf(stream, "\t0x%08lx << [ 0x%08lx ] >> 0x%08lx (data:0x%08lx)\n",
(u_long)it->prev, (u_long)it,
(u_long)it->next, (u_long)it->data);
}
fprintf(stream, "}\n\n");
}