]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[Core] Add new switch_core_hash_insert_dup_auto_free() API
authorAndrey Volk <andywolk@gmail.com>
Fri, 2 Apr 2021 13:26:08 +0000 (16:26 +0300)
committerAndrey Volk <andywolk@gmail.com>
Tue, 19 Oct 2021 17:39:36 +0000 (20:39 +0300)
src/include/switch_core.h
src/mod/endpoints/mod_sofia/sofia.c
src/switch_core_hash.c

index b6b3efbd905f14476437a0f9bc2b7b71ecbc26b7..a0c91ec6fd55facf79917d157269b3431ae46e6e 100644 (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);
 
+/*!
+  \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
 */
index e5ba5ce0466aeec8025fb7b30a07befbc3d64f31..fec5997f08f89698f7028151b0270ae0ad2e4a1a 100644 (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);
 
                                                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 {
index 1f13a44a6f3369cfb13ce53cbf7d6e806c165223..3009ec48240a11ebf7e3d4ec1ee238398666ba52 100644 (file)
@@ -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;