+static void ht_resize_grow(struct rcu_ht *ht)
+{
+ unsigned long i, new_size, old_size;
+ struct rcu_table *new_t, *old_t;
+ struct rcu_ht_node *node, *new_node, *tmp;
+ unsigned long hash;
+
+ old_t = ht->t;
+ old_size = old_t->size;
+
+ if (old_size == MAX_HT_BUCKETS)
+ return;
+
+ new_size = old_size << 1;
+ new_t = calloc(1, sizeof(struct rcu_table)
+ + (new_size * sizeof(struct rcu_ht_node *)));
+ new_t->size = new_size;
+
+ for (i = 0; i < old_size; i++) {
+ /*
+ * Re-hash each entry, insert in new table.
+ * It's important that a reader looking for a key _will_ find it
+ * if it's in the table.
+ * Copy each node. (just the node, not ->data)
+ */
+ node = old_t->tbl[i];
+ while (node) {
+ hash = ht->hash_fct(node->key, ht->keylen, ht->hashseed)
+ % new_size;
+ new_node = malloc(sizeof(struct rcu_ht_node));
+ new_node->key = node->key;
+ new_node->data = node->data;
+ new_node->flags = node->flags;
+ new_node->next = new_t->tbl[hash]; /* link to first */
+ new_t->tbl[hash] = new_node; /* add to head */
+ node = node->next;
+ }
+ }
+
+ /* Changing table and size atomically wrt lookups */
+ rcu_assign_pointer(ht->t, new_t);
+
+ /* Ensure all concurrent lookups use new size and table */
+ synchronize_rcu();
+
+ for (i = 0; i < old_size; i++) {
+ node = old_t->tbl[i];
+ while (node) {
+ tmp = node->next;
+ free(node);
+ node = tmp;
+ }
+ }
+ free(old_t);
+}
+
+static void ht_resize_shrink(struct rcu_ht *ht)
+{
+ unsigned long i, new_size;
+ struct rcu_table *new_t, *old_t;
+ struct rcu_ht_node **prev, *node;
+
+ old_t = ht->t;
+ if (old_t->size == 1)
+ return;
+
+ new_size = old_t->size >> 1;
+
+ for (i = 0; i < new_size; i++) {
+ /* Link end with first entry of i + new_size */
+ prev = &old_t->tbl[i];
+ node = *prev;
+ while (node) {
+ prev = &node->next;
+ node = *prev;
+ }
+ *prev = old_t->tbl[i + new_size];
+ }
+ smp_wmb(); /* write links before changing size */
+ STORE_SHARED(old_t->size, new_size);
+
+ /* Ensure all concurrent lookups use new size */
+ synchronize_rcu();
+
+ new_t = realloc(old_t, sizeof(struct rcu_table)
+ + (new_size * sizeof(struct rcu_ht_node *)));
+ /* shrinking, pointers should not move */
+ assert(new_t == old_t);
+}
+
+/*
+ * growth: >0: *2, <0: /2
+ */
+void ht_resize(struct rcu_ht *ht, int growth)
+{
+ int ret;
+
+ ret = pthread_mutex_lock(&ht->resize_mutex);
+ assert(!ret);
+ STORE_SHARED(ht->resize_ongoing, 1);
+ synchronize_rcu();
+ /* All add/remove are waiting on the mutex. */
+ if (growth > 0)
+ ht_resize_grow(ht);
+ else if (growth < 0)
+ ht_resize_shrink(ht);
+ smp_mb();
+ STORE_SHARED(ht->resize_ongoing, 0);
+ ret = pthread_mutex_unlock(&ht->resize_mutex);
+ assert(!ret);
+}
+