5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2000 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
25 /****h* silcutil/SILC Dynamic List Interface
29 * SILC Dynamic List API can be used to add opaque contexts to list that
30 * will automatically allocate list entries. Normal SILC List API cannot
31 * be used for this purpose because in that case the context passed to the
32 * list must be defined as list structure already. This is not the case in
33 * SilcDList. But SilcDList is a bit slower than SilcList because it
34 * requires memory allocation when adding new entries to the list.
36 * SILC Dynamic List is not thread-safe. If the same list context must be
37 * used in multithreaded environment concurrency control must be employed.
41 * SilcDList list = silc_dlist_init();
43 * silc_dlist_add(list, entry1);
44 * silc_dlist_add(list, entry2);
46 * // Traverse the list from the beginning to the end
47 * silc_dlist_start(list)
48 * while ((entry = silc_dlist_get(list)) != SILC_LIST_END) {
52 * silc_dlist_uninit(list);
56 /****s* silcutil/SilcDListAPI/SilcDList
60 * typedef struct { ... } *SilcDList;
64 * This is the actual SilcDList object that is used by application.
65 * Application defines this object and adds contexts to this list with
66 * Dynamic List Interface functions.
69 typedef struct SilcDListStruct {
76 /* SilcDListEntry structure, one entry in the list. This MUST NOT be used
77 directly by the application. */
78 typedef struct SilcDListEntryStruct {
80 struct SilcDListEntryStruct *next;
81 struct SilcDListEntryStruct *prev;
84 /****f* silcutil/SilcDListAPI/silc_dlist_init
89 * SilcDList silc_dlist_init(void);
93 * Initializes SilcDList. Returns the SilcDList context or NULL on error.
98 SilcDList silc_dlist_init(void)
102 list = (SilcDList)silc_malloc(sizeof(*list));
105 list->stack = list->current = list->prev = NULL;
106 silc_list_init_prev(list->list, struct SilcDListEntryStruct, next, prev);
111 /****f* silcutil/SilcDListAPI/silc_dlist_sinit
116 * SilcDList silc_dlist_sinit(SilcStack stack);
120 * Initializes SilcDList. Returns the SilcDList context or NULL on error.
121 * This is same as silc_dlist_init but allocates the memory from `stack'
122 * if `stack' is non-NULL.
127 SilcDList silc_dlist_sinit(SilcStack stack)
132 stack = silc_stack_alloc(0, stack);
133 list = (SilcDList)silc_smalloc(stack, sizeof(*list));
135 silc_stack_free(stack);
139 list->current = list->prev = NULL;
140 silc_list_init_prev(list->list, struct SilcDListEntryStruct, next, prev);
145 /****f* silcutil/SilcDListAPI/silc_dlist_uninit
150 * void silc_dlist_uninit(SilcDList list);
154 * Uninits and frees all memory. Must be called to free memory. Does NOT
155 * free the contexts saved by caller. If the silc_dlist_sinit was used
156 * with SilcStack this will release all memory allocated by the SilcDList
157 * back to the SilcStack.
162 void silc_dlist_uninit(SilcDList list)
166 SilcStack stack = list->stack;
167 silc_list_start(list->list);
168 while ((e = (SilcDListEntry)silc_list_get(list->list)) != SILC_LIST_END) {
169 silc_list_del(list->list, e);
170 silc_sfree(stack, e);
172 silc_sfree(stack, list);
173 silc_stack_free(stack);
177 /****f* silcutil/SilcDListAPI/silc_dlist_count
182 * int silc_dlist_count(SilcDList list);
186 * Return the number of entries in the list.
191 int silc_dlist_count(SilcDList list)
193 return silc_list_count(list->list);
196 /****f* silcutil/SilcDListAPI/silc_dlist_start
201 * void silc_dlist_start(SilcDList list);
205 * Set the start of the list. This prepares the list for traversing entries
206 * from the start of the list towards end of the list.
211 void silc_dlist_start(SilcDList list)
213 silc_list_start(list->list);
214 list->current = list->prev = NULL;
217 /****f* silcutil/SilcDListAPI/silc_dlist_end
222 * void silc_dlist_end(SilcDList list);
226 * Set the end of the list. This prepares the list for traversing entries
227 * from the end of the list towards start of the list.
232 void silc_dlist_end(SilcDList list)
234 silc_list_end(list->list);
235 list->current = list->prev = NULL;
238 /****f* silcutil/SilcDListAPI/silc_dlist_add
243 * SilcBool silc_dlist_add(SilcDList list, void *context);
247 * Adds new entry to the list. This is the default function to add new
248 * entries to the list.
253 SilcBool silc_dlist_add(SilcDList list, void *context)
255 SilcDListEntry e = (SilcDListEntry)silc_smalloc(list->stack, sizeof(*e));
256 if (silc_unlikely(!e))
258 e->context = context;
259 silc_list_add(list->list, e);
263 /****f* silcutil/SilcDList/silc_dlist_insert
268 * SilcBool silc_dlist_insert(SilcDList list, void *context);
272 * Insert new entry to the list between current and previous entry.
273 * If list is at the start this adds the entry at head of the list.
274 * Use silc_dlist_add to add at the end of the list.
279 SilcBool silc_dlist_insert(SilcDList list, void *context)
281 SilcDListEntry e = (SilcDListEntry)silc_smalloc(list->stack, sizeof(*e));
282 if (silc_unlikely(!e))
284 e->context = context;
285 silc_list_insert(list->list, list->prev, e);
289 /****f* silcutil/SilcDListAPI/silc_dlist_del
294 * void silc_dlist_del(SilcDList list, void *entry);
298 * Remove entry from the list.
303 void silc_dlist_del(SilcDList list, void *entry)
307 silc_list_start(list->list);
308 while ((e = (SilcDListEntry)silc_list_get(list->list)) != SILC_LIST_END) {
309 if (e->context == entry) {
310 silc_list_del(list->list, e);
311 #if defined(SILC_DEBUG)
312 memset(e, 'F', sizeof(*e));
314 if (list->current == e)
315 list->current = NULL;
318 silc_sfree(list->stack, e);
324 /****f* silcutil/SilcDListAPI/silc_dlist_get
329 * void *silc_dlist_get(SilcDList list);
333 * Returns current entry from the list and moves the list pointer forward
334 * so that calling this next time returns the next entry from the list.
335 * This can be used to traverse the list. Return SILC_LIST_END when the
336 * entire list has been traversed. Later, silc_list_start (or
337 * silc_dlist_end) must be called again when re-starting list traversing.
341 * // Traverse the list from the beginning to the end
342 * silc_dlist_start(list)
343 * while ((entry = silc_dlist_get(list)) != SILC_LIST_END) {
350 void *silc_dlist_get(SilcDList list)
353 list->prev = list->current;
354 list->current = e = (SilcDListEntry)silc_list_get(list->list);
355 if (e != SILC_LIST_END)
357 return SILC_LIST_END;