4f5607a281e8ce484e2caf19154581a832180c1b
[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    * Number of elements in the list.
69    */
70   unsigned int length;
71 };
72
73
74 /**
75  * Handle to a singly linked list iterator 
76  */
77 struct GNUNET_CONTAINER_SList_Iterator
78 {
79   /**
80    * Linked list that we are iterating over.
81    */
82   struct GNUNET_CONTAINER_SList *list;
83
84   /**
85    * Last element accessed.
86    */
87   struct GNUNET_CONTAINER_SList_Elem *last;
88
89   /**
90    * Current list element.
91    */
92   struct GNUNET_CONTAINER_SList_Elem *elem;
93 };
94
95
96 /**
97  * Create a new element that is to be inserted into the list
98  * @internal
99  * @param disp memory disposition
100  * @param buf payload buffer
101  * @param len length of the buffer
102  * @return a new element
103  */
104 static struct GNUNET_CONTAINER_SList_Elem *
105 create_elem (enum GNUNET_CONTAINER_SListDisposition disp,
106              const void *buf, size_t len)
107 {
108   struct GNUNET_CONTAINER_SList_Elem *e;
109
110   if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
111     {
112       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
113       memcpy (&e[1], buf, len);
114       e->elem = (void *) &e[1];
115     }
116   else
117     {
118       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
119       e->elem = (void *) buf;
120     }
121   e->disp = disp;
122   e->len = len;
123   return e;
124 }
125
126
127 /**
128  * Add a new element to the list
129  * @param l list
130  * @param disp memory disposition
131  * @param buf payload buffer
132  * @param len length of the buffer
133  */
134 void
135 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
136                             enum GNUNET_CONTAINER_SListDisposition disp,
137                             const void *buf, size_t len)
138 {
139   struct GNUNET_CONTAINER_SList_Elem *e;
140
141   e = create_elem (disp, buf, len);
142   e->next = l->head;
143   l->head = e;
144   l->length++;
145 }
146
147
148 /**
149  * Create a new singly linked list
150  * @return the new list
151  */
152 struct GNUNET_CONTAINER_SList *
153 GNUNET_CONTAINER_slist_create ()
154 {
155   return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
156 }
157
158
159 /**
160  * Destroy a singly linked list
161  * @param l the list to be destroyed
162  */
163 void
164 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
165 {
166   GNUNET_CONTAINER_slist_clear (l);
167   GNUNET_free (l);
168 }
169
170
171 /**
172  * Return the beginning of a list
173  * @param l list
174  * @return iterator pointing to the beginning
175  */
176 struct GNUNET_CONTAINER_SList_Iterator *
177 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
178 {
179   struct GNUNET_CONTAINER_SList_Iterator *ret;
180
181   ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
182   ret->elem = l->head;
183   ret->list = l;
184   return ret;
185 }
186
187
188 /**
189  * Clear a list
190  * @param l list
191  */
192 void
193 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
194 {
195   struct GNUNET_CONTAINER_SList_Elem *e;
196   struct GNUNET_CONTAINER_SList_Elem *n;
197
198   e = l->head;
199   while (e != NULL)
200     {
201       n = e->next;
202       if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
203         GNUNET_free (e->elem);
204       GNUNET_free (e);
205       e = n;
206     }
207   l->head = NULL;
208   l->length = 0;
209 }
210
211
212 /**
213  * Check if a list contains a certain element
214  *
215  * @param l list
216  * @param buf payload buffer to find
217  * @param len length of the payload (number of bytes in buf)
218  */
219 int
220 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
221                                  const void *buf, size_t len)
222 {
223   struct GNUNET_CONTAINER_SList_Elem *e;
224
225   for (e = l->head; e != NULL; e = e->next)
226     if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
227       return GNUNET_YES;
228   return GNUNET_NO;
229 }
230
231
232 /**
233  * Count the elements of a list
234  * @param l list
235  * @return number of elements in the list
236  */
237 int
238 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
239 {
240   return l->length;
241 }
242
243
244 /**
245  * Remove an element from the list
246  *
247  * @param i iterator that points to the element to be removed
248  */
249 void
250 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
251 {
252   struct GNUNET_CONTAINER_SList_Elem *next;
253
254   next = i->elem->next;
255   if (i->last != NULL)
256     i->last->next = next;
257   else
258     i->list->head = next;
259   if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
260     GNUNET_free (i->elem->elem);
261   GNUNET_free (i->elem);
262   i->list->length--;
263   i->elem = next;
264 }
265
266
267 /**
268  * Insert an element into a list at a specific position
269  * @param before where to insert the new element
270  * @param disp memory disposition
271  * @param buf payload buffer
272  * @param len length of the payload
273  */
274 void
275 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
276                                enum GNUNET_CONTAINER_SListDisposition disp,
277                                const void *buf, size_t len)
278 {
279   struct GNUNET_CONTAINER_SList_Elem *e;
280
281   e = create_elem (disp, buf, len);
282   e->next = before->elem;
283   if (before->last != NULL)
284     before->last->next = e;
285   else
286     before->list->head = e;
287   before->list->length++;
288 }
289
290
291 /**
292  * Advance an iterator to the next element
293  * @param i iterator
294  * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
295  */
296 int
297 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
298 {
299   i->last = i->elem;
300   i->elem = i->elem->next;
301
302   return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
303 }
304
305
306 /**
307  * Check if an iterator points beyond the end of a list
308  *
309  * @param i iterator
310  * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
311  *         points to a valid element
312  */
313 int
314 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
315 {
316   return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
317 }
318
319
320 /**
321  * Retrieve the element at a specific position in a list
322  * @param i iterator
323  * @param len payload length
324  * @return payload
325  */
326 const void *
327 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
328                             size_t * len)
329 {
330   if (len)
331     *len = i->elem->len;
332   return i->elem->elem;
333 }
334
335 /* end of container_slist.c */