util/hash_table: Add specialized resizing add function
authorConnor Abbott <cwabbott0@gmail.com>
Tue, 21 May 2019 10:36:56 +0000 (12:36 +0200)
committerConnor Abbott <cwabbott0@gmail.com>
Fri, 31 May 2019 17:14:22 +0000 (19:14 +0200)
To keep it in sync with the set implementation.

Reviewed-by: Eric Anholt <eric@anholt.net>
Acked-by: Jason Ekstrand <jason@jlekstrand.net>
src/util/hash_table.c

index 65b685a0828e928a677c75ac1ec24ce90f224c4a..3882f2c73f92da4a7d4032fd443bc065a8395e3c 100644 (file)
@@ -291,6 +291,30 @@ static struct hash_entry *
 hash_table_insert(struct hash_table *ht, uint32_t hash,
                   const void *key, void *data);
 
+static void
+hash_table_insert_rehash(struct hash_table *ht, uint32_t hash,
+                         const void *key, void *data)
+{
+   uint32_t size = ht->size;
+   uint32_t start_hash_address = hash % size;
+   uint32_t hash_address = start_hash_address;
+   uint32_t double_hash = 1 + hash % ht->rehash;
+   do {
+      struct hash_entry *entry = ht->table + hash_address;
+
+      if (likely(entry->key == NULL)) {
+         entry->hash = hash;
+         entry->key = key;
+         entry->data = data;
+         return;
+      }
+
+      hash_address += double_hash;
+      if (hash_address >= size)
+         hash_address -= size;
+   } while (true);
+}
+
 static void
 _mesa_hash_table_rehash(struct hash_table *ht, unsigned new_size_index)
 {
@@ -316,9 +340,11 @@ _mesa_hash_table_rehash(struct hash_table *ht, unsigned new_size_index)
    ht->deleted_entries = 0;
 
    hash_table_foreach(&old_ht, entry) {
-      hash_table_insert(ht, entry->hash, entry->key, entry->data);
+      hash_table_insert_rehash(ht, entry->hash, entry->key, entry->data);
    }
 
+   ht->entries = old_ht.entries;
+
    ralloc_free(old_ht.table);
 }