From: Wouter Wijngaards Date: Wed, 14 Mar 2007 12:21:03 +0000 (+0000) Subject: lruhash remove routine. X-Git-Tag: release-0.2~49 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=06b25ffa5aecfe851f89ebeab1576733e3d95916;p=thirdparty%2Funbound.git lruhash remove routine. git-svn-id: file:///svn/unbound/trunk@178 be551aaa-1e26-0410-a405-d3ace91eadb9 --- diff --git a/doc/Changelog b/doc/Changelog index fcda773a2..ff622daec 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,5 +1,6 @@ 14 March 2007: Wouter - hash table insert (and subroutines) and lookup implemented. + - hash table remove. 13 March 2007: Wouter - lock_unprotect in checklocks. diff --git a/util/storage/lruhash.c b/util/storage/lruhash.c index de6fc7aa1..e173a5f98 100644 --- a/util/storage/lruhash.c +++ b/util/storage/lruhash.c @@ -138,10 +138,10 @@ bin_init(struct lruhash_bin* array, size_t size) } } -struct lruhash* lruhash_create(size_t start_size, size_t maxmem, - lruhash_sizefunc_t sizefunc, lruhash_compfunc_t compfunc, - lruhash_delkeyfunc_t delkeyfunc, lruhash_deldatafunc_t deldatafunc, - void* arg) +struct lruhash* +lruhash_create(size_t start_size, size_t maxmem, lruhash_sizefunc_t sizefunc, + lruhash_compfunc_t compfunc, lruhash_delkeyfunc_t delkeyfunc, + lruhash_deldatafunc_t deldatafunc, void* arg) { struct lruhash* table = (struct lruhash*)calloc(1, sizeof(struct lruhash)); @@ -173,7 +173,8 @@ struct lruhash* lruhash_create(size_t start_size, size_t maxmem, return table; } -static void bin_delete(struct lruhash* table, struct lruhash_bin* bin) +static void +bin_delete(struct lruhash* table, struct lruhash_bin* bin) { struct lruhash_entry* p, *np; if(!bin) @@ -217,7 +218,8 @@ bin_split(struct lruhash* table, struct lruhash_bin* newa, } } -void lruhash_delete(struct lruhash* table) +void +lruhash_delete(struct lruhash* table) { size_t i; if(!table) @@ -361,7 +363,8 @@ lru_touch(struct lruhash* table, struct lruhash_entry* entry) lru_front(table, entry); } -void lruhash_insert(struct lruhash* table, hashvalue_t hash, +void +lruhash_insert(struct lruhash* table, hashvalue_t hash, struct lruhash_entry* entry, void* data) { struct lruhash_bin* bin; @@ -408,10 +411,10 @@ void lruhash_insert(struct lruhash* table, hashvalue_t hash, } } -struct lruhash_entry* lruhash_lookup(struct lruhash* table, hashvalue_t hash, - void* key, int wr) +struct lruhash_entry* +lruhash_lookup(struct lruhash* table, hashvalue_t hash, void* key, int wr) { - struct lruhash_entry* entry = NULL; + struct lruhash_entry* entry; struct lruhash_bin* bin; lock_quick_lock(&table->lock); @@ -430,6 +433,28 @@ struct lruhash_entry* lruhash_lookup(struct lruhash* table, hashvalue_t hash, return entry; } -void lruhash_remove(struct lruhash* table, void* key) +void +lruhash_remove(struct lruhash* table, hashvalue_t hash, void* key) { + struct lruhash_entry* entry; + struct lruhash_bin* bin; + + lock_quick_lock(&table->lock); + bin = &table->array[hash & table->size_mask]; + lock_quick_lock(&bin->lock); + if((entry=bin_find_entry(table, bin, hash, key))) { + bin_overflow_remove(bin, entry); + lru_remove(table, entry); + } else { + lock_quick_unlock(&table->lock); + lock_quick_unlock(&bin->lock); + return; + } + lock_quick_unlock(&table->lock); + lock_rw_wrlock(&entry->lock); + lock_quick_unlock(&bin->lock); + /* finish removal */ + lock_rw_unlock(&entry->lock); + (*table->delkeyfunc)(entry->key, table->cb_arg); + (*table->deldatafunc)(entry->data, table->cb_arg); } diff --git a/util/storage/lruhash.h b/util/storage/lruhash.h index 8fd8da7eb..f4397bac7 100644 --- a/util/storage/lruhash.h +++ b/util/storage/lruhash.h @@ -270,8 +270,9 @@ struct lruhash_entry* lruhash_lookup(struct lruhash* table, hashvalue_t hash, * Remove entry from hashtable. Does nothing if not found in hashtable. * Delfunc is called for the entry. * @param table: hash table. + * @param hash: hash of key. * @param key: what to look for. */ -void lruhash_remove(struct lruhash* table, void* key); +void lruhash_remove(struct lruhash* table, hashvalue_t hash, void* key); #endif /* UTIL_STORAGE_LRUHASH_H */