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 /****s* silcutil/SilcDListAPI/SilcDList
45 * typedef struct { ... } *SilcDList;
49 * This is the actual SilcDList object that is used by application.
50 * Application defines this object and adds contexts to this list with
51 * Dynamic List Interface functions.
54 typedef struct SilcDListStruct {
61 /* SilcDListEntry structure, one entry in the list. This MUST NOT be used
62 directly by the application. */
63 typedef struct SilcDListEntryStruct {
65 struct SilcDListEntryStruct *next;
66 struct SilcDListEntryStruct *prev;
69 /****f* silcutil/SilcDListAPI/silc_dlist_init
74 * SilcDList silc_dlist_init(void);
78 * Initializes SilcDList. Returns the SilcDList context or NULL on error.
83 SilcDList silc_dlist_init(void)
87 list = (SilcDList)silc_malloc(sizeof(*list));
90 list->current = list->prev = NULL;
91 silc_list_init_prev(list->list, struct SilcDListEntryStruct, next, prev);
96 /****f* silcutil/SilcDListAPI/silc_dlist_sinit
101 * SilcDList silc_dlist_sinit(SilcStack stack);
105 * Initializes SilcDList. Returns the SilcDList context or NULL on error.
106 * This is same as silc_dlist_init but allocates the memory from `stack'
107 * if `stack' is non-NULL.
112 SilcDList silc_dlist_sinit(SilcStack stack)
117 stack = silc_stack_alloc(0, stack);
118 list = (SilcDList)silc_smalloc(stack, sizeof(*list));
120 silc_stack_free(stack);
124 list->current = list->prev = NULL;
125 silc_list_init_prev(list->list, struct SilcDListEntryStruct, next, prev);
130 /****f* silcutil/SilcDListAPI/silc_dlist_uninit
135 * void silc_dlist_uninit(SilcDList list);
139 * Uninits and frees all memory. Must be called to free memory. Does NOT
140 * free the contexts saved by caller. If the silc_dlist_sinit was used
141 * with SilcStack this will release all memory allocated by the SilcDList
142 * back to the SilcStack.
147 void silc_dlist_uninit(SilcDList list)
151 SilcStack stack = list->stack;
152 silc_list_start(list->list);
153 while ((e = (SilcDListEntry)silc_list_get(list->list)) != SILC_LIST_END) {
154 silc_list_del(list->list, e);
155 silc_sfree(stack, e);
157 silc_sfree(stack, list);
158 silc_stack_free(stack);
162 /****f* silcutil/SilcDListAPI/silc_dlist_count
167 * int silc_dlist_count(SilcDList list);
171 * Return the number of entries in the list.
176 int silc_dlist_count(SilcDList list)
178 return silc_list_count(list->list);
181 /****f* silcutil/SilcDListAPI/silc_dlist_start
186 * void silc_dlist_start(SilcDList list);
190 * Set the start of the list. This prepares the list for traversing entries
191 * from the start of the list towards end of the list.
196 void silc_dlist_start(SilcDList list)
198 silc_list_start(list->list);
199 list->current = list->prev = NULL;
202 /****f* silcutil/SilcDListAPI/silc_dlist_end
207 * void silc_dlist_end(SilcDList list);
211 * Set the end of the list. This prepares the list for traversing entries
212 * from the end of the list towards start of the list.
217 void silc_dlist_end(SilcDList list)
219 silc_list_end(list->list);
220 list->current = list->prev = NULL;
223 /****f* silcutil/SilcDListAPI/silc_dlist_add
228 * SilcBool silc_dlist_add(SilcDList list, void *context);
232 * Adds new entry to the list. This is the default function to add new
233 * entries to the list.
238 SilcBool silc_dlist_add(SilcDList list, void *context)
240 SilcDListEntry e = (SilcDListEntry)silc_smalloc(list->stack, sizeof(*e));
241 if (silc_unlikely(!e))
243 e->context = context;
244 silc_list_add(list->list, e);
248 /****f* silcutil/SilcDList/silc_dlist_insert
253 * SilcBool silc_dlist_insert(SilcDList list, void *context);
257 * Insert new entry to the list between current and previous entry.
258 * If list is at the start this adds the entry at head of the list.
259 * Use silc_dlist_add to add at the end of the list.
264 SilcBool silc_dlist_insert(SilcDList list, void *context)
266 SilcDListEntry e = (SilcDListEntry)silc_smalloc(list->stack, sizeof(*e));
267 if (silc_unlikely(!e))
269 e->context = context;
270 silc_list_insert(list->list, list->prev, e);
274 /****f* silcutil/SilcDListAPI/silc_dlist_del
279 * void silc_dlist_del(SilcDList list, void *entry);
283 * Remove entry from the list.
288 void silc_dlist_del(SilcDList list, void *entry)
292 silc_list_start(list->list);
293 while ((e = (SilcDListEntry)silc_list_get(list->list)) != SILC_LIST_END) {
294 if (e->context == entry) {
295 silc_list_del(list->list, e);
296 #if defined(SILC_DEBUG)
297 memset(e, 'F', sizeof(*e));
299 if (list->current == e)
300 list->current = NULL;
303 silc_sfree(list->stack, e);
309 /****f* silcutil/SilcDListAPI/silc_dlist_get
314 * void *silc_dlist_get(SilcDList list);
318 * Returns current entry from the list and moves the list pointer forward
319 * so that calling this next time returns the next entry from the list.
320 * This can be used to traverse the list. Return SILC_LIST_END when the
321 * entire list has been traversed. Later, silc_list_start (or
322 * silc_dlist_end) must be called again when re-starting list traversing.
326 * // Traverse the list from the beginning to the end
327 * silc_dlist_start(list)
328 * while ((entry = silc_dlist_get(list)) != SILC_LIST_END) {
335 void *silc_dlist_get(SilcDList list)
338 list->prev = list->current;
339 list->current = e = (SilcDListEntry)silc_list_get(list->list);
340 if (e != SILC_LIST_END)
342 return SILC_LIST_END;