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 * Create a new singly linked list
150 * @return the new list
152 struct GNUNET_CONTAINER_SList *
153 GNUNET_CONTAINER_slist_create ()
155 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
160 * Destroy a singly linked list
161 * @param l the list to be destroyed
164 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
166 GNUNET_CONTAINER_slist_clear (l);
172 * Return the beginning of a list
174 * @return iterator pointing to the beginning
176 struct GNUNET_CONTAINER_SList_Iterator *
177 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
179 struct GNUNET_CONTAINER_SList_Iterator *ret;
181 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
193 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
195 struct GNUNET_CONTAINER_SList_Elem *e;
196 struct GNUNET_CONTAINER_SList_Elem *n;
202 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
203 GNUNET_free (e->elem);
213 * Check if a list contains a certain element
216 * @param buf payload buffer to find
217 * @param len length of the payload (number of bytes in buf)
220 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
221 const void *buf, size_t len)
223 struct GNUNET_CONTAINER_SList_Elem *e;
225 for (e = l->head; e != NULL; e = e->next)
226 if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
233 * Count the elements of a list
235 * @return number of elements in the list
238 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
245 * Remove an element from the list
247 * @param i iterator that points to the element to be removed
250 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
252 struct GNUNET_CONTAINER_SList_Elem *next;
254 next = i->elem->next;
256 i->last->next = next;
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);
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
275 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
276 enum GNUNET_CONTAINER_SListDisposition disp,
277 const void *buf, size_t len)
279 struct GNUNET_CONTAINER_SList_Elem *e;
281 e = create_elem (disp, buf, len);
282 e->next = before->elem;
283 if (before->last != NULL)
284 before->last->next = e;
286 before->list->head = e;
287 before->list->length++;
292 * Advance an iterator to the next element
294 * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
297 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
300 i->elem = i->elem->next;
302 return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
307 * Check if an iterator points beyond the end of a list
310 * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
311 * points to a valid element
314 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
316 return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
321 * Retrieve the element at a specific position in a list
323 * @param len payload length
327 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
332 return i->elem->elem;
336 * Release an iterator
340 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator
346 /* end of container_slist.c */