hashmap: add disallow_rehash setting
Teach hashmap to allow rehashes to be suppressed. This is useful when hashmaps are accessed by multiple threads. It still requires the caller to properly manage their locking. This just prevents unexpected rehashing during inserts and deletes. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
f75619bd6d
commit
0607e10009
12
hashmap.c
12
hashmap.c
@ -104,11 +104,19 @@ static inline unsigned int bucket(const struct hashmap *map,
|
||||
return key->hash & (map->tablesize - 1);
|
||||
}
|
||||
|
||||
int hashmap_bucket(const struct hashmap *map, unsigned int hash)
|
||||
{
|
||||
return hash & (map->tablesize - 1);
|
||||
}
|
||||
|
||||
static void rehash(struct hashmap *map, unsigned int newsize)
|
||||
{
|
||||
unsigned int i, oldsize = map->tablesize;
|
||||
struct hashmap_entry **oldtable = map->table;
|
||||
|
||||
if (map->disallow_rehash)
|
||||
return;
|
||||
|
||||
alloc_table(map, newsize);
|
||||
for (i = 0; i < oldsize; i++) {
|
||||
struct hashmap_entry *e = oldtable[i];
|
||||
@ -141,7 +149,9 @@ void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
|
||||
size_t initial_size)
|
||||
{
|
||||
unsigned int size = HASHMAP_INITIAL_SIZE;
|
||||
map->size = 0;
|
||||
|
||||
memset(map, 0, sizeof(*map));
|
||||
|
||||
map->cmpfn = equals_function ? equals_function : always_equal;
|
||||
|
||||
/* calculate initial table size and allocate the table */
|
||||
|
||||
Reference in New Issue
Block a user