X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcdlist.h;h=a27aaad41ca86fd5f9139a986e53fe3bf10e8322;hb=30b8fb532138b2d9b523d3c069724e78fb72f8e3;hp=87dfb4cce6cb26871b4602d23153cd55a0e8934d;hpb=52e57c880aba9c5e89f59d962eb9af75670b76e0;p=silc.git diff --git a/lib/silcutil/silcdlist.h b/lib/silcutil/silcdlist.h index 87dfb4cc..a27aaad4 100644 --- a/lib/silcutil/silcdlist.h +++ b/lib/silcutil/silcdlist.h @@ -27,7 +27,7 @@ * DESCRIPTION * * SILC Dynamic List API can be used to add opaque contexts to list that - * will automatically allocate list entries. Normal SILC List API cannot + * will automatically allocate list entries. The simpler SilcList cannot * be used for this purpose because in that case the context passed to the * list must be defined as list structure already. This is not the case in * SilcDList. But SilcDList is a bit slower than SilcList because it @@ -36,6 +36,21 @@ * SILC Dynamic List is not thread-safe. If the same list context must be * used in multithreaded environment concurrency control must be employed. * + * EXAMPLE + * + * SilcDList list = silc_dlist_init(); + * + * silc_dlist_add(list, entry1); + * silc_dlist_add(list, entry2); + * + * // Traverse the list from the beginning to the end + * silc_dlist_start(list) + * while ((entry = silc_dlist_get(list)) != SILC_LIST_END) { + * ... + * } + * + * silc_dlist_uninit(list); + * ***/ /****s* silcutil/SilcDListAPI/SilcDList @@ -52,6 +67,7 @@ * ***/ typedef struct SilcDListStruct { + SilcStack stack; SilcList list; void *current; void *prev; @@ -74,7 +90,8 @@ typedef struct SilcDListEntryStruct { * * DESCRIPTION * - * Initializes SilcDList. + * Initializes SilcDList. Returns the SilcDList context or NULL if system + * is out of memory. * ***/ @@ -86,6 +103,40 @@ SilcDList silc_dlist_init(void) list = (SilcDList)silc_malloc(sizeof(*list)); if (!list) return NULL; + list->stack = list->current = list->prev = NULL; + silc_list_init_prev(list->list, struct SilcDListEntryStruct, next, prev); + + return list; +} + +/****f* silcutil/SilcDListAPI/silc_dlist_sinit + * + * SYNOPSIS + * + * static inline + * SilcDList silc_dlist_sinit(SilcStack stack); + * + * DESCRIPTION + * + * Initializes SilcDList. Returns the SilcDList context or NULL on error. + * This is same as silc_dlist_init but allocates the memory from `stack' + * if `stack' is non-NULL. + * + ***/ + +static inline +SilcDList silc_dlist_sinit(SilcStack stack) +{ + SilcDList list; + + if (stack) + stack = silc_stack_alloc(0, stack); + list = (SilcDList)silc_smalloc(stack, sizeof(*list)); + if (!list) { + silc_stack_free(stack); + return NULL; + } + list->stack = stack; list->current = list->prev = NULL; silc_list_init_prev(list->list, struct SilcDListEntryStruct, next, prev); @@ -102,7 +153,9 @@ SilcDList silc_dlist_init(void) * DESCRIPTION * * Uninits and frees all memory. Must be called to free memory. Does NOT - * free the contexts saved by caller. + * free the contexts saved by caller. If the silc_dlist_sinit was used + * with SilcStack this will release all memory allocated by the SilcDList + * back to the SilcStack. * ***/ @@ -111,12 +164,14 @@ void silc_dlist_uninit(SilcDList list) { if (list) { SilcDListEntry e; + SilcStack stack = list->stack; silc_list_start(list->list); while ((e = (SilcDListEntry)silc_list_get(list->list)) != SILC_LIST_END) { silc_list_del(list->list, e); - silc_free(e); + silc_sfree(stack, e); } - silc_free(list); + silc_sfree(stack, list); + silc_stack_free(stack); } } @@ -198,7 +253,7 @@ void silc_dlist_end(SilcDList list) static inline SilcBool silc_dlist_add(SilcDList list, void *context) { - SilcDListEntry e = (SilcDListEntry)silc_malloc(sizeof(*e)); + SilcDListEntry e = (SilcDListEntry)silc_smalloc(list->stack, sizeof(*e)); if (silc_unlikely(!e)) return FALSE; e->context = context; @@ -224,7 +279,7 @@ SilcBool silc_dlist_add(SilcDList list, void *context) static inline SilcBool silc_dlist_insert(SilcDList list, void *context) { - SilcDListEntry e = (SilcDListEntry)silc_malloc(sizeof(*e)); + SilcDListEntry e = (SilcDListEntry)silc_smalloc(list->stack, sizeof(*e)); if (silc_unlikely(!e)) return FALSE; e->context = context; @@ -261,7 +316,7 @@ void silc_dlist_del(SilcDList list, void *entry) list->current = NULL; if (list->prev == e) list->prev = NULL; - silc_free(e); + silc_sfree(list->stack, e); break; } }