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 if system
99 SilcDList silc_dlist_init(void)
103 list = (SilcDList)silc_malloc(sizeof(*list));
106 list->stack = list->current = list->prev = NULL;
107 silc_list_init_prev(list->list, struct SilcDListEntryStruct, next, prev);
112 /****f* silcutil/SilcDListAPI/silc_dlist_sinit
117 * SilcDList silc_dlist_sinit(SilcStack stack);
121 * Initializes SilcDList. Returns the SilcDList context or NULL on error.
122 * This is same as silc_dlist_init but allocates the memory from `stack'
123 * if `stack' is non-NULL.
128 SilcDList silc_dlist_sinit(SilcStack stack)
133 stack = silc_stack_alloc(0, stack);
134 list = (SilcDList)silc_smalloc(stack, sizeof(*list));
136 silc_stack_free(stack);
140 list->current = list->prev = NULL;
141 silc_list_init_prev(list->list, struct SilcDListEntryStruct, next, prev);
146 /****f* silcutil/SilcDListAPI/silc_dlist_uninit
151 * void silc_dlist_uninit(SilcDList list);
155 * Uninits and frees all memory. Must be called to free memory. Does NOT
156 * free the contexts saved by caller. If the silc_dlist_sinit was used
157 * with SilcStack this will release all memory allocated by the SilcDList
158 * back to the SilcStack.
163 void silc_dlist_uninit(SilcDList list)
167 SilcStack stack = list->stack;
168 silc_list_start(list->list);
169 while ((e = (SilcDListEntry)silc_list_get(list->list)) != SILC_LIST_END) {
170 silc_list_del(list->list, e);
171 silc_sfree(stack, e);
173 silc_sfree(stack, list);
174 silc_stack_free(stack);
178 /****f* silcutil/SilcDListAPI/silc_dlist_count
183 * int silc_dlist_count(SilcDList list);
187 * Return the number of entries in the list.
192 int silc_dlist_count(SilcDList list)
194 return silc_list_count(list->list);
197 /****f* silcutil/SilcDListAPI/silc_dlist_start
202 * void silc_dlist_start(SilcDList list);
206 * Set the start of the list. This prepares the list for traversing entries
207 * from the start of the list towards end of the list.
212 void silc_dlist_start(SilcDList list)
214 silc_list_start(list->list);
215 list->current = list->prev = NULL;
218 /****f* silcutil/SilcDListAPI/silc_dlist_end
223 * void silc_dlist_end(SilcDList list);
227 * Set the end of the list. This prepares the list for traversing entries
228 * from the end of the list towards start of the list.
233 void silc_dlist_end(SilcDList list)
235 silc_list_end(list->list);
236 list->current = list->prev = NULL;
239 /****f* silcutil/SilcDListAPI/silc_dlist_add
244 * SilcBool silc_dlist_add(SilcDList list, void *context);
248 * Adds new entry to the list. This is the default function to add new
249 * entries to the list.
254 SilcBool silc_dlist_add(SilcDList list, void *context)
256 SilcDListEntry e = (SilcDListEntry)silc_smalloc(list->stack, sizeof(*e));
257 if (silc_unlikely(!e))
259 e->context = context;
260 silc_list_add(list->list, e);
264 /****f* silcutil/SilcDList/silc_dlist_insert
269 * SilcBool silc_dlist_insert(SilcDList list, void *context);
273 * Insert new entry to the list between current and previous entry.
274 * If list is at the start this adds the entry at head of the list.
275 * Use silc_dlist_add to add at the end of the list.
280 SilcBool silc_dlist_insert(SilcDList list, void *context)
282 SilcDListEntry e = (SilcDListEntry)silc_smalloc(list->stack, sizeof(*e));
283 if (silc_unlikely(!e))
285 e->context = context;
286 silc_list_insert(list->list, list->prev, e);
290 /****f* silcutil/SilcDListAPI/silc_dlist_del
295 * void silc_dlist_del(SilcDList list, void *entry);
299 * Remove entry from the list.
304 void silc_dlist_del(SilcDList list, void *entry)
308 silc_list_start(list->list);
309 while ((e = (SilcDListEntry)silc_list_get(list->list)) != SILC_LIST_END) {
310 if (e->context == entry) {
311 silc_list_del(list->list, e);
312 #if defined(SILC_DEBUG)
313 memset(e, 'F', sizeof(*e));
315 if (list->current == e)
316 list->current = NULL;
319 silc_sfree(list->stack, e);
325 /****f* silcutil/SilcDListAPI/silc_dlist_get
330 * void *silc_dlist_get(SilcDList list);
334 * Returns current entry from the list and moves the list pointer forward
335 * so that calling this next time returns the next entry from the list.
336 * This can be used to traverse the list. Return SILC_LIST_END when the
337 * entire list has been traversed. Later, silc_list_start (or
338 * silc_dlist_end) must be called again when re-starting list traversing.
342 * // Traverse the list from the beginning to the end
343 * silc_dlist_start(list)
344 * while ((entry = silc_dlist_get(list)) != SILC_LIST_END) {
351 void *silc_dlist_get(SilcDList list)
354 list->prev = list->current;
355 list->current = e = (SilcDListEntry)silc_list_get(list->list);
356 if (e != SILC_LIST_END)
358 return SILC_LIST_END;