From 60a3637b20570cf555cfb0322686bd28b87ba05f Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Thu, 31 Jul 2025 13:47:02 -0400 Subject: [PATCH] MT#63317 type safety for free functions Change-Id: I31cf09d7bd8d00626f95dc84a3cd9e0c48026d49 --- daemon/janus.c | 3 ++- daemon/media_player.c | 6 +++--- daemon/sdp.c | 2 +- lib/containers.h | 12 ++++++++++-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/daemon/janus.c b/daemon/janus.c index 980c8a467..9fe4363fb 100644 --- a/daemon/janus.c +++ b/daemon/janus.c @@ -28,7 +28,8 @@ struct janus_session { // "login" session janus_handles_set handles; // handle ID -> 0x1. handle ID owned by janus_handles }; -TYPED_GHASHTABLE(janus_sessions_ht, uint64_t, struct janus_session, int64_hash, int64_eq, NULL, __obj_put) +TYPED_GHASHTABLE(janus_sessions_ht, uint64_t, struct janus_session, int64_hash, int64_eq, NULL, + (void (*)(struct janus_session *)) __obj_put) struct janus_handle { // corresponds to a conference participant diff --git a/daemon/media_player.c b/daemon/media_player.c index 21bbc72a1..3f90f548b 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -109,12 +109,12 @@ static gboolean media_player_cache_entry_eq(const struct media_player_cache_inde static void __media_player_cache_entry_free(struct media_player_cache_entry *p); TYPED_GHASHTABLE(media_player_cache_ht, struct media_player_cache_index, struct media_player_cache_entry, media_player_cache_entry_hash, media_player_cache_entry_eq, - NULL, __obj_put) + NULL, (void(*)(struct media_player_cache_entry *)) __obj_put) TYPED_GQUEUE(media_player_cache_entry, struct media_player_cache_entry) static media_player_cache_ht media_player_cache; // keys and values only ever freed at shutdown TYPED_GHASHTABLE(media_player_media_files_ht, str, struct media_player_media_file, str_hash, str_equal, - NULL, __obj_put); + NULL, (void(*)(struct media_player_media_file *)) __obj_put); static mutex_t media_player_media_files_lock = MUTEX_STATIC_INIT; static media_player_media_files_ht media_player_media_files; static rwlock_t media_player_media_files_names_lock = RWLOCK_STATIC_INIT; @@ -122,7 +122,7 @@ static str_q media_player_media_files_names = TYPED_GQUEUE_INIT; // lock order: media_player_media_files_names_lock first, media_player_media_files_lock second TYPED_GHASHTABLE(media_player_db_media_ht, void, struct media_player_media_file, g_direct_hash, g_direct_equal, - NULL, __obj_put); + NULL, (void(*)(struct media_player_media_file *)) __obj_put); static mutex_t media_player_db_media_lock = MUTEX_STATIC_INIT; static media_player_db_media_ht media_player_db_media; static rwlock_t media_player_db_media_ids_lock = RWLOCK_STATIC_INIT; diff --git a/daemon/sdp.c b/daemon/sdp.c index b26449853..f8f18a484 100644 --- a/daemon/sdp.c +++ b/daemon/sdp.c @@ -80,7 +80,7 @@ struct sdp_connection { TYPED_GQUEUE(attributes, struct sdp_attribute) TYPED_GHASHTABLE(attr_id_ht, void, struct sdp_attribute, g_direct_hash, g_direct_equal, NULL, NULL) -TYPED_GHASHTABLE(attr_list_ht, void, attributes_q, g_direct_hash, g_direct_equal, NULL, g_queue_free) +TYPED_GHASHTABLE(attr_list_ht, void, attributes_q, g_direct_hash, g_direct_equal, NULL, attributes_q_free) TYPED_GHASHTABLE_LOOKUP_INSERT(attr_list_ht, NULL, attributes_q_new) struct sdp_attributes { diff --git a/lib/containers.h b/lib/containers.h index 19f3d83e8..711e1c8af 100644 --- a/lib/containers.h +++ b/lib/containers.h @@ -125,9 +125,17 @@ static inline void g_queue_clear_full(GQueue *q, GDestroyNotify free_func) { __typeof__(((type_name *)0)->__ckey))) (eq_func), \ default: (eq_func) \ ); \ + void (*__k_free_func)(__typeof__(((type_name *)0)->__key)) = _Generic((key_free_func), \ + void (*)(void *): (void (*)(__typeof__(((type_name *)0)->__key))) (key_free_func), \ + default: (key_free_func) \ + ); \ + void (*__v_free_func)(__typeof__(((type_name *)0)->__value)) = _Generic((value_free_func), \ + void (*)(void *): (void (*)(__typeof__(((type_name *)0)->__value))) (value_free_func), \ + default: (value_free_func) \ + ); \ GHashTable *ht = g_hash_table_new_full((GHashFunc) __hash_func, (GEqualFunc) __eq_func, \ - (GDestroyNotify) key_free_func, \ - (GDestroyNotify) value_free_func); \ + (GDestroyNotify) __k_free_func, \ + (GDestroyNotify) __v_free_func); \ return (type_name) { ht }; \ } \