+
+ht_iter_t *ht_iter_begin (hashtable_t *ht, map_key_t key) {
+ assert(key == NULL);
+ hti_t *hti = ht->hti;
+ int rcount;
+ do {
+ while (((volatile hti_t *)hti)->next != NULL) {
+ do { } while (hti_help_copy(hti) != TRUE);
+ hti = hti->next;
+ }
+
+ int old = hti->references;
+ do {
+ rcount = old;
+ if (rcount != -1) {
+ old = SYNC_CAS(&hti->references, rcount, rcount + 1);
+ }
+ } while (rcount != old);
+ } while (rcount == -1);
+
+ ht_iter_t *iter = nbd_malloc(sizeof(ht_iter_t));
+ iter->hti = hti;
+ iter->idx = -1;
+
+ return iter;
+}
+
+map_val_t ht_iter_next (ht_iter_t *iter, map_key_t *key_ptr) {
+ volatile entry_t *ent;
+ map_key_t key;
+ map_val_t val;
+ uint64_t table_size = (1 << iter->hti->scale);
+ do {
+ iter->idx++;
+ if (iter->idx == table_size) {
+ return DOES_NOT_EXIST;
+ }
+ ent = &iter->hti->table[iter->idx];
+ key = (iter->hti->ht->key_type == NULL) ? (map_key_t)ent->key : GET_PTR(ent->key);
+ val = ent->val;
+
+ } while (key == DOES_NOT_EXIST || val == DOES_NOT_EXIST || val == TOMBSTONE);
+
+ if (key_ptr) {
+ *key_ptr = key;
+ }
+ if (val == COPIED_VALUE) {
+ uint32_t hash = (iter->hti->ht->key_type == NULL)
+ ? murmur32_8b((uint64_t)key)
+ : iter->hti->ht->key_type->hash(key);
+ val = hti_get(iter->hti->next, (map_key_t)ent->key, hash);
+ }
+
+ return val;
+}
+
+void ht_iter_free (ht_iter_t *iter) {
+ SYNC_ADD(&iter->hti->references, -1);
+ nbd_free(iter);
+}