util/hash_table: Add specialized resizing add function

To keep it in sync with the set implementation.

Reviewed-by: Eric Anholt <eric@anholt.net>
Acked-by: Jason Ekstrand <jason@jlekstrand.net>
This commit is contained in:
Connor Abbott 2019-05-21 12:36:56 +02:00
parent 6f9beb28bb
commit 983b001c77

View 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);
}