[Core] Add new switch_core_hash_insert_pointer(switch_hash_t *hash, const void *data) API to be able to insert data into a hash with an auto-generated key based on the data pointer. Cleanup switch_loadable_module_process() to not generate hash keys for stored events itself. Add unit-test.

This commit is contained in:
Andrey Volk committed 2021-10-19 20:35:12 +03:00
1 parent e14df00aa6
commit bd7b76bacf
4 files changed
+74 -65

No files matched your search

+17
View File
@@ -55,6 +55,23 @@ SWITCH_DECLARE(switch_status_t) switch_core_hash_destroy(switch_hash_t **hash)
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_pointer(switch_hash_t *hash, const void *data)
{
size_t bytes_required = snprintf(NULL, 0, "%p", data) + 1;
char *dkey = malloc(bytes_required);
size_t bytes_written = snprintf(dkey, bytes_required, "%p", data);
if (bytes_written > 0 && bytes_written < bytes_required) {
if (switch_hashtable_insert_destructor(hash, dkey, (void *)data, HASHTABLE_FLAG_FREE_KEY | HASHTABLE_DUP_CHECK, NULL)) {
return SWITCH_STATUS_SUCCESS;
}
}
switch_safe_free(dkey);
return SWITCH_STATUS_FALSE;
}
SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_auto_free(switch_hash_t *hash, const char *key, const void *data)
{
char *dkey = strdup(key);