{
/**
* This is a linked list.
- */
+ */
struct GNUNET_CONTAINER_SList_Elem *next;
/**
*/
struct GNUNET_CONTAINER_SList_Elem *head;
+ /**
+ * Tail of the linked list.
+ */
+ struct GNUNET_CONTAINER_SList_Elem *tail;
+
/**
* Number of elements in the list.
*/
* @return a new element
*/
static struct GNUNET_CONTAINER_SList_Elem *
-create_elem (enum GNUNET_CONTAINER_SListDisposition 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;
{
e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
memcpy (&e[1], buf, len);
- e->elem = (void*) &e[1];
+ e->elem = (void *) &e[1];
}
else
{
e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
- e->elem = (void*) buf;
+ e->elem = (void *) buf;
}
e->disp = disp;
e->len = len;
* @param len length of the buffer
*/
void
-GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
- enum GNUNET_CONTAINER_SListDisposition 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;
e = create_elem (disp, buf, len);
e->next = l->head;
l->head = e;
+ if (l->tail == NULL) l->tail = e;
+ l->length++;
+}
+
+/**
+ * Add a new element to the end of the list
+ * @param l list
+ * @param disp memory disposition
+ * @param buf payload buffer
+ * @param len length of the buffer
+ */
+void
+GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
+ enum GNUNET_CONTAINER_SListDisposition disp,
+ const void *buf, size_t len)
+{
+ struct GNUNET_CONTAINER_SList_Elem *e;
+
+ e = create_elem (disp, buf, len);
+ if (l->tail != NULL)
+ l->tail->next = e;
+ if (l->head == NULL)
+ l->head = e;
+ l->tail = e;
l->length++;
}
+/**
+ * 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
{
n = e->next;
if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
- GNUNET_free (e->elem);
+ GNUNET_free (e->elem);
GNUNET_free (e);
e = n;
}
l->head = NULL;
+ l->tail = NULL;
l->length = 0;
}
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;
}
i->last->next = next;
else
i->list->head = next;
+ if (next == NULL)
+ i->list->tail = i->last;
if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
GNUNET_free (i->elem->elem);
GNUNET_free (i->elem);
*/
void
GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
- enum GNUNET_CONTAINER_SListDisposition disp,
- const void *buf, size_t len)
+ enum GNUNET_CONTAINER_SListDisposition disp,
+ const void *buf, size_t len)
{
struct GNUNET_CONTAINER_SList_Elem *e;
before->last->next = e;
else
before->list->head = e;
+ if (e->next == NULL)
+ before->list->tail = e;
before->list->length++;
}
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 */