2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file util/container_slist.c
23 * @brief Implementation of a singly-linked list
28 #include "gnunet_container_lib.h"
31 * Element in our linked list.
33 struct GNUNET_CONTAINER_SList_Elem
36 * This is a linked list.
38 struct GNUNET_CONTAINER_SList_Elem *next;
41 * Application data stored at this element.
46 * Number of bytes stored in elem.
51 * Disposition of the element.
53 enum GNUNET_CONTAINER_SListDisposition disp;
58 * Handle to a singly linked list
60 struct GNUNET_CONTAINER_SList
63 * Head of the linked list.
65 struct GNUNET_CONTAINER_SList_Elem *head;
68 * Tail of the linked list.
70 struct GNUNET_CONTAINER_SList_Elem *tail;
73 * Number of elements in the list.
80 * Handle to a singly linked list iterator
82 struct GNUNET_CONTAINER_SList_Iterator
85 * Linked list that we are iterating over.
87 struct GNUNET_CONTAINER_SList *list;
90 * Last element accessed.
92 struct GNUNET_CONTAINER_SList_Elem *last;
95 * Current list element.
97 struct GNUNET_CONTAINER_SList_Elem *elem;
102 * Create a new element that is to be inserted into the list
104 * @param disp memory disposition
105 * @param buf payload buffer
106 * @param len length of the buffer
107 * @return a new element
109 static struct GNUNET_CONTAINER_SList_Elem *
110 create_elem (enum GNUNET_CONTAINER_SListDisposition disp,
111 const void *buf, size_t len)
113 struct GNUNET_CONTAINER_SList_Elem *e;
115 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
117 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
118 memcpy (&e[1], buf, len);
119 e->elem = (void *) &e[1];
123 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
124 e->elem = (void *) buf;
133 * Add a new element to the list
135 * @param disp memory disposition
136 * @param buf payload buffer
137 * @param len length of the buffer
140 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
141 enum GNUNET_CONTAINER_SListDisposition disp,
142 const void *buf, size_t len)
144 struct GNUNET_CONTAINER_SList_Elem *e;
146 e = create_elem (disp, buf, len);
155 * Add a new element to the end of the list
157 * @param disp memory disposition
158 * @param buf payload buffer
159 * @param len length of the buffer
162 GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
163 enum GNUNET_CONTAINER_SListDisposition disp,
164 const void *buf, size_t len)
166 struct GNUNET_CONTAINER_SList_Elem *e;
168 e = create_elem (disp, buf, len);
179 * Append a singly linked list to another
180 * @param dst list to append to
184 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
185 struct GNUNET_CONTAINER_SList *src)
187 struct GNUNET_CONTAINER_SList_Iterator *i;
189 for (i = GNUNET_CONTAINER_slist_begin (src); GNUNET_CONTAINER_slist_end (i) !=
190 GNUNET_YES; GNUNET_CONTAINER_slist_next (i))
193 GNUNET_CONTAINER_slist_add (dst,
195 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ?
196 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC :
197 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
198 i->elem->elem, i->elem->len);
200 GNUNET_CONTAINER_slist_iter_destroy (i);
205 * Create a new singly linked list
206 * @return the new list
208 struct GNUNET_CONTAINER_SList *
209 GNUNET_CONTAINER_slist_create ()
211 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
216 * Destroy a singly linked list
217 * @param l the list to be destroyed
220 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
222 GNUNET_CONTAINER_slist_clear (l);
228 * Return the beginning of a list
230 * @return iterator pointing to the beginning
232 struct GNUNET_CONTAINER_SList_Iterator *
233 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
235 struct GNUNET_CONTAINER_SList_Iterator *ret;
237 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
249 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
251 struct GNUNET_CONTAINER_SList_Elem *e;
252 struct GNUNET_CONTAINER_SList_Elem *n;
258 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
259 GNUNET_free (e->elem);
270 * Check if a list contains a certain element
273 * @param buf payload buffer to find
274 * @param len length of the payload (number of bytes in buf)
277 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
278 const void *buf, size_t len)
280 struct GNUNET_CONTAINER_SList_Elem *e;
282 for (e = l->head; e != NULL; e = e->next)
283 if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
290 * Count the elements of a list
292 * @return number of elements in the list
295 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
302 * Remove an element from the list
304 * @param i iterator that points to the element to be removed
307 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
309 struct GNUNET_CONTAINER_SList_Elem *next;
311 next = i->elem->next;
313 i->last->next = next;
315 i->list->head = next;
317 i->list->tail = i->last;
318 if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
319 GNUNET_free (i->elem->elem);
320 GNUNET_free (i->elem);
327 * Insert an element into a list at a specific position
328 * @param before where to insert the new element
329 * @param disp memory disposition
330 * @param buf payload buffer
331 * @param len length of the payload
334 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
335 enum GNUNET_CONTAINER_SListDisposition disp,
336 const void *buf, size_t len)
338 struct GNUNET_CONTAINER_SList_Elem *e;
340 e = create_elem (disp, buf, len);
341 e->next = before->elem;
342 if (before->last != NULL)
343 before->last->next = e;
345 before->list->head = e;
347 before->list->tail = e;
348 before->list->length++;
353 * Advance an iterator to the next element
355 * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
358 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
361 i->elem = i->elem->next;
363 return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
368 * Check if an iterator points beyond the end of a list
371 * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
372 * points to a valid element
375 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
377 return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
382 * Retrieve the element at a specific position in a list
384 * @param len payload length
388 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
393 return i->elem->elem;
397 * Release an iterator
401 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i)
406 /* end of container_slist.c */