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;
96 * Create a new element that is to be inserted into the list
98 * @param disp memory disposition
99 * @param buf payload buffer
100 * @param len length of the buffer
101 * @return a new element
103 static struct GNUNET_CONTAINER_SList_Elem *
104 create_elem (enum GNUNET_CONTAINER_SListDisposition disp,
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 lenght of the payload
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) &&
227 (memcmp (buf, e->elem, len) == 0) )
234 * Count the elements of a list
236 * @return number of elements in the list
239 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
246 * Remove an element from the list
248 * @param i iterator that points to the element to be removed
251 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
253 struct GNUNET_CONTAINER_SList_Elem *next;
255 next = i->elem->next;
257 i->last->next = next;
259 i->list->head = next;
260 if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
261 GNUNET_free (i->elem->elem);
262 GNUNET_free (i->elem);
269 * Insert an element into a list at a specific position
270 * @param before where to insert the new element
271 * @param disp memory disposition
272 * @param buf payload buffer
273 * @param len length of the payload
276 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
277 enum GNUNET_CONTAINER_SListDisposition disp,
278 const void *buf, size_t len)
280 struct GNUNET_CONTAINER_SList_Elem *e;
282 e = create_elem (disp, buf, len);
283 e->next = before->elem;
284 if (before->last != NULL)
285 before->last->next = e;
287 before->list->head = e;
288 before->list->length++;
293 * Advance an iterator to the next element
295 * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
298 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
301 i->elem = i->elem->next;
303 return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
308 * Check if an iterator points beyond the end of a list
311 * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
312 * points to a valid element
315 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
317 return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
322 * Retrieve the element at a specific position in a list
324 * @param len payload length
328 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
333 return i->elem->elem;
336 /* end of container_slist.c */