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 * Number of elements in the list.
75 * Handle to a singly linked list iterator
77 struct GNUNET_CONTAINER_SList_Iterator
80 * Linked list that we are iterating over.
82 struct GNUNET_CONTAINER_SList *list;
85 * Last element accessed.
87 struct GNUNET_CONTAINER_SList_Elem *last;
90 * Current list element.
92 struct GNUNET_CONTAINER_SList_Elem *elem;
97 * Create a new element that is to be inserted into the list
99 * @param disp memory disposition
100 * @param buf payload buffer
101 * @param len length of the buffer
102 * @return a new element
104 static struct GNUNET_CONTAINER_SList_Elem *
105 create_elem (enum GNUNET_CONTAINER_SListDisposition disp,
106 const void *buf, size_t len)
108 struct GNUNET_CONTAINER_SList_Elem *e;
110 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
112 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
113 memcpy (&e[1], buf, len);
114 e->elem = (void *) &e[1];
118 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
119 e->elem = (void *) buf;
128 * Add a new element to the list
130 * @param disp memory disposition
131 * @param buf payload buffer
132 * @param len length of the buffer
135 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
136 enum GNUNET_CONTAINER_SListDisposition disp,
137 const void *buf, size_t len)
139 struct GNUNET_CONTAINER_SList_Elem *e;
141 e = create_elem (disp, buf, len);
149 * Append a singly linked list to another
150 * @param dst list to append to
154 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst, struct GNUNET_CONTAINER_SList *src)
156 struct GNUNET_CONTAINER_SList_Iterator *i;
158 for (i = GNUNET_CONTAINER_slist_begin (src); GNUNET_CONTAINER_slist_end (i) !=
159 GNUNET_YES; GNUNET_CONTAINER_slist_next (i))
162 GNUNET_CONTAINER_slist_add (dst,
163 (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC
164 : GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, i->elem->elem,
167 GNUNET_CONTAINER_slist_iter_destroy (i);
172 * Create a new singly linked list
173 * @return the new list
175 struct GNUNET_CONTAINER_SList *
176 GNUNET_CONTAINER_slist_create ()
178 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
183 * Destroy a singly linked list
184 * @param l the list to be destroyed
187 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
189 GNUNET_CONTAINER_slist_clear (l);
195 * Return the beginning of a list
197 * @return iterator pointing to the beginning
199 struct GNUNET_CONTAINER_SList_Iterator *
200 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
202 struct GNUNET_CONTAINER_SList_Iterator *ret;
204 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
216 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
218 struct GNUNET_CONTAINER_SList_Elem *e;
219 struct GNUNET_CONTAINER_SList_Elem *n;
225 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
226 GNUNET_free (e->elem);
236 * Check if a list contains a certain element
239 * @param buf payload buffer to find
240 * @param len length of the payload (number of bytes in buf)
243 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
244 const void *buf, size_t len)
246 struct GNUNET_CONTAINER_SList_Elem *e;
248 for (e = l->head; e != NULL; e = e->next)
249 if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
256 * Count the elements of a list
258 * @return number of elements in the list
261 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
268 * Remove an element from the list
270 * @param i iterator that points to the element to be removed
273 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
275 struct GNUNET_CONTAINER_SList_Elem *next;
277 next = i->elem->next;
279 i->last->next = next;
281 i->list->head = next;
282 if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
283 GNUNET_free (i->elem->elem);
284 GNUNET_free (i->elem);
291 * Insert an element into a list at a specific position
292 * @param before where to insert the new element
293 * @param disp memory disposition
294 * @param buf payload buffer
295 * @param len length of the payload
298 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
299 enum GNUNET_CONTAINER_SListDisposition disp,
300 const void *buf, size_t len)
302 struct GNUNET_CONTAINER_SList_Elem *e;
304 e = create_elem (disp, buf, len);
305 e->next = before->elem;
306 if (before->last != NULL)
307 before->last->next = e;
309 before->list->head = e;
310 before->list->length++;
315 * Advance an iterator to the next element
317 * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
320 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
323 i->elem = i->elem->next;
325 return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
330 * Check if an iterator points beyond the end of a list
333 * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
334 * points to a valid element
337 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
339 return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
344 * Retrieve the element at a specific position in a list
346 * @param len payload length
350 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
355 return i->elem->elem;
359 * Release an iterator
363 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator
369 /* end of container_slist.c */