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.
83 * Create a new element that is to be inserted into the list
85 * @param disp memory disposition
86 * @param buf payload buffer
87 * @param len length of the buffer
88 * @return a new element
90 static struct GNUNET_CONTAINER_SList_Elem *
91 create_elem (enum GNUNET_CONTAINER_SListDisposition disp, const void *buf,
94 struct GNUNET_CONTAINER_SList_Elem *e;
96 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
98 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
99 memcpy (&e[1], buf, len);
100 e->elem = (void *) &e[1];
104 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
105 e->elem = (void *) buf;
114 * Add a new element to the list
116 * @param disp memory disposition
117 * @param buf payload buffer
118 * @param len length of the buffer
121 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
122 enum GNUNET_CONTAINER_SListDisposition disp,
123 const void *buf, size_t len)
125 struct GNUNET_CONTAINER_SList_Elem *e;
127 e = create_elem (disp, buf, len);
136 * Add a new element to the end of the list
138 * @param disp memory disposition
139 * @param buf payload buffer
140 * @param len length of the buffer
143 GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
144 enum GNUNET_CONTAINER_SListDisposition disp,
145 const void *buf, size_t len)
147 struct GNUNET_CONTAINER_SList_Elem *e;
149 e = create_elem (disp, buf, len);
160 * Append a singly linked list to another
161 * @param dst list to append to
165 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
166 struct GNUNET_CONTAINER_SList *src)
168 struct GNUNET_CONTAINER_SList_Iterator i;
170 for (i = GNUNET_CONTAINER_slist_begin (src);
171 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
172 GNUNET_CONTAINER_slist_next (&i))
175 GNUNET_CONTAINER_slist_add (dst,
177 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC)
178 ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC
180 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
181 i.elem->elem, i.elem->len);
183 GNUNET_CONTAINER_slist_iter_destroy (&i);
188 * Create a new singly linked list
189 * @return the new list
191 struct GNUNET_CONTAINER_SList *
192 GNUNET_CONTAINER_slist_create ()
194 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
199 * Destroy a singly linked list
200 * @param l the list to be destroyed
203 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
205 GNUNET_CONTAINER_slist_clear (l);
211 * Return the beginning of a list
213 * @return iterator pointing to the beginning
215 struct GNUNET_CONTAINER_SList_Iterator
216 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
218 struct GNUNET_CONTAINER_SList_Iterator ret;
220 memset (&ret, 0, sizeof (ret));
232 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
234 struct GNUNET_CONTAINER_SList_Elem *e;
235 struct GNUNET_CONTAINER_SList_Elem *n;
241 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
242 GNUNET_free (e->elem);
253 * Check if a list contains a certain element
256 * @param buf payload buffer to find
257 * @param len length of the payload (number of bytes in buf)
260 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
261 const void *buf, size_t len)
263 struct GNUNET_CONTAINER_SList_Elem *e;
265 for (e = l->head; e != NULL; e = e->next)
266 if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
273 * Count the elements of a list
275 * @return number of elements in the list
278 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
285 * Remove an element from the list
287 * @param i iterator that points to the element to be removed
290 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
292 struct GNUNET_CONTAINER_SList_Elem *next;
294 next = i->elem->next;
296 i->last->next = next;
298 i->list->head = next;
300 i->list->tail = i->last;
301 if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
302 GNUNET_free (i->elem->elem);
303 GNUNET_free (i->elem);
310 * Insert an element into a list at a specific position
311 * @param before where to insert the new element
312 * @param disp memory disposition
313 * @param buf payload buffer
314 * @param len length of the payload
317 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
318 enum GNUNET_CONTAINER_SListDisposition disp,
319 const void *buf, size_t len)
321 struct GNUNET_CONTAINER_SList_Elem *e;
323 e = create_elem (disp, buf, len);
324 e->next = before->elem;
325 if (before->last != NULL)
326 before->last->next = e;
328 before->list->head = e;
330 before->list->tail = e;
331 before->list->length++;
336 * Advance an iterator to the next element
338 * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
341 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
344 i->elem = i->elem->next;
346 return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
351 * Check if an iterator points beyond the end of a list
354 * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
355 * points to a valid element
358 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
360 return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
365 * Retrieve the element at a specific position in a list
367 * @param len payload length
371 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
376 return i->elem->elem;
380 * Release an iterator
384 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator
389 /* end of container_slist.c */