[Core] Add new switch_core_hash_insert_dup_auto_free() API

This commit is contained in:
Andrey Volk 2021-04-02 16:26:08 +03:00
parent 0b0f5fff65
commit 58abf91d1d
3 changed files with 32 additions and 10 deletions

View File

@ -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); 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 \brief Insert data into a hash
\param hash the hash to add data to \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) #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 \brief Insert strdup(str) into a hash
\param hash the hash to add data to \param hash the hash to add str to
\param key the name of the key to add the data to \param key the name of the key to add the str to
\param data string to strdup and add \param str string to strdup and add
\return SWITCH_STATUS_SUCCESS if the data is added \return SWITCH_STATUS_SUCCESS if the data is added
\note the string key must be a constant or a dynamic string \note the string key must be a constant or a dynamic string
*/ */

View File

@ -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); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "detaching session %s\n", sofia_private->uuid);
if (!zstr(tech_pvt->call_id)) { if (!zstr(tech_pvt->call_id)) {
char *uuid = strdup(switch_core_session_get_uuid(session));
tech_pvt->sofia_private = NULL; tech_pvt->sofia_private = NULL;
tech_pvt->nh = NULL; tech_pvt->nh = NULL;
sofia_set_flag(tech_pvt, TFLAG_BYE); sofia_set_flag(tech_pvt, TFLAG_BYE);
switch_mutex_lock(profile->flag_mutex); switch_mutex_lock(profile->flag_mutex);
switch_core_hash_insert_dup_auto_free(profile->chat_hash, tech_pvt->call_id, switch_core_session_get_uuid(session));
if (switch_core_hash_insert_auto_free(profile->chat_hash, tech_pvt->call_id, uuid) != SWITCH_STATUS_SUCCESS) {
switch_safe_free(uuid);
}
switch_mutex_unlock(profile->flag_mutex); switch_mutex_unlock(profile->flag_mutex);
nua_handle_destroy(nh); nua_handle_destroy(nh);
} else { } else {

View File

@ -85,6 +85,23 @@ SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_auto_free(switch_hash_t
return SWITCH_STATUS_FALSE; 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) { 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; char *dkey;
void *data; void *data;