[Core, mod_hash] Add two APIs switch_core_hash_insert_alloc_destructor and switch_core_hash_insert_dup_destructor. Code cleanup in mod_hash. Add unit-tests.

This commit is contained in:
Andrey Volk committed 2021-10-19 20:26:50 +03:00
1 parent f92368d2f2
commit 1b048a8a97
4 files changed
+113 -16

No files matched your search

+5 -16
View File
@@ -144,10 +144,7 @@ SWITCH_LIMIT_INCR(limit_incr_hash)
if (!(item = (limit_hash_item_t *) switch_core_hash_find(globals.limit_hash, hashkey))) {
/* No, create an empty structure and add it, then continue like as if it existed */
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10, "Creating new limit structure: key: %s\n", hashkey);
item = (limit_hash_item_t *) malloc(sizeof(limit_hash_item_t));
switch_assert(item);
memset(item, 0, sizeof(limit_hash_item_t));
switch_core_hash_insert(globals.limit_hash, hashkey, item);
item = (limit_hash_item_t *)switch_core_hash_insert_alloc(globals.limit_hash, hashkey, sizeof(limit_hash_item_t));
}
if (!(pvt = switch_channel_get_private(channel, "limit_hash"))) {
@@ -433,17 +430,13 @@ SWITCH_STANDARD_APP(hash_function)
free(value);
switch_core_hash_delete(globals.db_hash, hash_key);
}
value = strdup(argv[3]);
switch_assert(value);
switch_core_hash_insert(globals.db_hash, hash_key, value);
switch_core_hash_insert_dup(globals.db_hash, hash_key, argv[3]);
} else if (!strcasecmp(argv[0], "insert_ifempty")) {
if (argc < 4) {
goto usage;
}
if (!(value = switch_core_hash_find(globals.db_hash, hash_key))) {
value = strdup(argv[3]);
switch_assert(value);
switch_core_hash_insert(globals.db_hash, hash_key, value);
switch_core_hash_insert_dup(globals.db_hash, hash_key, argv[3]);
}
} else if (!strcasecmp(argv[0], "delete")) {
@@ -506,9 +499,7 @@ SWITCH_STANDARD_API(hash_api_function)
switch_safe_free(value);
switch_core_hash_delete(globals.db_hash, hash_key);
}
value = strdup(argv[3]);
switch_assert(value);
switch_core_hash_insert(globals.db_hash, hash_key, value);
switch_core_hash_insert_dup(globals.db_hash, hash_key, argv[3]);
stream->write_function(stream, "+OK\n");
switch_thread_rwlock_unlock(globals.db_hash_rwlock);
} else if (!strcasecmp(argv[0], "insert_ifempty")) {
@@ -519,9 +510,7 @@ SWITCH_STANDARD_API(hash_api_function)
if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
stream->write_function(stream, "-ERR key already exists\n");
} else {
value = strdup(argv[3]);
switch_assert(value);
switch_core_hash_insert(globals.db_hash, hash_key, value);
switch_core_hash_insert_dup(globals.db_hash, hash_key, argv[3]);
stream->write_function(stream, "+OK\n");
}
switch_thread_rwlock_unlock(globals.db_hash_rwlock);