X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcdlist.h;h=87dfb4cce6cb26871b4602d23153cd55a0e8934d;hb=52e57c880aba9c5e89f59d962eb9af75670b76e0;hp=6966f304a0719faec9c18a5090a0b4971b111c1c;hpb=413da0f8686910f5e627393157566ae729ca99c4;p=silc.git diff --git a/lib/silcutil/silcdlist.h b/lib/silcutil/silcdlist.h index 6966f304..87dfb4cc 100644 --- a/lib/silcutil/silcdlist.h +++ b/lib/silcutil/silcdlist.h @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 2000 - 2003 Pekka Riikonen + Copyright (C) 2000 - 2007 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -30,13 +30,11 @@ * will automatically allocate list entries. Normal SILC List API 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. + * SilcDList. But SilcDList is a bit slower than SilcList because it + * requires memory allocation when adding new entries to the list. * - * This is slower than SilcList because this requires one extra memory - * allocation when adding new entries to the list. The context is probably - * allocated already and the new list entry requires one additional memory - * allocation. The memory allocation and freeing is done automatically in - * the API and does not show to the caller. + * SILC Dynamic List is not thread-safe. If the same list context must be + * used in multithreaded environment concurrency control must be employed. * ***/ @@ -52,12 +50,12 @@ * Application defines this object and adds contexts to this list with * Dynamic List Interface functions. * - * SOURCE - */ + ***/ typedef struct SilcDListStruct { SilcList list; + void *current; + void *prev; } *SilcDList; -/***/ /* SilcDListEntry structure, one entry in the list. This MUST NOT be used directly by the application. */ @@ -88,6 +86,7 @@ SilcDList silc_dlist_init(void) list = (SilcDList)silc_malloc(sizeof(*list)); if (!list) return NULL; + list->current = list->prev = NULL; silc_list_init_prev(list->list, struct SilcDListEntryStruct, next, prev); return list; @@ -158,6 +157,7 @@ static inline void silc_dlist_start(SilcDList list) { silc_list_start(list->list); + list->current = list->prev = NULL; } /****f* silcutil/SilcDListAPI/silc_dlist_end @@ -178,6 +178,7 @@ static inline void silc_dlist_end(SilcDList list) { silc_list_end(list->list); + list->current = list->prev = NULL; } /****f* silcutil/SilcDListAPI/silc_dlist_add @@ -185,7 +186,7 @@ void silc_dlist_end(SilcDList list) * SYNOPSIS * * static inline - * void silc_dlist_add(SilcDList list, void *context); + * SilcBool silc_dlist_add(SilcDList list, void *context); * * DESCRIPTION * @@ -195,13 +196,40 @@ void silc_dlist_end(SilcDList list) ***/ static inline -void silc_dlist_add(SilcDList list, void *context) +SilcBool silc_dlist_add(SilcDList list, void *context) { SilcDListEntry e = (SilcDListEntry)silc_malloc(sizeof(*e)); - if (!e) - return; + if (silc_unlikely(!e)) + return FALSE; e->context = context; silc_list_add(list->list, e); + return TRUE; +} + +/****f* silcutil/SilcDList/silc_dlist_insert + * + * SYNOPSIS + * + * static inline + * SilcBool silc_dlist_insert(SilcDList list, void *context); + * + * DESCRIPTION + * + * Insert new entry to the list between current and previous entry. + * If list is at the start this adds the entry at head of the list. + * Use silc_dlist_add to add at the end of the list. + * + ***/ + +static inline +SilcBool silc_dlist_insert(SilcDList list, void *context) +{ + SilcDListEntry e = (SilcDListEntry)silc_malloc(sizeof(*e)); + if (silc_unlikely(!e)) + return FALSE; + e->context = context; + silc_list_insert(list->list, list->prev, e); + return TRUE; } /****f* silcutil/SilcDListAPI/silc_dlist_del @@ -209,26 +237,30 @@ void silc_dlist_add(SilcDList list, void *context) * SYNOPSIS * * static inline - * void silc_dlist_del(SilcDList list, void *context); + * void silc_dlist_del(SilcDList list, void *entry); * * DESCRIPTION * - * Remove entry from the list. Returns < 0 on error, 0 otherwise. + * Remove entry from the list. * ***/ static inline -void silc_dlist_del(SilcDList list, void *context) +void silc_dlist_del(SilcDList list, void *entry) { SilcDListEntry e; silc_list_start(list->list); while ((e = (SilcDListEntry)silc_list_get(list->list)) != SILC_LIST_END) { - if (e->context == context) { + if (e->context == entry) { silc_list_del(list->list, e); #if defined(SILC_DEBUG) memset(e, 'F', sizeof(*e)); #endif + if (list->current == e) + list->current = NULL; + if (list->prev == e) + list->prev = NULL; silc_free(e); break; } @@ -263,7 +295,9 @@ void silc_dlist_del(SilcDList list, void *context) static inline void *silc_dlist_get(SilcDList list) { - SilcDListEntry e = (SilcDListEntry)silc_list_get(list->list); + SilcDListEntry e; + list->prev = list->current; + list->current = e = (SilcDListEntry)silc_list_get(list->list); if (e != SILC_LIST_END) return e->context; return SILC_LIST_END;