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,
109 struct GNUNET_CONTAINER_SList_Elem *e;
111 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
113 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
114 memcpy (&e[1], buf, len);
115 e->elem = (void*) &e[1];
119 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
120 e->elem = (void*) buf;
129 * Add a new element to the list
131 * @param disp memory disposition
132 * @param buf payload buffer
133 * @param len length of the buffer
136 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
137 enum GNUNET_CONTAINER_SListDisposition disp,
138 const void *buf, size_t len)
140 struct GNUNET_CONTAINER_SList_Elem *e;
142 e = create_elem (disp, buf, len);
150 * Create a new singly linked list
151 * @return the new list
153 struct GNUNET_CONTAINER_SList *
154 GNUNET_CONTAINER_slist_create ()
156 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
161 * Destroy a singly linked list
162 * @param l the list to be destroyed
165 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
167 GNUNET_CONTAINER_slist_clear (l);
173 * Return the beginning of a list
175 * @return iterator pointing to the beginning
177 struct GNUNET_CONTAINER_SList_Iterator *
178 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
180 struct GNUNET_CONTAINER_SList_Iterator *ret;
182 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
194 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
196 struct GNUNET_CONTAINER_SList_Elem *e;
197 struct GNUNET_CONTAINER_SList_Elem *n;
203 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
204 GNUNET_free (e->elem);
214 * Check if a list contains a certain element
217 * @param buf payload buffer to find
218 * @param lenght of the payload
221 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
222 const void *buf, size_t len)
224 struct GNUNET_CONTAINER_SList_Elem *e;
226 for (e = l->head; e != NULL; e = e->next)
227 if ( (e->len == len) &&
228 (memcmp (buf, e->elem, len) == 0) )
235 * Count the elements of a list
237 * @return number of elements in the list
240 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
247 * Remove an element from the list
249 * @param i iterator that points to the element to be removed
252 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
254 struct GNUNET_CONTAINER_SList_Elem *next;
256 next = i->elem->next;
258 i->last->next = next;
260 i->list->head = next;
261 if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
262 GNUNET_free (i->elem->elem);
263 GNUNET_free (i->elem);
270 * Insert an element into a list at a specific position
271 * @param before where to insert the new element
272 * @param disp memory disposition
273 * @param buf payload buffer
274 * @param len length of the payload
277 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
278 enum GNUNET_CONTAINER_SListDisposition disp,
279 const void *buf, size_t len)
281 struct GNUNET_CONTAINER_SList_Elem *e;
283 e = create_elem (disp, buf, len);
284 e->next = before->elem;
285 if (before->last != NULL)
286 before->last->next = e;
288 before->list->head = e;
289 before->list->length++;
294 * Advance an iterator to the next element
296 * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
299 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
302 i->elem = i->elem->next;
304 return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
309 * Check if an iterator points beyond the end of a list
312 * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
313 * points to a valid element
316 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
318 return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
323 * Retrieve the element at a specific position in a list
325 * @param len payload length
329 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
334 return i->elem->elem;
337 /* end of container_slist.c */