From: Connor Abbott Date: Tue, 21 May 2019 10:36:56 +0000 (+0200) Subject: util/hash_table: Add specialized resizing add function X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=983b001c77c6c9e9aa436c3252fafe34edfe6738;p=mesa.git util/hash_table: Add specialized resizing add function To keep it in sync with the set implementation. Reviewed-by: Eric Anholt Acked-by: Jason Ekstrand --- diff --git a/src/util/hash_table.c b/src/util/hash_table.c index 65b685a0828..3882f2c73f9 100644 --- a/src/util/hash_table.c +++ b/src/util/hash_table.c @@ -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); }