asserts
[oweals/gnunet.git] / src / util / container_slist.c
index 874068bdb942ce63865272f47a5faa2fb279f327..fb9ab5558719c2a17b7efa8a5c395e65c2445458 100644 (file)
 #include "platform.h"
 #include "gnunet_container_lib.h"
 
+/**
+ * Element in our linked list.
+ */
 struct GNUNET_CONTAINER_SList_Elem
 {
+  /**
+   * This is a linked list.
+   */
   struct GNUNET_CONTAINER_SList_Elem *next;
-  const void *elem;
+
+  /**
+   * Application data stored at this element.
+   */
+  void *elem;
+
+  /**
+   * Number of bytes stored in elem.
+   */
   size_t len;
-  int disp;
+
+  /**
+   * Disposition of the element.
+   */
+  enum GNUNET_CONTAINER_SListDisposition disp;
 };
 
+
+/**
+ * Handle to a singly linked list  
+ */
 struct GNUNET_CONTAINER_SList
 {
+  /**
+   * Head of the linked list.
+   */
   struct GNUNET_CONTAINER_SList_Elem *head;
+
+  /**
+   * Number of elements in the list.
+   */
   unsigned int length;
 };
 
+
+/**
+ * Handle to a singly linked list iterator 
+ */
 struct GNUNET_CONTAINER_SList_Iterator
 {
+  /**
+   * Linked list that we are iterating over.
+   */
   struct GNUNET_CONTAINER_SList *list;
+
+  /**
+   * Last element accessed.
+   */
   struct GNUNET_CONTAINER_SList_Elem *last;
+
+  /**
+   * Current list element.
+   */
   struct GNUNET_CONTAINER_SList_Elem *elem;
 };
 
+
 /**
  * Create a new element that is to be inserted into the list
  * @internal
@@ -57,20 +102,21 @@ struct GNUNET_CONTAINER_SList_Iterator
  * @return a new element
  */
 static struct GNUNET_CONTAINER_SList_Elem *
-create_elem (int disp, const void *buf, size_t len)
+create_elem (enum GNUNET_CONTAINER_SListDisposition disp,
+             const void *buf, size_t len)
 {
   struct GNUNET_CONTAINER_SList_Elem *e;
 
-  if (disp == GNUNET_MEM_DISP_TRANSIENT)
+  if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
     {
       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
       memcpy (&e[1], buf, len);
-      e->elem = (const void*) &e[1];
+      e->elem = (void *) &e[1];
     }
   else
     {
       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
-      e->elem = buf;
+      e->elem = (void *) buf;
     }
   e->disp = disp;
   e->len = len;
@@ -86,7 +132,8 @@ create_elem (int disp, const void *buf, size_t len)
  * @param len length of the buffer
  */
 void
-GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, int disp,
+GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
+                            enum GNUNET_CONTAINER_SListDisposition disp,
                             const void *buf, size_t len)
 {
   struct GNUNET_CONTAINER_SList_Elem *e;
@@ -98,6 +145,29 @@ GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, int disp,
 }
 
 
+/**
+ * Append a singly linked list to another
+ * @param dst list to append to
+ * @param src source
+ */
+void
+GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst, struct GNUNET_CONTAINER_SList *src)
+{
+  struct GNUNET_CONTAINER_SList_Iterator *i;
+
+  for (i = GNUNET_CONTAINER_slist_begin (src); GNUNET_CONTAINER_slist_end (i) !=
+      GNUNET_YES; GNUNET_CONTAINER_slist_next (i))
+
+    {
+      GNUNET_CONTAINER_slist_add (dst,
+          (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ? GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC
+              : GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, i->elem->elem,
+          i->elem->len);
+    }
+  GNUNET_CONTAINER_slist_iter_destroy (i);
+}
+
+
 /**
  * Create a new singly linked list
  * @return the new list
@@ -108,6 +178,7 @@ GNUNET_CONTAINER_slist_create ()
   return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
 }
 
+
 /**
  * Destroy a singly linked list
  * @param l the list to be destroyed
@@ -151,6 +222,8 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
   while (e != NULL)
     {
       n = e->next;
+      if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
+        GNUNET_free (e->elem);
       GNUNET_free (e);
       e = n;
     }
@@ -164,7 +237,7 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
  *
  * @param l list
  * @param buf payload buffer to find
- * @param lenght of the payload
+ * @param len length of the payload (number of bytes in buf)
  */
 int
 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
@@ -173,8 +246,7 @@ GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
   struct GNUNET_CONTAINER_SList_Elem *e;
 
   for (e = l->head; e != NULL; e = e->next)
-    if ( (e->len == len) && 
-        (memcmp (buf, e->elem, len) == 0) )
+    if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
       return GNUNET_YES;
   return GNUNET_NO;
 }
@@ -207,6 +279,8 @@ GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
     i->last->next = next;
   else
     i->list->head = next;
+  if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
+    GNUNET_free (i->elem->elem);
   GNUNET_free (i->elem);
   i->list->length--;
   i->elem = next;
@@ -222,7 +296,8 @@ GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
  */
 void
 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
-                               int disp, const void *buf, size_t len)
+                               enum GNUNET_CONTAINER_SListDisposition disp,
+                               const void *buf, size_t len)
 {
   struct GNUNET_CONTAINER_SList_Elem *e;
 
@@ -280,4 +355,15 @@ GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
   return i->elem->elem;
 }
 
+/**
+ * Release an iterator
+ * @param i iterator
+ */
+void
+GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator
+                                     *i)
+{
+  GNUNET_free (i);
+}
+
 /* end of container_slist.c */