+ assert(!is_removed(iter));
+ if (is_dummy(iter))
+ new_next = flag_dummy(clear_flag(next));
+ else
+ new_next = clear_flag(next);
+ (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
+ }
+}
+
+static
+struct rcu_ht_node *_ht_add(struct rcu_ht *ht, struct rcu_table *t,
+ struct rcu_ht_node *node, int unique, int dummy)
+{
+ struct rcu_ht_node *iter_prev, *iter, *next, *new_node, *new_next,
+ *dummy_node;
+ struct _rcu_ht_node *lookup;
+ unsigned long hash, index, order;
+
+ if (!t->size) {
+ assert(dummy);
+ node->p.next = flag_dummy(NULL);
+ return node; /* Initial first add (head) */
+ }
+ hash = bit_reverse_ulong(node->p.reverse_hash);
+ for (;;) {
+ uint32_t chain_len = 0;
+
+ /*
+ * iter_prev points to the non-removed node prior to the
+ * insert location.
+ */
+ index = hash & (t->size - 1);
+ order = get_count_order_ulong(index + 1);
+ lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
+ iter_prev = (struct rcu_ht_node *) lookup;
+ /* We can always skip the dummy node initially */
+ iter = rcu_dereference(iter_prev->p.next);
+ assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
+ for (;;) {
+ if (unlikely(!clear_flag(iter)))
+ goto insert;
+ if (clear_flag(iter)->p.reverse_hash > node->p.reverse_hash)
+ goto insert;
+ next = rcu_dereference(clear_flag(iter)->p.next);
+ if (is_removed(next))
+ goto gc_node;
+ if (unique
+ && !is_dummy(next)
+ && !ht->compare_fct(node->key, node->key_len,
+ clear_flag(iter)->key,
+ clear_flag(iter)->key_len))
+ return clear_flag(iter);
+ /* Only account for identical reverse hash once */
+ if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash
+ && !is_dummy(next))
+ check_resize(ht, t, ++chain_len);
+ iter_prev = clear_flag(iter);
+ iter = next;
+ }
+ insert:
+ assert(node != clear_flag(iter));
+ assert(!is_removed(iter_prev));