]> pd.if.org Git - nbds/blobdiff - struct/ht.c
fix resize and copy bugs
[nbds] / struct / ht.c
index 8d5e1600e3ad6d5441bcec4baf4c52fa6fff6b33..704443550f686776b5d98ff887163b7e8659c607 100644 (file)
@@ -16,7 +16,6 @@
 #include "ht.h"
 #include "murmur.h"
 #include "mem.h"
-#include "rcu.h"
 
 #define COPIED_VALUE            (-1)
 #define TOMBSTONE               STRIP_TAG(COPIED_VALUE)
 #define MIN_SCALE               (__builtin_ctz(ENTRIES_PER_BUCKET) + 2) // min 4 buckets
 #define MAX_BUCKETS_TO_PROBE    250
 
+#define GET_PTR(x) ((string_t *)((x) & MASK(48))) // low-order 48 bits is a pointer to a string_t
+
 typedef struct ht_entry {
-    int64_t key;
-    int64_t value;
+    uint64_t key;
+    uint64_t value;
 } entry_t;
 
 typedef struct string {
@@ -49,7 +50,7 @@ typedef struct hash_table_i {
 } hash_table_i_t;
 
 static int hti_copy_entry 
-    (hash_table_i_t *old_hti, volatile entry_t *e, uint32_t e_key_hash, hash_table_i_t *new_hti);
+    (hash_table_i_t *ht1, volatile entry_t *e, uint32_t e_key_hash, hash_table_i_t *ht2);
 
 // Choose the next bucket to probe using the high-order bits of <key_hash>.
 static inline int get_next_ndx(int old_ndx, uint32_t key_hash, int ht_scale) {
@@ -66,7 +67,7 @@ static inline int get_next_ndx(int old_ndx, uint32_t key_hash, int ht_scale) {
 static inline int ht_key_equals (uint64_t a, uint32_t b_hash, const char *b_value, uint32_t b_len) {
     if ((b_hash >> 16) != (a >> 48)) // high-order 16 bits are from the hash value
         return FALSE;
-    const string_t *a_key = (string_t *)(a & MASK(48)); // low-order 48 bits is a pointer 
+    const string_t *a_key = GET_PTR(a); 
     assert(a_key);
     return a_key->len == b_len && memcmp(a_key->val, b_value, b_len) == 0;
 }
@@ -80,34 +81,31 @@ static inline int ht_key_equals (uint64_t a, uint32_t b_hash, const char *b_valu
 // Record if the entry being returned is empty. Otherwise the caller will have to waste time with
 // ht_key_equals() to confirm that it did not lose a race to fill an empty entry.
 static volatile entry_t *hti_lookup (hash_table_i_t *hti, uint32_t key_hash, const char *key_val, uint32_t key_len, int *is_empty) {
-    TRACE("h0", "hti_lookup(key \"%s\" in hti %p)", key_val, hti);
+    TRACE("h0", "hti_lookup(key %p in hti %p)", key_val, hti);
     *is_empty = 0;
 
     // Probe one cache line at a time
     int ndx = key_hash & MASK(hti->scale); // the first entry to search
-    int i;
-    for (i = 0; i < hti->max_probe; ++i) {
+    for (int i = 0; i < hti->max_probe; ++i) {
 
         // The start of the bucket is the first entry in the cache line.
         volatile entry_t *bucket = hti->table + (ndx & ~(ENTRIES_PER_BUCKET-1)); 
 
         // Start searching at the indexed entry. Then loop around to the begining of the cache line.
-        int j;
-        for (j = 0; j < ENTRIES_PER_BUCKET; ++j) {
+        for (int j = 0; j < ENTRIES_PER_BUCKET; ++j) {
             volatile entry_t *e = bucket + ((ndx + j) & (ENTRIES_PER_BUCKET-1));
 
             uint64_t e_key = e->key;
             if (e_key == DOES_NOT_EXIST) {
-                TRACE("h0", "hti_lookup: empty entry %p found on probe %d", e, i*ENTRIES_PER_BUCKET+j+1);
-                // we tag the pointer so the caller can avoid an expensive ht_key_equals()
-                *is_empty = 1;
+                TRACE("h0", "hti_lookup: empty entry %p found", e, 0);
+                *is_empty = 1; // indicate an empty so the caller avoids an expensive ht_key_equals
                 return e;
             }
 
             if (ht_key_equals(e_key, key_hash, key_val, key_len)) {
-                TRACE("h0", "hti_lookup: entry %p found on probe %d", e, i*ENTRIES_PER_BUCKET+j+1);
-                TRACE("h0", "hti_lookup: with key \"%s\" value %p"
-                            ((string_t *)(e_key & MASK(48)))->val, e->value);
+                TRACE("h0", "hti_lookup: entry %p found value %p", e, e->value);
+                TRACE("h0", "hti_lookup: entry key %p len %llu", GET_PTR(e_key)->val
+                                                                 GET_PTR(e_key)->len);
                 return e;
             }
         }
@@ -153,9 +151,7 @@ static hash_table_i_t *hti_alloc (hash_table_t *parent, int scale) {
 //
 // Initiates a copy by creating a larger hash_table_i_t and installing it in <hti->next>.
 static void hti_start_copy (hash_table_i_t *hti) {
-    TRACE("h0", "hti_start_copy(hti %p hti->next %p)", hti, hti->next);
-    if (hti->next) 
-        return; // another thread beat us to it
+    TRACE("h0", "hti_start_copy(hti %p scale %llu)", hti, hti->scale);
 
     // heuristics to determine the size of the new table
     uint64_t count = ht_count(hti->ht);
@@ -164,7 +160,8 @@ static void hti_start_copy (hash_table_i_t *hti) {
     new_scale += (count > (1 << (new_scale - 2))); // double size again if more than 1/2 full
 
     // Allocate the new table and attempt to install it.
-    hash_table_i_t *next = hti_alloc(hti->ht, hti->scale + 1);
+    hash_table_i_t *next = hti_alloc(hti->ht, new_scale);
+    TRACE("h0", "hti_start_copy: new hti %p scale %llu", next->scale, next->scale);
     hash_table_i_t *old_next = SYNC_CAS(&hti->next, NULL, next);
     if (old_next != NULL) {
         TRACE("h0", "hti_start_copy: lost race to install new hti; found %p", old_next, 0);
@@ -175,31 +172,32 @@ static void hti_start_copy (hash_table_i_t *hti) {
     TRACE("h0", "hti_start_copy: new hti is %p", next, 0);
 }
 
-// Copy the key and value stored in <old_e> (which must be an entry in <old_hti>) to <new_hti>. 
+// Copy the key and value stored in <ht1_e> (which must be an entry in <ht1>) to <ht2>. 
 //
-// Return 1 unless <old_e> is already copied (then return 0), so the caller can account for the total
+// Return 1 unless <ht1_e> is already copied (then return 0), so the caller can account for the total
 // number of entries left to copy.
-static int hti_copy_entry (hash_table_i_t *old_hti, volatile entry_t *old_e, uint32_t key_hash, 
-                           hash_table_i_t *new_hti) {
-    TRACE("h0", "hti_copy_entry(copy entry from %p to %p)", old_hti, new_hti);
-    assert(old_hti);
-    assert(old_hti->next);
-    assert(new_hti);
-    assert(old_e >= old_hti->table && old_e < old_hti->table + (1 << old_hti->scale));
-
-    int64_t old_e_value = old_e->value;
-    TRACE("h0", "hti_copy_entry: entry %p current value %p", old_e, old_e_value);
-    if (EXPECT_FALSE(old_e_value == COPIED_VALUE))
+static int hti_copy_entry (hash_table_i_t *ht1, volatile entry_t *ht1_e, uint32_t key_hash, 
+                           hash_table_i_t *ht2) {
+    TRACE("h0", "hti_copy_entry(copy entry from %p to %p)", ht1, ht2);
+    assert(ht1);
+    assert(ht1->next);
+    assert(ht2);
+    assert(ht1_e >= ht1->table && ht1_e < ht1->table + (1 << ht1->scale));
+    assert(key_hash == 0 || (key_hash >> 16) == (ht1_e->key >> 48));
+
+    uint64_t ht1_e_value = ht1_e->value;
+    TRACE("h0", "hti_copy_entry: entry %p current value %p", ht1_e, ht1_e_value);
+    if (EXPECT_FALSE(ht1_e_value == COPIED_VALUE))
         return FALSE; // already copied
 
     // Kill empty entries.
-    if (EXPECT_FALSE(old_e_value == DOES_NOT_EXIST)) {
-        old_e_value = SYNC_CAS(&old_e->value, DOES_NOT_EXIST, COPIED_VALUE);
-        if (old_e_value == DOES_NOT_EXIST) {
+    if (EXPECT_FALSE(ht1_e_value == DOES_NOT_EXIST)) {
+        uint64_t ht1_e_value = SYNC_CAS(&ht1_e->value, DOES_NOT_EXIST, COPIED_VALUE);
+        if (ht1_e_value == DOES_NOT_EXIST) {
             TRACE("h0", "hti_copy_entry: old entry killed", 0, 0);
             return TRUE;
         }
-        if (old_e_value == COPIED_VALUE) {
+        if (ht1_e_value == COPIED_VALUE) {
             TRACE("h0", "hti_copy_entry: lost race to kill empty entry in old hti", 0, 0);
             return FALSE; // another thread beat us to it
         }
@@ -208,72 +206,74 @@ static int hti_copy_entry (hash_table_i_t *old_hti, volatile entry_t *old_e, uin
     }
 
     // Tag the value in the old entry to indicate a copy is in progress.
-    old_e_value = SYNC_FETCH_AND_OR(&old_e->value, TAG_VALUE(0));
-    TRACE("h0", "hti_copy_entry: tagged the value %p in old entry %p", old_e_value, old_e);
-    if (old_e_value == COPIED_VALUE) 
+    ht1_e_value = SYNC_FETCH_AND_OR(&ht1_e->value, TAG_VALUE(0));
+    TRACE("h0", "hti_copy_entry: tagged the value %p in old entry %p", ht1_e_value, ht1_e);
+    if (ht1_e_value == COPIED_VALUE) 
         return FALSE; // <value> was already copied by another thread.
 
     // Deleted entries don't need to be installed into to the new table, but their keys do need to
     // be freed.
     assert(COPIED_VALUE == TAG_VALUE(TOMBSTONE));
-    if (old_e_value == TOMBSTONE) {
-        nbd_free((string_t *)(old_e->key & MASK(48)));
+    if (ht1_e_value == TOMBSTONE) {
+        nbd_defer_free(GET_PTR(ht1_e->key));
         return TRUE; 
     }
-    old_e_value = STRIP_TAG(old_e_value);
 
     // Install the key in the new table.
-    uint64_t old_e_key = old_e->key;
-    string_t *key = (string_t *)(old_e_key & MASK(48));
-    TRACE("h0", "hti_copy_entry: key %p is %s", old_e_key, key->val);
+    uint64_t key = ht1_e->key;
+    string_t *key_string = GET_PTR(key);
+    uint64_t value = STRIP_TAG(ht1_e_value);
+    TRACE("h0", "hti_copy_entry: key %p value %p", key, value);
 
     // We use 0 to indicate that <key_hash> isn't initiallized. Occasionally the <key_hash> will
     // really be 0 and we will waste time recomputing it. That is rare enough that it is OK. 
     if (key_hash == 0) { 
-        key_hash = murmur32(key->val, key->len);
+        key_hash = murmur32(key_string->val, key_string->len);
     }
 
     int is_empty;
-    volatile entry_t *new_e = hti_lookup(new_hti, key_hash, key->val, key->len, &is_empty);
+    volatile entry_t *ht2_e = hti_lookup(ht2, key_hash, key_string->val, key_string->len, &is_empty);
 
     // it is possible that there is not any room in the new table either
-    if (EXPECT_FALSE(new_e == NULL)) {
-        hti_start_copy(new_hti); // initiate nested copy, if not already started
-        return hti_copy_entry(old_hti, old_e, key_hash, new_hti->next); // recursive tail-call
+    if (EXPECT_FALSE(ht2_e == NULL)) {
+        if (ht2->next == NULL) {
+            hti_start_copy(ht2); // initiate nested copy, if not already started
+        }
+        return hti_copy_entry(ht1, ht1_e, key_hash, ht2->next); // recursive tail-call
     }
 
     // a tagged entry returned from hti_lookup() means it is either empty or has a new key
     if (is_empty) {
-        uint64_t new_e_key = SYNC_CAS(&new_e->key, DOES_NOT_EXIST, old_e_key);
-        if (new_e_key != DOES_NOT_EXIST) {
+        uint64_t old_ht2_e_key = SYNC_CAS(&ht2_e->key, DOES_NOT_EXIST, key);
+        if (old_ht2_e_key != DOES_NOT_EXIST) {
             TRACE("h0", "hti_copy_entry: lost race to CAS key %p into new entry; found %p",
-                    old_e_key, new_e_key);
-            return hti_copy_entry(old_hti, old_e, key_hash, new_hti); // recursive tail-call
+                    key, old_ht2_e_key);
+            return hti_copy_entry(ht1, ht1_e, key_hash, ht2); // recursive tail-call
         }
     }
-    assert(ht_key_equals(new_e->key, key_hash, key->val, key->len));
-    TRACE("h0", "hti_copy_entry: key %p installed in new old hti %p", key->val, new_hti);
+    assert(ht_key_equals(ht2_e->key, key_hash, key_string->val, key_string->len));
+    TRACE("h0", "hti_copy_entry: key %p installed in new hti %p", key_string->val, ht2);
 
     // Copy the value to the entry in the new table.
-    uint64_t new_e_value = SYNC_CAS(&new_e->value, DOES_NOT_EXIST, old_e_value);
+    uint64_t old_ht2_e_value = SYNC_CAS(&ht2_e->value, DOES_NOT_EXIST, value);
 
     // If there is a nested copy in progress, we might have installed the key into a dead entry.
-    if (new_e_value == COPIED_VALUE)
-        return hti_copy_entry(old_hti, old_e, key_hash, new_hti->next); // recursive tail-call
+    if (old_ht2_e_value == COPIED_VALUE)
+        return hti_copy_entry(ht1, ht1_e, key_hash, ht2->next); // recursive tail-call
 
     // Mark the old entry as dead.
-    old_e->value = COPIED_VALUE;
+    ht1_e->value = COPIED_VALUE;
 
     // Update the count if we were the one that completed the copy.
-    if (new_e_value == DOES_NOT_EXIST) {
-        TRACE("h0", "hti_copy_entry: value %p installed in new hti %p", old_e_value, new_hti);
-        SYNC_ADD(&old_hti->count, -1);
-        SYNC_ADD(&new_hti->count, 1);
+    if (old_ht2_e_value == DOES_NOT_EXIST) {
+        TRACE("h0", "hti_copy_entry: value %p installed in new hti %p", value, ht2);
+        SYNC_ADD(&ht1->count, -1);
+        SYNC_ADD(&ht2->count, 1);
         return TRUE;
     }
 
     TRACE("h0", "hti_copy_entry: lost race to CAS value %p in new hti; found %p", 
-                old_e_value, new_e_value);
+                value, old_ht2_e_value);
     return FALSE; // another thread completed the copy
 }
 
@@ -291,10 +291,10 @@ static int hti_copy_entry (hash_table_i_t *old_hti, volatile entry_t *old_e, uin
 // real value matches (i.e. not a TOMBSTONE or DOES_NOT_EXIST) as long as <key> is in the table. If
 // <expected> is HT_EXPECT_WHATEVER then skip the test entirely.
 //
-static int64_t hti_compare_and_set (hash_table_i_t *hti, uint32_t key_hash, const char *key_val, 
-                                    uint32_t key_len, int64_t expected, int64_t new) {
-    TRACE("h0", "hti_compare_and_set(hti %p key \"%s\")", hti, key_val);
-    TRACE("h0", "hti_compare_and_set(new value %p; caller expects value %p)", new, expected);
+static uint64_t hti_compare_and_set (hash_table_i_t *hti, uint32_t key_hash, const char *key_val, 
+                                    uint32_t key_len, uint64_t expected, uint64_t new) {
+    TRACE("h0", "hti_compare_and_set: hti %p key %p", hti, key_val);
+    TRACE("h0", "hti_compare_and_set: new value %p expected old value %p", new, expected);
     assert(hti);
     assert(new != DOES_NOT_EXIST && !IS_TAGGED(new));
     assert(key_val);
@@ -304,7 +304,9 @@ static int64_t hti_compare_and_set (hash_table_i_t *hti, uint32_t key_hash, cons
 
     // There is no room for <key>, grow the table and try again.
     if (e == NULL) {
-        hti_start_copy(hti);
+        if (hti->next == NULL) {
+            hti_start_copy(hti);
+        }
         return COPIED_VALUE;
     }
 
@@ -318,26 +320,27 @@ static int64_t hti_compare_and_set (hash_table_i_t *hti, uint32_t key_hash, cons
         if (new == TOMBSTONE)
             return DOES_NOT_EXIST;
 
-        // allocate <key>.
+        // Allocate <key>.
         string_t *key = nbd_malloc(sizeof(uint32_t) + key_len);
         key->len = key_len;
         memcpy(key->val, key_val, key_len);
 
-        // CAS <key> into the table. 
-        uint64_t e_key = SYNC_CAS(&e->key, DOES_NOT_EXIST, ((uint64_t)(key_hash >> 16) << 48) | (uint64_t)key);
+        // Combine <key> pointer with bits from its hash, CAS it into the table. 
+        uint64_t temp = ((uint64_t)(key_hash >> 16) << 48) | (uint64_t)key; 
+        uint64_t e_key = SYNC_CAS(&e->key, DOES_NOT_EXIST, temp);
 
         // Retry if another thread stole the entry out from under us.
         if (e_key != DOES_NOT_EXIST) {
-            TRACE("h0", "hti_compare_and_set: key in entry %p is \"%s\"", e, e_key & MASK(48));
-            TRACE("h0", "hti_compare_and_set: lost race to install key \"%s\" in %p", key->val, e);
+            TRACE("h0", "hti_compare_and_set: lost race to install key %p in entry %p", key, e);
+            TRACE("h0", "hti_compare_and_set: found %p instead of NULL", GET_PTR(e_key), 0);
             nbd_free(key);
             return hti_compare_and_set(hti, key_hash, key_val, key_len, expected, new); // tail-call
         }
-        TRACE("h0", "hti_compare_and_set: installed key \"%s\" in entry %p", key_val, e);
+        TRACE("h0", "hti_compare_and_set: installed key %p in entry %p", key, e);
     }
 
     // If the entry is in the middle of a copy, the copy must be completed first.
-    int64_t e_value = e->value;
+    uint64_t e_value = e->value;
     TRACE("h0", "hti_compare_and_set: value in entry %p is %p", e, e_value);
     if (EXPECT_FALSE(IS_TAGGED(e_value))) {
         int did_copy = hti_copy_entry(hti, e, key_hash, ((volatile hash_table_i_t *)hti)->next);
@@ -351,8 +354,8 @@ static int64_t hti_compare_and_set (hash_table_i_t *hti, uint32_t key_hash, cons
     int old_existed = (e_value != TOMBSTONE && e_value != DOES_NOT_EXIST);
     if (EXPECT_FALSE(expected != HT_EXPECT_WHATEVER && expected != e_value)) {
         if (EXPECT_FALSE(expected != (old_existed ? HT_EXPECT_EXISTS : HT_EXPECT_NOT_EXISTS))) {
-            TRACE("h0", "hti_compare_and_set: value expected by caller for key \"%s\" not found; "
-                        "found value %p", key_val, e_value);
+            TRACE("h0", "hti_compare_and_set: value %p expected by caller not found; found value %p",
+                        expected, e_value);
             return e_value;
         }
     }
@@ -377,7 +380,7 @@ static int64_t hti_compare_and_set (hash_table_i_t *hti, uint32_t key_hash, cons
 }
 
 //
-static int64_t hti_get (hash_table_i_t *hti, uint32_t key_hash, const char *key_val, uint32_t key_len) {
+static uint64_t hti_get (hash_table_i_t *hti, uint32_t key_hash, const char *key_val, uint32_t key_len) {
     assert(key_val);
 
     int is_empty;
@@ -396,7 +399,7 @@ static int64_t hti_get (hash_table_i_t *hti, uint32_t key_hash, const char *key_
         return DOES_NOT_EXIST;
 
     // If the entry is being copied, finish the copy and retry on the next table.
-    int64_t e_value = e->value;
+    uint64_t e_value = e->value;
     if (EXPECT_FALSE(IS_TAGGED(e_value))) {
         if (EXPECT_FALSE(e_value != COPIED_VALUE)) {
             int did_copy = hti_copy_entry(hti, e, key_hash, ((volatile hash_table_i_t *)hti)->next);
@@ -411,14 +414,16 @@ static int64_t hti_get (hash_table_i_t *hti, uint32_t key_hash, const char *key_
 }
 
 //
-int64_t ht_get (hash_table_t *ht, const char *key_val, uint32_t key_len) {
+uint64_t ht_get (hash_table_t *ht, const char *key_val, uint32_t key_len) {
     return hti_get(*ht, murmur32(key_val, key_len), key_val, key_len);
 }
 
 //
-int64_t ht_compare_and_set (hash_table_t *ht, const char *key_val, uint32_t key_len, 
-                            int64_t expected_val, int64_t new_val) {
+uint64_t ht_compare_and_set (hash_table_t *ht, const char *key_val, uint32_t key_len, 
+                            uint64_t expected_val, uint64_t new_val) {
 
+    TRACE("h0", "ht_compare_and_set: key %p len %u", key_val, key_len);
+    TRACE("h0", "ht_compare_and_set: expected val %p new val %p", expected_val, new_val);
     assert(key_val);
     assert(!IS_TAGGED(new_val) && new_val != DOES_NOT_EXIST);
 
@@ -426,38 +431,51 @@ int64_t ht_compare_and_set (hash_table_t *ht, const char *key_val, uint32_t key_
 
     // Help with an ongoing copy.
     if (EXPECT_FALSE(hti->next != NULL)) {
-
-        // Reserve some entries for this thread to copy. There is a race condition here because the
-        // fetch and add isn't atomic, but that is ok.
+        volatile entry_t *e;
+        uint64_t limit; 
+        int num_copied = 0;
         int x = hti->scan; 
-        hti->scan = x + ENTRIES_PER_COPY_CHUNK; 
 
-        // <hti->scan> might be larger than the size of the table, if some thread stalls while 
-        // copying. In that case we just wrap around to the begining and make another pass through
-        // the table.
-        volatile entry_t *e = hti->table + (x & MASK(hti->scale));
+        TRACE("h0", "ht_compare_and_set: help copy. scan is %llu, size is %llu", x, 1<<hti->scale);
+        // Panic if we've been around the array twice and still haven't finished the copy.
+        int panic = (x >= (1 << (hti->scale + 1))); 
+        if (!panic) {
+            limit = ENTRIES_PER_COPY_CHUNK;
+
+            // Reserve some entries for this thread to copy. There is a race condition here because the
+            // fetch and add isn't atomic, but that is ok.
+            hti->scan = x + ENTRIES_PER_COPY_CHUNK; 
+
+            // <hti->scan> might be larger than the size of the table, if some thread stalls while 
+            // copying. In that case we just wrap around to the begining and make another pass through
+            // the table.
+            e = hti->table + (x & MASK(hti->scale));
+        } else {
+            TRACE("h0", "ht_compare_and_set: help copy panic", 0, 0);
+            // scan the whole table
+            limit = (1 << hti->scale);
+            e = hti->table;
+        }
 
         // Copy the entries
-        int num_copied = 0, i;
-        for (i = 0; i < ENTRIES_PER_COPY_CHUNK; ++i) {
+        for (int i = 0; i < limit; ++i) {
             num_copied += hti_copy_entry(hti, e++, 0, hti->next);
             assert(e <= hti->table + (1 << hti->scale));
         }
         if (num_copied != 0) {
             SYNC_ADD(&hti->num_entries_copied, num_copied);
         }
-    }
 
-    // Dispose of fully copied tables.
-    while (hti->num_entries_copied == (1 << hti->scale)) {
-        assert(hti->next);
-        if (SYNC_CAS(ht, hti, hti->next) == hti) {
-            nbd_defer_free(hti); 
+        // Dispose of fully copied tables.
+        if (hti->num_entries_copied == (1 << hti->scale) || panic) {
+            assert(hti->next);
+            if (SYNC_CAS(ht, hti, hti->next) == hti) {
+                nbd_defer_free(hti); 
+            }
         }
-        hti = *ht;
     }
 
-    int64_t old_val;
+    uint64_t old_val;
     uint32_t key_hash = murmur32(key_val, key_len);
     while ((old_val = hti_compare_and_set(hti, key_hash, key_val, key_len, expected_val, new_val)) 
            == COPIED_VALUE) {
@@ -470,9 +488,9 @@ int64_t ht_compare_and_set (hash_table_t *ht, const char *key_val, uint32_t key_
 
 // Remove the value in <ht> associated with <key_val>. Returns the value removed, or 
 // DOES_NOT_EXIST if there was no value for that key.
-int64_t ht_remove (hash_table_t *ht, const char *key_val, uint32_t key_len) {
+uint64_t ht_remove (hash_table_t *ht, const char *key_val, uint32_t key_len) {
     hash_table_i_t *hti = *ht;
-    int64_t val;
+    uint64_t val;
     uint32_t key_hash = murmur32(key_val, key_len);
     do {
         val = hti_compare_and_set(hti, key_hash, key_val, key_len, HT_EXPECT_WHATEVER, TOMBSTONE);
@@ -506,6 +524,12 @@ hash_table_t *ht_alloc (void) {
 void ht_free (hash_table_t *ht) {
     hash_table_i_t *hti = *ht;
     do {
+        for (uint32_t i = 0; i < (1 << hti->scale); ++i) {
+            assert(hti->table[i].value == COPIED_VALUE || !IS_TAGGED(hti->table[i].value));
+            if (hti->table[i].key != DOES_NOT_EXIST) {
+                nbd_free(GET_PTR(hti->table[i].key));
+            }
+        }
         hash_table_i_t *next = hti->next;
         nbd_free(hti);
         hti = next;