aboutsummaryrefslogtreecommitdiff
path: root/src/util/container_slist.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-10-03 18:46:00 +0000
committerChristian Grothoff <christian@grothoff.org>2009-10-03 18:46:00 +0000
commit6aaab5e7cb4cb0c5fd5a5575c45b48124597e570 (patch)
treeefd7fb369ead6fdbf9a93f810f9d914275854ff7 /src/util/container_slist.c
parent2681e7d4316cd505b646473bf0039b91db0b04b1 (diff)
downloadgnunet-6aaab5e7cb4cb0c5fd5a5575c45b48124597e570.tar.gz
gnunet-6aaab5e7cb4cb0c5fd5a5575c45b48124597e570.zip
use enum
Diffstat (limited to 'src/util/container_slist.c')
-rw-r--r--src/util/container_slist.c69
1 files changed, 61 insertions, 8 deletions
diff --git a/src/util/container_slist.c b/src/util/container_slist.c
index 874068bdb..4d3c71a38 100644
--- a/src/util/container_slist.c
+++ b/src/util/container_slist.c
@@ -27,24 +27,68 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_container_lib.h" 28#include "gnunet_container_lib.h"
29 29
30/**
31 * Element in our linked list.
32 */
30struct GNUNET_CONTAINER_SList_Elem 33struct GNUNET_CONTAINER_SList_Elem
31{ 34{
35 /**
36 * This is a linked list.
37 */
32 struct GNUNET_CONTAINER_SList_Elem *next; 38 struct GNUNET_CONTAINER_SList_Elem *next;
33 const void *elem; 39
40 /**
41 * Application data stored at this element.
42 */
43 void *elem;
44
45 /**
46 * Number of bytes stored in elem.
47 */
34 size_t len; 48 size_t len;
35 int disp; 49
50 /**
51 * Disposition of the element.
52 */
53 enum GNUNET_CONTAINER_SListDisposition disp;
36}; 54};
37 55
56
57/**
58 * Handle to a singly linked list
59 */
38struct GNUNET_CONTAINER_SList 60struct GNUNET_CONTAINER_SList
39{ 61{
62 /**
63 * Head of the linked list.
64 */
40 struct GNUNET_CONTAINER_SList_Elem *head; 65 struct GNUNET_CONTAINER_SList_Elem *head;
66
67 /**
68 * Number of elements in the list.
69 */
41 unsigned int length; 70 unsigned int length;
42}; 71};
43 72
73
74/**
75 * Handle to a singly linked list iterator
76 */
44struct GNUNET_CONTAINER_SList_Iterator 77struct GNUNET_CONTAINER_SList_Iterator
45{ 78{
79 /**
80 * Linked list that we are iterating over.
81 */
46 struct GNUNET_CONTAINER_SList *list; 82 struct GNUNET_CONTAINER_SList *list;
83
84 /**
85 * Last element accessed.
86 */
47 struct GNUNET_CONTAINER_SList_Elem *last; 87 struct GNUNET_CONTAINER_SList_Elem *last;
88
89 /**
90 * Current list element.
91 */
48 struct GNUNET_CONTAINER_SList_Elem *elem; 92 struct GNUNET_CONTAINER_SList_Elem *elem;
49}; 93};
50 94
@@ -57,20 +101,22 @@ struct GNUNET_CONTAINER_SList_Iterator
57 * @return a new element 101 * @return a new element
58 */ 102 */
59static struct GNUNET_CONTAINER_SList_Elem * 103static struct GNUNET_CONTAINER_SList_Elem *
60create_elem (int disp, const void *buf, size_t len) 104create_elem (enum GNUNET_CONTAINER_SListDisposition disp,
105 const void *buf,
106 size_t len)
61{ 107{
62 struct GNUNET_CONTAINER_SList_Elem *e; 108 struct GNUNET_CONTAINER_SList_Elem *e;
63 109
64 if (disp == GNUNET_MEM_DISP_TRANSIENT) 110 if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
65 { 111 {
66 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len); 112 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
67 memcpy (&e[1], buf, len); 113 memcpy (&e[1], buf, len);
68 e->elem = (const void*) &e[1]; 114 e->elem = (void*) &e[1];
69 } 115 }
70 else 116 else
71 { 117 {
72 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem)); 118 e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
73 e->elem = buf; 119 e->elem = (void*) buf;
74 } 120 }
75 e->disp = disp; 121 e->disp = disp;
76 e->len = len; 122 e->len = len;
@@ -86,7 +132,8 @@ create_elem (int disp, const void *buf, size_t len)
86 * @param len length of the buffer 132 * @param len length of the buffer
87 */ 133 */
88void 134void
89GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, int disp, 135GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
136 enum GNUNET_CONTAINER_SListDisposition disp,
90 const void *buf, size_t len) 137 const void *buf, size_t len)
91{ 138{
92 struct GNUNET_CONTAINER_SList_Elem *e; 139 struct GNUNET_CONTAINER_SList_Elem *e;
@@ -108,6 +155,7 @@ GNUNET_CONTAINER_slist_create ()
108 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList)); 155 return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
109} 156}
110 157
158
111/** 159/**
112 * Destroy a singly linked list 160 * Destroy a singly linked list
113 * @param l the list to be destroyed 161 * @param l the list to be destroyed
@@ -151,6 +199,8 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
151 while (e != NULL) 199 while (e != NULL)
152 { 200 {
153 n = e->next; 201 n = e->next;
202 if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
203 GNUNET_free (e->elem);
154 GNUNET_free (e); 204 GNUNET_free (e);
155 e = n; 205 e = n;
156 } 206 }
@@ -207,6 +257,8 @@ GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
207 i->last->next = next; 257 i->last->next = next;
208 else 258 else
209 i->list->head = next; 259 i->list->head = next;
260 if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
261 GNUNET_free (i->elem->elem);
210 GNUNET_free (i->elem); 262 GNUNET_free (i->elem);
211 i->list->length--; 263 i->list->length--;
212 i->elem = next; 264 i->elem = next;
@@ -222,7 +274,8 @@ GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
222 */ 274 */
223void 275void
224GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before, 276GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
225 int disp, const void *buf, size_t len) 277 enum GNUNET_CONTAINER_SListDisposition disp,
278 const void *buf, size_t len)
226{ 279{
227 struct GNUNET_CONTAINER_SList_Elem *e; 280 struct GNUNET_CONTAINER_SList_Elem *e;
228 281