7b85dc87760780239eeab9720e1e030f64cc97fb
[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                                 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
180                                 i.elem->elem, i.elem->len);
181   }
182   GNUNET_CONTAINER_slist_iter_destroy (&i);
183 }
184
185
186 /**
187  * Create a new singly linked list
188  * @return the new list
189  */
190 struct GNUNET_CONTAINER_SList *
191 GNUNET_CONTAINER_slist_create ()
192 {
193   return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
194 }
195
196
197 /**
198  * Destroy a singly linked list
199  * @param l the list to be destroyed
200  */
201 void
202 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
203 {
204   GNUNET_CONTAINER_slist_clear (l);
205   GNUNET_free (l);
206 }
207
208
209 /**
210  * Return the beginning of a list
211  * @param l list
212  * @return iterator pointing to the beginning
213  */
214 struct GNUNET_CONTAINER_SList_Iterator
215 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
216 {
217   struct GNUNET_CONTAINER_SList_Iterator ret;
218
219   memset (&ret, 0, sizeof (ret));
220   ret.elem = l->head;
221   ret.list = l;
222   return ret;
223 }
224
225
226 /**
227  * Clear a list
228  * @param l list
229  */
230 void
231 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
232 {
233   struct GNUNET_CONTAINER_SList_Elem *e;
234   struct GNUNET_CONTAINER_SList_Elem *n;
235
236   e = l->head;
237   while (e != NULL)
238   {
239     n = e->next;
240     if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
241       GNUNET_free (e->elem);
242     GNUNET_free (e);
243     e = n;
244   }
245   l->head = NULL;
246   l->tail = NULL;
247   l->length = 0;
248 }
249
250
251 /**
252  * Check if a list contains a certain element
253  *
254  * @param l list
255  * @param buf payload buffer to find
256  * @param len length of the payload (number of bytes in buf)
257  */
258 int
259 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
260                                  const void *buf, size_t len)
261 {
262   struct GNUNET_CONTAINER_SList_Elem *e;
263
264   for (e = l->head; e != NULL; e = e->next)
265     if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
266       return GNUNET_YES;
267   return GNUNET_NO;
268 }
269
270
271 /**
272  * Count the elements of a list
273  * @param l list
274  * @return number of elements in the list
275  */
276 int
277 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
278 {
279   return l->length;
280 }
281
282
283 /**
284  * Remove an element from the list
285  *
286  * @param i iterator that points to the element to be removed
287  */
288 void
289 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
290 {
291   struct GNUNET_CONTAINER_SList_Elem *next;
292
293   next = i->elem->next;
294   if (i->last != NULL)
295     i->last->next = next;
296   else
297     i->list->head = next;
298   if (next == NULL)
299     i->list->tail = i->last;
300   if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
301     GNUNET_free (i->elem->elem);
302   GNUNET_free (i->elem);
303   i->list->length--;
304   i->elem = next;
305 }
306
307
308 /**
309  * Insert an element into a list at a specific position
310  * @param before where to insert the new element
311  * @param disp memory disposition
312  * @param buf payload buffer
313  * @param len length of the payload
314  */
315 void
316 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
317                                enum GNUNET_CONTAINER_SListDisposition disp,
318                                const void *buf, size_t len)
319 {
320   struct GNUNET_CONTAINER_SList_Elem *e;
321
322   e = create_elem (disp, buf, len);
323   e->next = before->elem;
324   if (before->last != NULL)
325     before->last->next = e;
326   else
327     before->list->head = e;
328   if (e->next == NULL)
329     before->list->tail = e;
330   before->list->length++;
331 }
332
333
334 /**
335  * Advance an iterator to the next element
336  * @param i iterator
337  * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
338  */
339 int
340 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
341 {
342   i->last = i->elem;
343   i->elem = i->elem->next;
344
345   return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
346 }
347
348
349 /**
350  * Check if an iterator points beyond the end of a list
351  *
352  * @param i iterator
353  * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
354  *         points to a valid element
355  */
356 int
357 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
358 {
359   return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
360 }
361
362
363 /**
364  * Retrieve the element at a specific position in a list
365  * @param i iterator
366  * @param len payload length
367  * @return payload
368  */
369 void *
370 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
371                             size_t * len)
372 {
373   if (len)
374     *len = i->elem->len;
375   return i->elem->elem;
376 }
377
378 /**
379  * Release an iterator
380  * @param i iterator
381  */
382 void
383 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i)
384 {
385 }
386
387 /* end of container_slist.c */