From 58abf91d1d325b706ee5446983025add193e63bf Mon Sep 17 00:00:00 2001 From: Andrey Volk Date: Fri, 2 Apr 2021 16:26:08 +0300 Subject: [PATCH] [Core] Add new switch_core_hash_insert_dup_auto_free() API --- src/include/switch_core.h | 18 ++++++++++++++---- src/mod/endpoints/mod_sofia/sofia.c | 7 +------ src/switch_core_hash.c | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/include/switch_core.h b/src/include/switch_core.h index b6b3efbd90..a0c91ec6fd 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -1446,6 +1446,16 @@ SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_pointer(switch_hash_t *h */ SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_auto_free(switch_hash_t *hash, const char *key, const void *data); +/*! + \brief Insert strdup(str) into a hash and set flags so the value is automatically freed on delete + \param hash the hash to add str to + \param key the name of the key to add the str to + \param str string to strdup and add + \return SWITCH_STATUS_SUCCESS if the data is added + \note the string key must be a constant or a dynamic string +*/ +SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_dup_auto_free(switch_hash_t *hash, const char *key, const char *str); + /*! \brief Insert data into a hash \param hash the hash to add data to @@ -1469,10 +1479,10 @@ SWITCH_DECLARE(void *) switch_core_hash_insert_alloc_destructor(_In_ switch_hash #define switch_core_hash_insert_alloc(_h, _k, _s) switch_core_hash_insert_alloc_destructor(_h, _k, _s, NULL) /*! - \brief Insert strdup(data) into a hash - \param hash the hash to add data to - \param key the name of the key to add the data to - \param data string to strdup and add + \brief Insert strdup(str) into a hash + \param hash the hash to add str to + \param key the name of the key to add the str to + \param str string to strdup and add \return SWITCH_STATUS_SUCCESS if the data is added \note the string key must be a constant or a dynamic string */ diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index e5ba5ce046..fec5997f08 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -2441,16 +2441,11 @@ void sofia_event_callback(nua_event_t event, switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "detaching session %s\n", sofia_private->uuid); if (!zstr(tech_pvt->call_id)) { - char *uuid = strdup(switch_core_session_get_uuid(session)); tech_pvt->sofia_private = NULL; tech_pvt->nh = NULL; sofia_set_flag(tech_pvt, TFLAG_BYE); switch_mutex_lock(profile->flag_mutex); - - if (switch_core_hash_insert_auto_free(profile->chat_hash, tech_pvt->call_id, uuid) != SWITCH_STATUS_SUCCESS) { - switch_safe_free(uuid); - } - + switch_core_hash_insert_dup_auto_free(profile->chat_hash, tech_pvt->call_id, switch_core_session_get_uuid(session)); switch_mutex_unlock(profile->flag_mutex); nua_handle_destroy(nh); } else { diff --git a/src/switch_core_hash.c b/src/switch_core_hash.c index 1f13a44a6f..3009ec4824 100644 --- a/src/switch_core_hash.c +++ b/src/switch_core_hash.c @@ -85,6 +85,23 @@ SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_auto_free(switch_hash_t return SWITCH_STATUS_FALSE; } +SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_dup_auto_free(switch_hash_t *hash, const char *key, const char *str) +{ + char *dkey = strdup(key); + char *dup = strdup(str); + + assert(dup); + + if (switch_hashtable_insert_destructor(hash, dkey, (void *)dup, HASHTABLE_FLAG_FREE_KEY | HASHTABLE_FLAG_FREE_VALUE | HASHTABLE_DUP_CHECK, NULL)) { + return SWITCH_STATUS_SUCCESS; + } + + switch_safe_free(dup); + switch_safe_free(dkey); + + return SWITCH_STATUS_FALSE; +} + SWITCH_DECLARE(void *) switch_core_hash_insert_alloc_destructor(_In_ switch_hash_t *hash, _In_z_ const char *key, _In_opt_ size_t size, hashtable_destructor_t destructor) { char *dkey; void *data;