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"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33 * Element in our linked list.
35 struct GNUNET_CONTAINER_SList_Elem
38 * This is a linked list.
40 struct GNUNET_CONTAINER_SList_Elem *next;
43 * Application data stored at this element.
48 * Number of bytes stored in elem.
53 * Disposition of the element.
55 enum GNUNET_CONTAINER_SListDisposition disp;
60 * Handle to a singly linked list
62 struct GNUNET_CONTAINER_SList
65 * Head of the linked list.
67 struct GNUNET_CONTAINER_SList_Elem *head;
70 * Tail of the linked list.
72 struct GNUNET_CONTAINER_SList_Elem *tail;
75 * Number of elements in the list.
82 * Handle to a singly linked list iterator
84 struct GNUNET_CONTAINER_SList_Iterator
87 * Linked list that we are iterating over.
89 struct GNUNET_CONTAINER_SList *list;
92 * Last element accessed.
94 struct GNUNET_CONTAINER_SList_Elem *last;
97 * Current list element.
99 struct GNUNET_CONTAINER_SList_Elem *elem;
104 * Create a new element that is to be inserted into the list
106 * @param disp memory disposition
107 * @param buf payload buffer
108 * @param len length of the buffer
109 * @return a new element
111 static struct GNUNET_CONTAINER_SList_Elem *
112 create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf,
115 struct GNUNET_CONTAINER_SList_Elem *e;
117 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
119 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
120 memcpy (&e[1], buf, len);
121 e->elem = (void *) &e[1];
125 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
126 e->elem = (void *) buf;
135 * Add a new element to the list
137 * @param disp memory disposition
138 * @param buf payload buffer
139 * @param len length of the buffer
142 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
143 enum GNUNET_CONTAINER_SListDisposition disp,
144 const void *buf, size_t len)
146 struct GNUNET_CONTAINER_SList_Elem *e;
148 e = create_elem (disp, buf, len);
157 * Add a new element to the end of the list
159 * @param disp memory disposition
160 * @param buf payload buffer
161 * @param len length of the buffer
164 GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
165 enum GNUNET_CONTAINER_SListDisposition disp,
166 const void *buf, size_t len)
168 struct GNUNET_CONTAINER_SList_Elem *e;
170 e = create_elem (disp, buf, len);
181 * Append a singly linked list to another
182 * @param dst list to append to
186 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
187 struct GNUNET_CONTAINER_SList *src)
189 struct GNUNET_CONTAINER_SList_Iterator *i;
191 for (i = GNUNET_CONTAINER_slist_begin (src);
192 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
193 GNUNET_CONTAINER_slist_next (i))
196 GNUNET_CONTAINER_slist_add (dst,
198 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC)
199 ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC
201 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
202 i->elem->elem, i->elem->len);
204 GNUNET_CONTAINER_slist_iter_destroy (i);
209 * Create a new singly linked list
210 * @return the new list
212 struct GNUNET_CONTAINER_SList *
213 GNUNET_CONTAINER_slist_create ()
215 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
220 * Destroy a singly linked list
221 * @param l the list to be destroyed
224 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
226 GNUNET_CONTAINER_slist_clear (l);
232 * Return the beginning of a list
234 * @return iterator pointing to the beginning
236 struct GNUNET_CONTAINER_SList_Iterator *
237 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
239 struct GNUNET_CONTAINER_SList_Iterator *ret;
241 ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
253 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
255 struct GNUNET_CONTAINER_SList_Elem *e;
256 struct GNUNET_CONTAINER_SList_Elem *n;
262 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
263 GNUNET_free (e->elem);
274 * Check if a list contains a certain element
277 * @param buf payload buffer to find
278 * @param len length of the payload (number of bytes in buf)
281 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
282 const void *buf, size_t len)
284 struct GNUNET_CONTAINER_SList_Elem *e;
286 for (e = l->head; e != NULL; e = e->next)
287 if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
294 * Count the elements of a list
296 * @return number of elements in the list
299 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
306 * Remove an element from the list
308 * @param i iterator that points to the element to be removed
311 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
313 struct GNUNET_CONTAINER_SList_Elem *next;
315 next = i->elem->next;
317 i->last->next = next;
319 i->list->head = next;
321 i->list->tail = i->last;
322 if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
323 GNUNET_free (i->elem->elem);
324 GNUNET_free (i->elem);
331 * Insert an element into a list at a specific position
332 * @param before where to insert the new element
333 * @param disp memory disposition
334 * @param buf payload buffer
335 * @param len length of the payload
338 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
339 enum GNUNET_CONTAINER_SListDisposition disp,
340 const void *buf, size_t len)
342 struct GNUNET_CONTAINER_SList_Elem *e;
344 e = create_elem (disp, buf, len);
345 e->next = before->elem;
346 if (before->last != NULL)
347 before->last->next = e;
349 before->list->head = e;
351 before->list->tail = e;
352 before->list->length++;
357 * Advance an iterator to the next element
359 * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
362 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
365 i->elem = i->elem->next;
367 return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
372 * Check if an iterator points beyond the end of a list
375 * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
376 * points to a valid element
379 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
381 return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
386 * Retrieve the element at a specific position in a list
388 * @param len payload length
392 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
397 return i->elem->elem;
401 * Release an iterator
405 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator
411 /* end of container_slist.c */