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, const void *buf,
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);
190 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
191 GNUNET_CONTAINER_slist_next (i))
194 GNUNET_CONTAINER_slist_add (dst,
196 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ?
197 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC :
198 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
199 i->elem->elem, i->elem->len);
201 GNUNET_CONTAINER_slist_iter_destroy (i);
206 * Create a new singly linked list
207 * @return the new list
209 struct GNUNET_CONTAINER_SList *
210 GNUNET_CONTAINER_slist_create ()
212 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
217 * Destroy a singly linked list
218 * @param l the list to be destroyed
221 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
223 GNUNET_CONTAINER_slist_clear (l);
229 * Return the beginning of a list
231 * @return iterator pointing to the beginning
233 struct GNUNET_CONTAINER_SList_Iterator *
234 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
236 struct GNUNET_CONTAINER_SList_Iterator *ret;
238 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
250 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
252 struct GNUNET_CONTAINER_SList_Elem *e;
253 struct GNUNET_CONTAINER_SList_Elem *n;
259 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
260 GNUNET_free (e->elem);
271 * Check if a list contains a certain element
274 * @param buf payload buffer to find
275 * @param len length of the payload (number of bytes in buf)
278 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
279 const void *buf, size_t len)
281 struct GNUNET_CONTAINER_SList_Elem *e;
283 for (e = l->head; e != NULL; e = e->next)
284 if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
291 * Count the elements of a list
293 * @return number of elements in the list
296 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
303 * Remove an element from the list
305 * @param i iterator that points to the element to be removed
308 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
310 struct GNUNET_CONTAINER_SList_Elem *next;
312 next = i->elem->next;
314 i->last->next = next;
316 i->list->head = next;
318 i->list->tail = i->last;
319 if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
320 GNUNET_free (i->elem->elem);
321 GNUNET_free (i->elem);
328 * Insert an element into a list at a specific position
329 * @param before where to insert the new element
330 * @param disp memory disposition
331 * @param buf payload buffer
332 * @param len length of the payload
335 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
336 enum GNUNET_CONTAINER_SListDisposition disp,
337 const void *buf, size_t len)
339 struct GNUNET_CONTAINER_SList_Elem *e;
341 e = create_elem (disp, buf, len);
342 e->next = before->elem;
343 if (before->last != NULL)
344 before->last->next = e;
346 before->list->head = e;
348 before->list->tail = e;
349 before->list->length++;
354 * Advance an iterator to the next element
356 * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
359 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
362 i->elem = i->elem->next;
364 return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
369 * Check if an iterator points beyond the end of a list
372 * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
373 * points to a valid element
376 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
378 return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
383 * Retrieve the element at a specific position in a list
385 * @param len payload length
389 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
394 return i->elem->elem;
398 * Release an iterator
402 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i)
407 /* end of container_slist.c */