]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
hash clear function.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Wed, 31 Oct 2007 16:15:44 +0000 (16:15 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Wed, 31 Oct 2007 16:15:44 +0000 (16:15 +0000)
git-svn-id: file:///svn/unbound/trunk@725 be551aaa-1e26-0410-a405-d3ace91eadb9

doc/Changelog
testcode/unitlruhash.c
testcode/unitslabhash.c
util/storage/lruhash.c
util/storage/lruhash.h
util/storage/slabhash.c
util/storage/slabhash.h

index 55b646a98b643a320e2f08a2b45ae4ae7ffdc344..6942f1691a87303cdedb3e6cf64a1fa10e32d232 100644 (file)
@@ -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
index 9afd24cbcb20a44df556d92f77abbec504615cb0..dbf172e6dd0e6ebce063181e0c5ba260d5a04b6f 100644 (file)
@@ -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:
index 52dc30f269d538371c64fcf72bfb329974526935..cfb2fe7380b3b7d1fc5cb2e44a6f86f6fb2ce65f 100644 (file)
@@ -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:
index fe1dbf2a0a2ce765b01dccde5e58358195d488e8..3f33e7f76b18a43210a1ce1c91aa3c3ed6f2b9ea 100644 (file)
@@ -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; i<table->size; 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)
index 035f4a49a15d19945c7aefc41e7b25ec345c472a..d1ea80588633aabe7a89b907a43eeae4d6b2948f 100644 (file)
@@ -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.
index 6683acf52b76abbc835fb77cd8bf8237b2ca9121..8e7bb0b610f66555c081ce3ccebc3262f0a9bb41 100644 (file)
@@ -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; i<sl->size; i++)
+               lruhash_clear(sl->array[i]);
+}
+
 /** helper routine to calculate the slabhash index */
 static unsigned int
 slab_idx(struct slabhash* sl, hashvalue_t hash)
index c5f8032318817c7609a0cbfd20b863a345ab49bd..a7757bd77957e1744ec895f8212a590c6ad8095f 100644 (file)
@@ -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.