From 2725ad55fc03eb05f401d8d91246caaa0afa4bb9 Mon Sep 17 00:00:00 2001 From: Wouter Wijngaards Date: Wed, 31 Oct 2007 16:15:44 +0000 Subject: [PATCH] hash clear function. git-svn-id: file:///svn/unbound/trunk@725 be551aaa-1e26-0410-a405-d3ace91eadb9 --- doc/Changelog | 1 + testcode/unitlruhash.c | 5 +++++ testcode/unitslabhash.c | 5 +++++ util/storage/lruhash.c | 39 +++++++++++++++++++++++++++++++++++++++ util/storage/lruhash.h | 7 +++++++ util/storage/slabhash.c | 9 +++++++++ util/storage/slabhash.h | 6 ++++++ 7 files changed, 72 insertions(+) diff --git a/doc/Changelog b/doc/Changelog index 55b646a98..6942f1691 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -9,6 +9,7 @@ sort per reference key pointer, not on referencepointer itself. - pidfile: "/etc/unbound/unbound.pid" is now the default. - tests changed to reflect the updated default. + - created hashtable clear() function that respects locks. 30 October 2007: Wouter - fixup assertion failure that relied on compressed names to be diff --git a/testcode/unitlruhash.c b/testcode/unitlruhash.c index 9afd24cbc..dbf172e6d 100644 --- a/testcode/unitlruhash.c +++ b/testcode/unitlruhash.c @@ -371,6 +371,11 @@ test_long_table(struct lruhash* table) srandom(48); for(i=0; i<1000; i++) { /* what to do? */ + if(i == 500) { + lruhash_clear(table); + memset(ref, 0, sizeof(ref)); + continue; + } switch(random() % 4) { case 0: case 3: diff --git a/testcode/unitslabhash.c b/testcode/unitslabhash.c index 52dc30f26..cfb2fe738 100644 --- a/testcode/unitslabhash.c +++ b/testcode/unitslabhash.c @@ -251,6 +251,11 @@ test_long_table(struct slabhash* table) srandom(48); for(i=0; i<1000; i++) { /* what to do? */ + if(i == 500) { + slabhash_clear(table); + memset(ref, 0, sizeof(ref)); + continue; + } switch(random() % 4) { case 0: case 3: diff --git a/util/storage/lruhash.c b/util/storage/lruhash.c index fe1dbf2a0..3f33e7f76 100644 --- a/util/storage/lruhash.c +++ b/util/storage/lruhash.c @@ -402,6 +402,45 @@ lruhash_remove(struct lruhash* table, hashvalue_t hash, void* key) (*table->deldatafunc)(d, table->cb_arg); } +/** clear bin, respecting locks, does not do space, LRU */ +static void +bin_clear(struct lruhash* table, struct lruhash_bin* bin) +{ + struct lruhash_entry* p, *np; + void *d; + lock_quick_lock(&bin->lock); + p = bin->overflow_list; + while(p) { + lock_rw_wrlock(&p->lock); + np = p->overflow_next; + d = p->data; + (*table->delkeyfunc)(p->key, table->cb_arg, 1); + (*table->deldatafunc)(d, table->cb_arg); + p = np; + } + bin->overflow_list = NULL; + lock_quick_unlock(&bin->lock); +} + +void +lruhash_clear(struct lruhash* table) +{ + size_t i; + log_assert(fptr_whitelist_hash_delkeyfunc(table->delkeyfunc)); + log_assert(fptr_whitelist_hash_deldatafunc(table->deldatafunc)); + if(!table) + return; + + lock_quick_lock(&table->lock); + for(i=0; isize; i++) { + bin_clear(table, &table->array[i]); + } + table->lru_start = NULL; + table->lru_end = NULL; + table->num = 0; + table->space_used = 0; + lock_quick_unlock(&table->lock); +} void lruhash_status(struct lruhash* table, const char* id, int extended) diff --git a/util/storage/lruhash.h b/util/storage/lruhash.h index 035f4a49a..d1ea80588 100644 --- a/util/storage/lruhash.h +++ b/util/storage/lruhash.h @@ -243,6 +243,13 @@ struct lruhash* lruhash_create(size_t start_size, size_t maxmem, */ void lruhash_delete(struct lruhash* table); +/** + * Clear hash table. Entries are all deleted, while locking them before + * doing so. At end the table is empty. + * @param table: to make empty. + */ +void lruhash_clear(struct lruhash* table); + /** * Insert a new element into the hashtable. * If key is already present data pointer in that entry is updated. diff --git a/util/storage/slabhash.c b/util/storage/slabhash.c index 6683acf52..8e7bb0b61 100644 --- a/util/storage/slabhash.c +++ b/util/storage/slabhash.c @@ -97,6 +97,15 @@ void slabhash_delete(struct slabhash* sl) free(sl); } +void slabhash_clear(struct slabhash* sl) +{ + size_t i; + if(!sl) + return; + for(i=0; isize; i++) + lruhash_clear(sl->array[i]); +} + /** helper routine to calculate the slabhash index */ static unsigned int slab_idx(struct slabhash* sl, hashvalue_t hash) diff --git a/util/storage/slabhash.h b/util/storage/slabhash.h index c5f803231..a7757bd77 100644 --- a/util/storage/slabhash.h +++ b/util/storage/slabhash.h @@ -90,6 +90,12 @@ struct slabhash* slabhash_create(size_t numtables, size_t start_size, */ void slabhash_delete(struct slabhash* table); +/** + * Clear hash table. Entries are all deleted. + * @param table: to make empty. + */ +void slabhash_clear(struct slabhash* table); + /** * Insert a new element into the hashtable, uses lruhash_insert. * If key is already present data pointer in that entry is updated. -- 2.47.2