From 05ae5bb258c503552f57684987ed9f2c6b528ad2 Mon Sep 17 00:00:00 2001 From: Vftdan Date: Fri, 25 Oct 2024 17:44:14 +0200 Subject: [PATCH] Fix macros used by hash table --- src/common/defs.h | 2 ++ src/common/util/hash_table.h | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/common/defs.h b/src/common/defs.h index 6eedee3..4bd0de7 100644 --- a/src/common/defs.h +++ b/src/common/defs.h @@ -11,6 +11,8 @@ // Assuming child type has a field for the base type // So for structs it is usually actual downcast, but for unions it is an upcast #define DOWNCAST(contype, basename, ptr) containerof(ptr, contype, as_##basename) +// Expects ptr to be of type srctype* or void*, returns (dsttype*)ptr +#define EXPECT_AND_CAST(dsttype, srctype, ptr) (((union { typeof(srctype) *src; typeof(dsttype) *dst; }){.src = (ptr)}).dst) #define T_ALLOC(count, T) ((T*)calloc((count), sizeof(T))) #define HEADER_FN __attribute__((unused)) inline static diff --git a/src/common/util/hash_table.h b/src/common/util/hash_table.h index cc8ae50..1472b9d 100644 --- a/src/common/util/hash_table.h +++ b/src/common/util/hash_table.h @@ -61,9 +61,9 @@ hash_table_delete_by_key_impl(HashTableDynamicData * dyndata, const HashTableKey return hash_table_delete_at_index_impl(dyndata, hash_table_find_impl(dyndata, key)); } -#define hash_table_init(ht, value_deinit) hash_table_init_impl(&(ht)->as_HashTableDynamicData, sizeof(*(ht)->value_array), IMPLICIT_CAST(void(void*), void(typeof((ht)->value_array)), value_deinit)) +#define hash_table_init(ht, value_deinit) hash_table_init_impl(&(ht)->as_HashTableDynamicData, sizeof(*(ht)->value_array), EXPECT_AND_CAST(void(void*), void(typeof((ht)->value_array)), value_deinit)) #define hash_table_deinit(ht) hash_table_deinit_impl(&(ht)->as_HashTableDynamicData) -#define hash_table_insert(ht, key, value_ptr) hash_table_insert_impl(&(ht)->as_HashTableDynamicData, key, IMPLICIT_CAST(const void, const typeof(*(ht)->value_array), value_ptr)) +#define hash_table_insert(ht, key, value_ptr) hash_table_insert_impl(&(ht)->as_HashTableDynamicData, key, EXPECT_AND_CAST(const void, const typeof(*(ht)->value_array), value_ptr)) #define hash_table_find(ht, key) hash_table_find_impl(&(ht)->as_HashTableDynamicData, key) #define hash_table_delete_at_index(ht, index) hash_table_delete_at_index_impl(&(ht)->as_HashTableDynamicData, index) #define hash_table_delete_by_key(ht, key) hash_table_delete_by_key_impl(&(ht)->as_HashTableDynamicData, key)