finishing gnunet-nat-server
[oweals/gnunet.git] / src / util / container_slist.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/container_slist.c
23  * @brief Implementation of a singly-linked list
24  * @author Nils Durner
25  */
26
27 #include "platform.h"
28 #include "gnunet_container_lib.h"
29
30 /**
31  * Element in our linked list.
32  */
33 struct GNUNET_CONTAINER_SList_Elem
34 {
35   /**
36    * This is a linked list.
37    */
38   struct GNUNET_CONTAINER_SList_Elem *next;
39
40   /**
41    * Application data stored at this element.
42    */
43   void *elem;
44
45   /**
46    * Number of bytes stored in elem.
47    */
48   size_t len;
49
50   /**
51    * Disposition of the element.
52    */
53   enum GNUNET_CONTAINER_SListDisposition disp;
54 };
55
56
57 /**
58  * Handle to a singly linked list  
59  */
60 struct GNUNET_CONTAINER_SList
61 {
62   /**
63    * Head of the linked list.
64    */
65   struct GNUNET_CONTAINER_SList_Elem *head;
66
67   /**
68    * Tail of the linked list.
69    */
70   struct GNUNET_CONTAINER_SList_Elem *tail;
71
72   /**
73    * Number of elements in the list.
74    */
75   unsigned int length;
76 };
77
78
79 /**
80  * Handle to a singly linked list iterator 
81  */
82 struct GNUNET_CONTAINER_SList_Iterator
83 {
84   /**
85    * Linked list that we are iterating over.
86    */
87   struct GNUNET_CONTAINER_SList *list;
88
89   /**
90    * Last element accessed.
91    */
92   struct GNUNET_CONTAINER_SList_Elem *last;
93
94   /**
95    * Current list element.
96    */
97   struct GNUNET_CONTAINER_SList_Elem *elem;
98 };
99
100
101 /**
102  * Create a new element that is to be inserted into the list
103  * @internal
104  * @param disp memory disposition
105  * @param buf payload buffer
106  * @param len length of the buffer
107  * @return a new element
108  */
109 static struct GNUNET_CONTAINER_SList_Elem *
110 create_elem (enum GNUNET_CONTAINER_SListDisposition disp,
111              const void *buf, size_t len)
112 {
113   struct GNUNET_CONTAINER_SList_Elem *e;
114
115   if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
116     {
117       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
118       memcpy (&e[1], buf, len);
119       e->elem = (void *) &e[1];
120     }
121   else
122     {
123       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
124       e->elem = (void *) buf;
125     }
126   e->disp = disp;
127   e->len = len;
128   return e;
129 }
130
131
132 /**
133  * Add a new element to the list
134  * @param l list
135  * @param disp memory disposition
136  * @param buf payload buffer
137  * @param len length of the buffer
138  */
139 void
140 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
141                             enum GNUNET_CONTAINER_SListDisposition disp,
142                             const void *buf, size_t len)
143 {
144   struct GNUNET_CONTAINER_SList_Elem *e;
145
146   e = create_elem (disp, buf, len);
147   e->next = l->head;
148   l->head = e;
149   if (l->tail == NULL) l->tail = e;
150   l->length++;
151 }
152
153 /**
154  * Add a new element to the end of the list
155  * @param l list
156  * @param disp memory disposition
157  * @param buf payload buffer
158  * @param len length of the buffer
159  */
160 void
161 GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
162                             enum GNUNET_CONTAINER_SListDisposition disp,
163                             const void *buf, size_t len)
164 {
165   struct GNUNET_CONTAINER_SList_Elem *e;
166
167   e = create_elem (disp, buf, len);
168   if (l->tail != NULL)
169     l->tail->next = e;
170   if (l->head == NULL)
171     l->head = e;
172   l->tail = e;
173   l->length++;
174 }
175
176
177 /**
178  * Append a singly linked list to another
179  * @param dst list to append to
180  * @param src source
181  */
182 void
183 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst, struct GNUNET_CONTAINER_SList *src)
184 {
185   struct GNUNET_CONTAINER_SList_Iterator *i;
186
187   for (i = GNUNET_CONTAINER_slist_begin (src); GNUNET_CONTAINER_slist_end (i) !=
188       GNUNET_YES; GNUNET_CONTAINER_slist_next (i))
189
190     {
191       GNUNET_CONTAINER_slist_add (dst,
192           (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC
193               : GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, i->elem->elem,
194           i->elem->len);
195     }
196   GNUNET_CONTAINER_slist_iter_destroy (i);
197 }
198
199
200 /**
201  * Create a new singly linked list
202  * @return the new list
203  */
204 struct GNUNET_CONTAINER_SList *
205 GNUNET_CONTAINER_slist_create ()
206 {
207   return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
208 }
209
210
211 /**
212  * Destroy a singly linked list
213  * @param l the list to be destroyed
214  */
215 void
216 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
217 {
218   GNUNET_CONTAINER_slist_clear (l);
219   GNUNET_free (l);
220 }
221
222
223 /**
224  * Return the beginning of a list
225  * @param l list
226  * @return iterator pointing to the beginning
227  */
228 struct GNUNET_CONTAINER_SList_Iterator *
229 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
230 {
231   struct GNUNET_CONTAINER_SList_Iterator *ret;
232
233   ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
234   ret->elem = l->head;
235   ret->list = l;
236   return ret;
237 }
238
239
240 /**
241  * Clear a list
242  * @param l list
243  */
244 void
245 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
246 {
247   struct GNUNET_CONTAINER_SList_Elem *e;
248   struct GNUNET_CONTAINER_SList_Elem *n;
249
250   e = l->head;
251   while (e != NULL)
252     {
253       n = e->next;
254       if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
255         GNUNET_free (e->elem);
256       GNUNET_free (e);
257       e = n;
258     }
259   l->head = NULL;
260   l->tail = NULL;
261   l->length = 0;
262 }
263
264
265 /**
266  * Check if a list contains a certain element
267  *
268  * @param l list
269  * @param buf payload buffer to find
270  * @param len length of the payload (number of bytes in buf)
271  */
272 int
273 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
274                                  const void *buf, size_t len)
275 {
276   struct GNUNET_CONTAINER_SList_Elem *e;
277
278   for (e = l->head; e != NULL; e = e->next)
279     if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
280       return GNUNET_YES;
281   return GNUNET_NO;
282 }
283
284
285 /**
286  * Count the elements of a list
287  * @param l list
288  * @return number of elements in the list
289  */
290 int
291 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
292 {
293   return l->length;
294 }
295
296
297 /**
298  * Remove an element from the list
299  *
300  * @param i iterator that points to the element to be removed
301  */
302 void
303 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
304 {
305   struct GNUNET_CONTAINER_SList_Elem *next;
306
307   next = i->elem->next;
308   if (i->last != NULL)
309     i->last->next = next;
310   else
311     i->list->head = next;
312   if (next == NULL)
313     i->list->tail = i->last;
314   if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
315     GNUNET_free (i->elem->elem);
316   GNUNET_free (i->elem);
317   i->list->length--;
318   i->elem = next;
319 }
320
321
322 /**
323  * Insert an element into a list at a specific position
324  * @param before where to insert the new element
325  * @param disp memory disposition
326  * @param buf payload buffer
327  * @param len length of the payload
328  */
329 void
330 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
331                                enum GNUNET_CONTAINER_SListDisposition disp,
332                                const void *buf, size_t len)
333 {
334   struct GNUNET_CONTAINER_SList_Elem *e;
335
336   e = create_elem (disp, buf, len);
337   e->next = before->elem;
338   if (before->last != NULL)
339     before->last->next = e;
340   else
341     before->list->head = e;
342   if (e->next == NULL)
343     before->list->tail = e;
344   before->list->length++;
345 }
346
347
348 /**
349  * Advance an iterator to the next element
350  * @param i iterator
351  * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
352  */
353 int
354 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
355 {
356   i->last = i->elem;
357   i->elem = i->elem->next;
358
359   return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
360 }
361
362
363 /**
364  * Check if an iterator points beyond the end of a list
365  *
366  * @param i iterator
367  * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
368  *         points to a valid element
369  */
370 int
371 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
372 {
373   return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
374 }
375
376
377 /**
378  * Retrieve the element at a specific position in a list
379  * @param i iterator
380  * @param len payload length
381  * @return payload
382  */
383 const void *
384 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
385                             size_t * len)
386 {
387   if (len)
388     *len = i->elem->len;
389   return i->elem->elem;
390 }
391
392 /**
393  * Release an iterator
394  * @param i iterator
395  */
396 void
397 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator
398                                      *i)
399 {
400   GNUNET_free (i);
401 }
402
403 /* end of container_slist.c */