util/hash_table: don't allocate hash_table_u64::table, declare it statically

We can use _mesa_hash_table_init instead of _mesa_hash_table_create.
It doesn't have to be allocated.

Reviewed-by: Gert Wollny <gert.wollny@collabora.com>
Acked-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36728>
This commit is contained in:
Marek Olšák 2025-08-11 01:01:59 -04:00 committed by Marge Bot
parent 9724f7eeae
commit 271a1d8dd9
8 changed files with 37 additions and 43 deletions

View file

@ -1186,13 +1186,13 @@ radv_free_shader_memory(struct radv_device *device, union radv_shader_arena_bloc
if (device->capture_replay_arena_vas) {
struct hash_entry *arena_entry = NULL;
hash_table_foreach (device->capture_replay_arena_vas->table, entry) {
hash_table_foreach (&device->capture_replay_arena_vas->table, entry) {
if (entry->data == arena) {
arena_entry = entry;
break;
}
}
_mesa_hash_table_remove(device->capture_replay_arena_vas->table, arena_entry);
_mesa_hash_table_remove(&device->capture_replay_arena_vas->table, arena_entry);
}
free(arena);

View file

@ -205,7 +205,7 @@ d3d12_context_state_table_init(struct d3d12_context *ctx)
void
d3d12_context_state_table_destroy(struct d3d12_context *ctx)
{
hash_table_foreach(ctx->bo_state_table->table, entry) {
hash_table_foreach(&ctx->bo_state_table->table, entry) {
d3d12_destroy_context_state_table_entry((d3d12_context_state_table_entry *)entry->data);
free(entry->data);
}

View file

@ -412,7 +412,7 @@ void si_destroy_sqtt(struct si_context *sctx)
ac_sqtt_finish(sctx->sqtt);
hash_table_foreach (sctx->sqtt->pipeline_bos->table, entry) {
hash_table_foreach (&sctx->sqtt->pipeline_bos->table, entry) {
struct si_sqtt_fake_pipeline *pipeline =
(struct si_sqtt_fake_pipeline *)entry->data;
si_resource_reference(&pipeline->bo, NULL);

View file

@ -763,7 +763,7 @@ dzn_meta_blits_finish(struct dzn_device *device)
}
if (meta->contexts) {
hash_table_foreach(meta->contexts->table, he)
hash_table_foreach(&meta->contexts->table, he)
dzn_meta_blit_destroy(device, he->data);
_mesa_hash_table_u64_destroy(meta->contexts);
}

View file

@ -882,11 +882,10 @@ _mesa_hash_table_u64_create(void *mem_ctx)
return NULL;
if (sizeof(void *) == 8) {
ht->table = _mesa_hash_table_create(ht, _mesa_hash_pointer,
_mesa_key_pointer_equal);
_mesa_hash_table_init(&ht->table, ht, _mesa_hash_pointer,
_mesa_key_pointer_equal);
} else {
ht->table = _mesa_hash_table_create(ht, key_u64_hash,
key_u64_equals);
_mesa_hash_table_init(&ht->table, ht, key_u64_hash, key_u64_equals);
/* Allocate a ralloc sub-context which takes the u64 hash table
* as a parent and attach a destructor to it so we can free the
@ -894,28 +893,25 @@ _mesa_hash_table_u64_create(void *mem_ctx)
* _mesa_hash_table_u64_insert().
*
* The order of creation of this sub-context is crucial: it needs
* to happen after the _mesa_hash_table_create() call to guarantee
* to happen after the _mesa_hash_table_init() call to guarantee
* that the destructor is called before ht->table and its children
* are freed, otherwise the _mesa_hash_table_u64_clear() call in the
* destructor leads to a use-after-free situation.
*/
if (ht->table) {
void *dummy_ctx = ralloc_context(ht);
void *dummy_ctx = ralloc_context(ht);
/* If we can't allocate a sub-context, free the hash table
* immediately and return NULL to avoid future leaks.
*/
if (!dummy_ctx) {
ralloc_free(ht);
return NULL;
}
ralloc_set_destructor(dummy_ctx, _mesa_hash_table_u64_delete_keys);
/* If we can't allocate a sub-context, free the hash table
* immediately and return NULL to avoid future leaks.
*/
if (!dummy_ctx) {
ralloc_free(ht);
return NULL;
}
ralloc_set_destructor(dummy_ctx, _mesa_hash_table_u64_delete_keys);
}
if (ht->table)
_mesa_hash_table_set_deleted_key(ht->table, uint_key(DELETED_KEY_VALUE));
_mesa_hash_table_set_deleted_key(&ht->table, uint_key(DELETED_KEY_VALUE));
return ht;
}
@ -938,7 +934,7 @@ _mesa_hash_table_u64_clear(struct hash_table_u64 *ht)
if (!ht)
return;
_mesa_hash_table_clear(ht->table, _mesa_hash_table_u64_delete_key);
_mesa_hash_table_clear(&ht->table, _mesa_hash_table_u64_delete_key);
ht->freed_key_data = NULL;
ht->deleted_key_data = NULL;
}
@ -946,8 +942,6 @@ _mesa_hash_table_u64_clear(struct hash_table_u64 *ht)
void
_mesa_hash_table_u64_destroy(struct hash_table_u64 *ht)
{
if (!ht)
return;
ralloc_free(ht);
}
@ -966,7 +960,7 @@ _mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key,
}
if (sizeof(void *) == 8) {
_mesa_hash_table_insert(ht->table, (void *)(uintptr_t)key, data);
_mesa_hash_table_insert(&ht->table, (void *)(uintptr_t)key, data);
} else {
struct hash_key_u64 *_key = CALLOC_STRUCT(hash_key_u64);
@ -975,7 +969,7 @@ _mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key,
_key->value = key;
struct hash_entry *entry =
hash_table_get_entry(ht->table, key_u64_hash(_key), _key);
hash_table_get_entry(&ht->table, key_u64_hash(_key), _key);
if (!entry) {
FREE(_key);
@ -983,7 +977,7 @@ _mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key,
}
entry->data = data;
if (!entry_is_present(ht->table, entry))
if (!entry_is_present(&ht->table, entry))
entry->key = _key;
else
FREE(_key);
@ -994,10 +988,10 @@ static struct hash_entry *
hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key)
{
if (sizeof(void *) == 8) {
return _mesa_hash_table_search(ht->table, (void *)(uintptr_t)key);
return _mesa_hash_table_search(&ht->table, (void *)(uintptr_t)key);
} else {
struct hash_key_u64 _key = { .value = key };
return _mesa_hash_table_search(ht->table, &_key);
return _mesa_hash_table_search(&ht->table, &_key);
}
}
@ -1039,11 +1033,11 @@ _mesa_hash_table_u64_remove(struct hash_table_u64 *ht, uint64_t key)
return;
if (sizeof(void *) == 8) {
_mesa_hash_table_remove(ht->table, entry);
_mesa_hash_table_remove(&ht->table, entry);
} else {
struct hash_key *_key = (struct hash_key *)entry->key;
_mesa_hash_table_remove(ht->table, entry);
_mesa_hash_table_remove(&ht->table, entry);
FREE(_key);
}
}
@ -1074,7 +1068,7 @@ _mesa_hash_table_u64_next_entry(struct hash_table_u64 *ht,
/* All other entries: regular */
struct hash_entry *next =
_mesa_hash_table_next_entry(ht->table, ent ? ent->_entry : NULL);
_mesa_hash_table_next_entry(&ht->table, ent ? ent->_entry : NULL);
if (!next)
return (struct hash_entry_u64){.data = NULL};

View file

@ -219,7 +219,7 @@ hash_table_call_foreach(struct hash_table *ht,
* Hash table wrapper which supports 64-bit keys.
*/
struct hash_table_u64 {
struct hash_table *table;
struct hash_table table;
void *freed_key_data;
void *deleted_key_data;
};
@ -262,7 +262,7 @@ static inline uint32_t
_mesa_hash_table_u64_num_entries(struct hash_table_u64 *ht)
{
return (!!ht->freed_key_data) + (!!ht->deleted_key_data) +
_mesa_hash_table_num_entries(ht->table);
_mesa_hash_table_num_entries(&ht->table);
}
/**

View file

@ -296,9 +296,9 @@ mesa_db_update_index(struct mesa_cache_db *db)
if (!mesa_db_seek(db->index.file, db->index.offset))
return false;
old_entries = _mesa_hash_table_num_entries(db->index_db->table);
old_entries = _mesa_hash_table_num_entries(&db->index_db->table);
new_entries = (file_length - db->index.offset) / sizeof(*index_entries);
_mesa_hash_table_reserve(db->index_db->table, old_entries + new_entries);
_mesa_hash_table_reserve(&db->index_db->table, old_entries + new_entries);
new_index_size = new_entries * sizeof(*index_entries);
index_entries = malloc(new_index_size);
@ -542,7 +542,7 @@ mesa_db_compact(struct mesa_cache_db *db, int64_t blob_size,
if (!remove_entry && !mesa_db_reload(db))
return false;
num_entries = _mesa_hash_table_num_entries(db->index_db->table);
num_entries = _mesa_hash_table_num_entries(&db->index_db->table);
if (!num_entries)
return true;
@ -563,7 +563,7 @@ mesa_db_compact(struct mesa_cache_db *db, int64_t blob_size,
index_header.uuid != db->uuid)
goto cleanup;
hash_table_foreach(db->index_db->table, entry) {
hash_table_foreach(&db->index_db->table, entry) {
entries[i] = entry->data;
entries[i]->evicted = (entries[i] == remove_entry);
buffer_size = MAX2(buffer_size, blob_file_size(entries[i]->size));
@ -1037,12 +1037,12 @@ mesa_cache_db_eviction_score(struct mesa_cache_db *db)
if (!mesa_db_reload(db))
goto fail_fatal;
num_entries = _mesa_hash_table_num_entries(db->index_db->table);
num_entries = _mesa_hash_table_num_entries(&db->index_db->table);
entries = calloc(num_entries, sizeof(*entries));
if (!entries)
goto fail;
hash_table_foreach(db->index_db->table, entry)
hash_table_foreach(&db->index_db->table, entry)
entries[i++] = entry->data;
util_qsort_r(entries, num_entries, sizeof(*entries),

View file

@ -59,7 +59,7 @@ vk_memory_trace_finish(struct vk_device *device)
}
}
util_dynarray_fini(&device->memory_trace_data.tokens);
if (_mesa_hash_table_num_entries(device->memory_trace_data.handle_table->table))
if (_mesa_hash_table_num_entries(&device->memory_trace_data.handle_table->table))
fprintf(stderr,
"mesa: Unfreed resources detected at device destroy, there may be memory leaks!\n");
_mesa_hash_table_u64_destroy(device->memory_trace_data.handle_table);