util/hash_table: add DERIVE macro

we typically use a hash table with a fixed struct key, but this requires tedious
boilerplate. add a macro that generates all the boilerplate for you so you can
just create a table and go.

naming inspired by Rust #![derive].

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/28056>
This commit is contained in:
Alyssa Rosenzweig 2024-03-07 16:27:50 -04:00 committed by Marge Bot
parent 49a89911c4
commit 93879b1920

View file

@ -162,6 +162,26 @@ hash_table_call_foreach(struct hash_table *ht,
callback(entry->key, entry->data, closure);
}
/**
* This helper macro generates the boilerplate required to use a hash table with
* a fixed-size struct as the key.
*/
#define DERIVE_HASH_TABLE(T) \
static uint32_t T##_hash(const void *key) \
{ \
return _mesa_hash_data(key, sizeof(struct T)); \
} \
\
static bool T##_equal(const void *a, const void *b) \
{ \
return memcmp(a, b, sizeof(struct T)) == 0; \
} \
\
static struct hash_table *T##_table_create(void *memctx) \
{ \
return _mesa_hash_table_create(memctx, T##_hash, T##_equal); \
}
/**
* Hash table wrapper which supports 64-bit keys.
*/