LRN loves slist: Use stack allocation for slist iterator
[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 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
31
32 /**
33  * Element in our linked list.
34  */
35 struct GNUNET_CONTAINER_SList_Elem
36 {
37   /**
38    * This is a linked list.
39    */
40   struct GNUNET_CONTAINER_SList_Elem *next;
41
42   /**
43    * Application data stored at this element.
44    */
45   void *elem;
46
47   /**
48    * Number of bytes stored in elem.
49    */
50   size_t len;
51
52   /**
53    * Disposition of the element.
54    */
55   enum GNUNET_CONTAINER_SListDisposition disp;
56 };
57
58
59 /**
60  * Handle to a singly linked list
61  */
62 struct GNUNET_CONTAINER_SList
63 {
64   /**
65    * Head of the linked list.
66    */
67   struct GNUNET_CONTAINER_SList_Elem *head;
68
69   /**
70    * Tail of the linked list.
71    */
72   struct GNUNET_CONTAINER_SList_Elem *tail;
73
74   /**
75    * Number of elements in the list.
76    */
77   unsigned int length;
78 };
79
80
81
82 /**
83  * Create a new element that is to be inserted into the list
84  * @internal
85  * @param disp memory disposition
86  * @param buf payload buffer
87  * @param len length of the buffer
88  * @return a new element
89  */
90 static struct GNUNET_CONTAINER_SList_Elem *
91 create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf,
92              size_t len)
93 {
94   struct GNUNET_CONTAINER_SList_Elem *e;
95
96   if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
97     {
98       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
99       memcpy (&e[1], buf, len);
100       e->elem = (void *) &e[1];
101     }
102   else
103     {
104       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
105       e->elem = (void *) buf;
106     }
107   e->disp = disp;
108   e->len = len;
109   return e;
110 }
111
112
113 /**
114  * Add a new element to the list
115  * @param l list
116  * @param disp memory disposition
117  * @param buf payload buffer
118  * @param len length of the buffer
119  */
120 void
121 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
122                             enum GNUNET_CONTAINER_SListDisposition disp,
123                             const void *buf, size_t len)
124 {
125   struct GNUNET_CONTAINER_SList_Elem *e;
126
127   e = create_elem (disp, buf, len);
128   e->next = l->head;
129   l->head = e;
130   if (l->tail == NULL)
131     l->tail = e;
132   l->length++;
133 }
134
135 /**
136  * Add a new element to the end of the list
137  * @param l list
138  * @param disp memory disposition
139  * @param buf payload buffer
140  * @param len length of the buffer
141  */
142 void
143 GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
144                                 enum GNUNET_CONTAINER_SListDisposition disp,
145                                 const void *buf, size_t len)
146 {
147   struct GNUNET_CONTAINER_SList_Elem *e;
148
149   e = create_elem (disp, buf, len);
150   if (l->tail != NULL)
151     l->tail->next = e;
152   if (l->head == NULL)
153     l->head = e;
154   l->tail = e;
155   l->length++;
156 }
157
158
159 /**
160  * Append a singly linked list to another
161  * @param dst list to append to
162  * @param src source
163  */
164 void
165 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
166                                struct GNUNET_CONTAINER_SList *src)
167 {
168   struct GNUNET_CONTAINER_SList_Iterator i;
169
170   for (i = GNUNET_CONTAINER_slist_begin (src);
171        GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
172        GNUNET_CONTAINER_slist_next (&i))
173
174     {
175       GNUNET_CONTAINER_slist_add (dst,
176                                   (i.elem->disp ==
177                                    GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC)
178                                   ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC
179                                   :
180                                   GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
181                                   i.elem->elem, i.elem->len);
182     }
183   GNUNET_CONTAINER_slist_iter_destroy (&i);
184 }
185
186
187 /**
188  * Create a new singly linked list
189  * @return the new list
190  */
191 struct GNUNET_CONTAINER_SList *
192 GNUNET_CONTAINER_slist_create ()
193 {
194   return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
195 }
196
197
198 /**
199  * Destroy a singly linked list
200  * @param l the list to be destroyed
201  */
202 void
203 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
204 {
205   GNUNET_CONTAINER_slist_clear (l);
206   GNUNET_free (l);
207 }
208
209
210 /**
211  * Return the beginning of a list
212  * @param l list
213  * @return iterator pointing to the beginning
214  */
215 struct GNUNET_CONTAINER_SList_Iterator
216 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
217 {
218   struct GNUNET_CONTAINER_SList_Iterator ret;
219
220   memset (&ret, 0, sizeof (ret));
221   ret.elem = l->head;
222   ret.list = l;
223   return ret;
224 }
225
226
227 /**
228  * Clear a list
229  * @param l list
230  */
231 void
232 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
233 {
234   struct GNUNET_CONTAINER_SList_Elem *e;
235   struct GNUNET_CONTAINER_SList_Elem *n;
236
237   e = l->head;
238   while (e != NULL)
239     {
240       n = e->next;
241       if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
242         GNUNET_free (e->elem);
243       GNUNET_free (e);
244       e = n;
245     }
246   l->head = NULL;
247   l->tail = NULL;
248   l->length = 0;
249 }
250
251
252 /**
253  * Check if a list contains a certain element
254  *
255  * @param l list
256  * @param buf payload buffer to find
257  * @param len length of the payload (number of bytes in buf)
258  */
259 int
260 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
261                                  const void *buf, size_t len)
262 {
263   struct GNUNET_CONTAINER_SList_Elem *e;
264
265   for (e = l->head; e != NULL; e = e->next)
266     if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
267       return GNUNET_YES;
268   return GNUNET_NO;
269 }
270
271
272 /**
273  * Count the elements of a list
274  * @param l list
275  * @return number of elements in the list
276  */
277 int
278 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
279 {
280   return l->length;
281 }
282
283
284 /**
285  * Remove an element from the list
286  *
287  * @param i iterator that points to the element to be removed
288  */
289 void
290 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
291 {
292   struct GNUNET_CONTAINER_SList_Elem *next;
293
294   next = i->elem->next;
295   if (i->last != NULL)
296     i->last->next = next;
297   else
298     i->list->head = next;
299   if (next == NULL)
300     i->list->tail = i->last;
301   if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
302     GNUNET_free (i->elem->elem);
303   GNUNET_free (i->elem);
304   i->list->length--;
305   i->elem = next;
306 }
307
308
309 /**
310  * Insert an element into a list at a specific position
311  * @param before where to insert the new element
312  * @param disp memory disposition
313  * @param buf payload buffer
314  * @param len length of the payload
315  */
316 void
317 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
318                                enum GNUNET_CONTAINER_SListDisposition disp,
319                                const void *buf, size_t len)
320 {
321   struct GNUNET_CONTAINER_SList_Elem *e;
322
323   e = create_elem (disp, buf, len);
324   e->next = before->elem;
325   if (before->last != NULL)
326     before->last->next = e;
327   else
328     before->list->head = e;
329   if (e->next == NULL)
330     before->list->tail = e;
331   before->list->length++;
332 }
333
334
335 /**
336  * Advance an iterator to the next element
337  * @param i iterator
338  * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
339  */
340 int
341 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
342 {
343   i->last = i->elem;
344   i->elem = i->elem->next;
345
346   return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
347 }
348
349
350 /**
351  * Check if an iterator points beyond the end of a list
352  *
353  * @param i iterator
354  * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
355  *         points to a valid element
356  */
357 int
358 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
359 {
360   return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
361 }
362
363
364 /**
365  * Retrieve the element at a specific position in a list
366  * @param i iterator
367  * @param len payload length
368  * @return payload
369  */
370 void *
371 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
372                             size_t * len)
373 {
374   if (len)
375     *len = i->elem->len;
376   return i->elem->elem;
377 }
378
379 /**
380  * Release an iterator
381  * @param i iterator
382  */
383 void
384 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator
385                                      *i)
386 {
387 }
388
389 /* end of container_slist.c */