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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/*
This file is part of GNUnet.
(C) 2009 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file util/container_slist.c
* @brief Implementation of a singly-linked list
* @author Nils Durner
*/
#include "platform.h"
#include "gnunet_container_lib.h"
#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
/**
* Element in our linked list.
*/
struct GNUNET_CONTAINER_SList_Elem
{
/**
* This is a linked list.
*/
struct GNUNET_CONTAINER_SList_Elem *next;
/**
* Application data stored at this element.
*/
void *elem;
/**
* Number of bytes stored in elem.
*/
size_t len;
/**
* Disposition of the element.
*/
enum GNUNET_CONTAINER_SListDisposition disp;
};
/**
* Handle to a singly linked list
*/
struct GNUNET_CONTAINER_SList
{
/**
* Head of the linked list.
*/
struct GNUNET_CONTAINER_SList_Elem *head;
/**
* Tail of the linked list.
*/
struct GNUNET_CONTAINER_SList_Elem *tail;
/**
* Number of elements in the list.
*/
unsigned int length;
};
/**
* Create a new element that is to be inserted into the list
* @internal
* @param disp memory disposition
* @param buf payload buffer
* @param len length of the buffer
* @return a new element
*/
static struct GNUNET_CONTAINER_SList_Elem *
create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf,
size_t len)
{
struct GNUNET_CONTAINER_SList_Elem *e;
if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
{
e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
memcpy (&e[1], buf, len);
e->elem = (void *) &e[1];
}
else
{
e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
e->elem = (void *) buf;
}
e->disp = disp;
e->len = len;
return e;
}
/**
* Add a new element to the list
* @param l list
* @param disp memory disposition
* @param buf payload buffer
* @param len length of the buffer
*/
void
GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
enum GNUNET_CONTAINER_SListDisposition disp,
const void *buf, size_t len)
{
struct GNUNET_CONTAINER_SList_Elem *e;
e = create_elem (disp, buf, len);
e->next = l->head;
l->head = e;
if (l->tail == NULL)
l->tail = e;
l->length++;
}
/**
* Add a new element to the end of the list
* @param l list
* @param disp memory disposition
* @param buf payload buffer
* @param len length of the buffer
*/
void
GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
enum GNUNET_CONTAINER_SListDisposition disp,
const void *buf, size_t len)
{
struct GNUNET_CONTAINER_SList_Elem *e;
e = create_elem (disp, buf, len);
if (l->tail != NULL)
l->tail->next = e;
if (l->head == NULL)
l->head = e;
l->tail = e;
l->length++;
}
/**
* Append a singly linked list to another
* @param dst list to append to
* @param src source
*/
void
GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
struct GNUNET_CONTAINER_SList *src)
{
struct GNUNET_CONTAINER_SList_Iterator i;
for (i = GNUNET_CONTAINER_slist_begin (src);
GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
GNUNET_CONTAINER_slist_next (&i))
{
GNUNET_CONTAINER_slist_add (dst,
(i.elem->disp ==
GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ?
GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC :
GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
i.elem->elem, i.elem->len);
}
GNUNET_CONTAINER_slist_iter_destroy (&i);
}
/**
* Create a new singly linked list
* @return the new list
*/
struct GNUNET_CONTAINER_SList *
GNUNET_CONTAINER_slist_create ()
{
return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
}
/**
* Destroy a singly linked list
* @param l the list to be destroyed
*/
void
GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
{
GNUNET_CONTAINER_slist_clear (l);
GNUNET_free (l);
}
/**
* Return the beginning of a list
* @param l list
* @return iterator pointing to the beginning
*/
struct GNUNET_CONTAINER_SList_Iterator
GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
{
struct GNUNET_CONTAINER_SList_Iterator ret;
memset (&ret, 0, sizeof (ret));
ret.elem = l->head;
ret.list = l;
return ret;
}
/**
* Clear a list
* @param l list
*/
void
GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
{
struct GNUNET_CONTAINER_SList_Elem *e;
struct GNUNET_CONTAINER_SList_Elem *n;
e = l->head;
while (e != NULL)
{
n = e->next;
if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
GNUNET_free (e->elem);
GNUNET_free (e);
e = n;
}
l->head = NULL;
l->tail = NULL;
l->length = 0;
}
/**
* Check if a list contains a certain element
* @param l list
* @param buf payload buffer to find
* @param len length of the payload (number of bytes in buf)
*
* @return GNUNET_YES if found, GNUNET_NO otherwise
*/
int
GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
const void *buf, size_t len)
{
struct GNUNET_CONTAINER_SList_Elem *e;
for (e = l->head; e != NULL; e = e->next)
if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
return GNUNET_YES;
return GNUNET_NO;
}
typedef int (*Comparator)(const void *, size_t, const void *, size_t);
/**
* Check if a list contains a certain element
*
* @param l list
* @param buf payload buffer to find
* @param len length of the payload (number of bytes in buf)
* @param compare comparison function, should return 0 if compared elements match
*
* @return NULL if the 'buf' could not be found, pointer to the
* list element, if found
*/
void *
GNUNET_CONTAINER_slist_contains2 (const struct GNUNET_CONTAINER_SList *l,
const void *buf, size_t len,
Comparator compare)
{
struct GNUNET_CONTAINER_SList_Elem *e;
for (e = l->head; e != NULL; e = e->next)
if ((e->len == len) && (*compare)(buf, len, e->elem, e->len) == 0)
return e->elem;
return NULL;
}
/**
* Count the elements of a list
* @param l list
* @return number of elements in the list
*/
int
GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
{
return l->length;
}
/**
* Remove an element from the list
*
* @param i iterator that points to the element to be removed
*/
void
GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
{
struct GNUNET_CONTAINER_SList_Elem *next;
next = i->elem->next;
if (i->last != NULL)
i->last->next = next;
else
i->list->head = next;
if (next == NULL)
i->list->tail = i->last;
if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
GNUNET_free (i->elem->elem);
GNUNET_free (i->elem);
i->list->length--;
i->elem = next;
}
/**
* Insert an element into a list at a specific position
* @param before where to insert the new element
* @param disp memory disposition
* @param buf payload buffer
* @param len length of the payload
*/
void
GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
enum GNUNET_CONTAINER_SListDisposition disp,
const void *buf, size_t len)
{
struct GNUNET_CONTAINER_SList_Elem *e;
e = create_elem (disp, buf, len);
e->next = before->elem;
if (before->last != NULL)
before->last->next = e;
else
before->list->head = e;
if (e->next == NULL)
before->list->tail = e;
before->list->length++;
}
/**
* Advance an iterator to the next element
* @param i iterator
* @return GNUNET_YES on success, GNUNET_NO if the end has been reached
*/
int
GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
{
i->last = i->elem;
i->elem = i->elem->next;
return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
}
/**
* Check if an iterator points beyond the end of a list
*
* @param i iterator
* @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
* points to a valid element
*/
int
GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
{
return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
}
/**
* Retrieve the element at a specific position in a list
* @param i iterator
* @param len payload length
* @return payload
*/
void *
GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
size_t * len)
{
if (len)
*len = i->elem->len;
return i->elem->elem;
}
/**
* Release an iterator
* @param i iterator
*/
void
GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i)
{
}
/* end of container_slist.c */
|