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)