2007-03-29 22:34:40 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
2014-02-05 21:02:28 +00:00
|
|
|
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
|
2007-03-29 22:34:40 +00:00
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2007-03-29 22:34:40 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2007-03-29 22:34:40 +00:00
|
|
|
* Michael Jerris <mike@jerris.com>
|
|
|
|
* Paul D. Tinsley <pdt at jackhammer.org>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* switch_core_hash.c -- Main Core Library (hash functions)
|
|
|
|
*
|
|
|
|
*/
|
2008-01-27 17:36:53 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
#include <switch.h>
|
2007-05-14 17:10:46 +00:00
|
|
|
#include "private/switch_core_pvt.h"
|
2014-03-08 19:37:09 +00:00
|
|
|
#include "private/switch_hashtable_private.h"
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_init_case(switch_hash_t **hash, switch_bool_t case_sensitive)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2014-03-08 19:37:09 +00:00
|
|
|
return switch_create_hashtable(hash, 16, case_sensitive ? switch_hash_default : switch_hash_default_ci, switch_hash_equalkeys);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
|
2007-09-29 01:06:08 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_destroy(switch_hash_t **hash)
|
2007-05-03 16:28:23 +00:00
|
|
|
{
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(hash != NULL && *hash != NULL);
|
2009-10-03 22:21:52 +00:00
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
switch_hashtable_destroy(hash);
|
2009-10-03 22:21:52 +00:00
|
|
|
|
2007-05-03 16:28:23 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_insert(switch_hash_t *hash, const char *key, const void *data)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2014-03-08 19:37:09 +00:00
|
|
|
switch_hashtable_insert(hash, strdup(key), (void *)data, HASHTABLE_FLAG_FREE_KEY);
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_locked(switch_hash_t *hash, const char *key, const void *data, switch_mutex_t *mutex)
|
2007-05-03 16:28:23 +00:00
|
|
|
{
|
|
|
|
if (mutex) {
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_mutex_lock(mutex);
|
|
|
|
}
|
2007-09-29 01:06:08 +00:00
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
switch_core_hash_insert(hash, key, data);
|
2007-05-03 16:28:23 +00:00
|
|
|
|
|
|
|
if (mutex) {
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_mutex_unlock(mutex);
|
|
|
|
}
|
2007-05-03 16:28:23 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2011-06-09 17:17:32 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_wrlock(switch_hash_t *hash, const char *key, const void *data, switch_thread_rwlock_t *rwlock)
|
|
|
|
{
|
|
|
|
if (rwlock) {
|
|
|
|
switch_thread_rwlock_wrlock(rwlock);
|
|
|
|
}
|
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
switch_core_hash_insert(hash, key, data);
|
2011-06-09 17:17:32 +00:00
|
|
|
|
|
|
|
if (rwlock) {
|
|
|
|
switch_thread_rwlock_unlock(rwlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_delete(switch_hash_t *hash, const char *key)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2014-03-08 19:37:09 +00:00
|
|
|
switch_hashtable_remove(hash, (void *)key);
|
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_delete_locked(switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
|
2007-05-03 16:28:23 +00:00
|
|
|
{
|
|
|
|
if (mutex) {
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
switch_core_hash_delete(hash, key);
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-05-03 16:28:23 +00:00
|
|
|
if (mutex) {
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_mutex_unlock(mutex);
|
|
|
|
}
|
2007-05-03 16:28:23 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2011-06-09 17:17:32 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_delete_wrlock(switch_hash_t *hash, const char *key, switch_thread_rwlock_t *rwlock)
|
|
|
|
{
|
|
|
|
if (rwlock) {
|
|
|
|
switch_thread_rwlock_wrlock(rwlock);
|
|
|
|
}
|
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
switch_core_hash_delete(hash, key);
|
2011-06-09 17:17:32 +00:00
|
|
|
|
|
|
|
if (rwlock) {
|
|
|
|
switch_thread_rwlock_unlock(rwlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2010-03-24 16:11:24 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_delete_multi(switch_hash_t *hash, switch_hash_delete_callback_t callback, void *pData) {
|
|
|
|
|
|
|
|
switch_hash_index_t *hi = NULL;
|
|
|
|
switch_event_t *event = NULL;
|
|
|
|
switch_event_header_t *header = NULL;
|
|
|
|
switch_status_t status = SWITCH_STATUS_GENERR;
|
|
|
|
|
|
|
|
switch_event_create_subclass(&event, SWITCH_EVENT_CLONE, NULL);
|
|
|
|
switch_assert(event);
|
|
|
|
|
2012-06-20 13:43:36 +00:00
|
|
|
/* iterate through the hash, call callback, if callback returns NULL or true, put the key on the list (event)
|
2010-03-24 16:11:24 +00:00
|
|
|
When done, iterate through the list deleting hash entries
|
|
|
|
*/
|
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
for (hi = switch_core_hash_first(hash); hi; hi = switch_core_hash_next(hi)) {
|
2010-03-24 16:11:24 +00:00
|
|
|
const void *key;
|
|
|
|
void *val;
|
2014-03-08 19:37:09 +00:00
|
|
|
switch_core_hash_this(hi, &key, NULL, &val);
|
2012-06-20 13:43:36 +00:00
|
|
|
if (!callback || callback(key, val, pData)) {
|
2010-03-24 16:11:24 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "delete", (const char *) key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now delete them */
|
|
|
|
for (header = event->headers; header; header = header->next) {
|
|
|
|
if (switch_core_hash_delete(hash, header->value) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(void *) switch_core_hash_find(switch_hash_t *hash, const char *key)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2014-03-08 19:37:09 +00:00
|
|
|
return switch_hashtable_search(hash, (void *)key);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
2007-05-03 16:28:23 +00:00
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(void *) switch_core_hash_find_locked(switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
|
2007-05-03 16:28:23 +00:00
|
|
|
{
|
|
|
|
void *val;
|
|
|
|
|
|
|
|
if (mutex) {
|
|
|
|
switch_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
val = switch_core_hash_find(hash, key);
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-05-03 16:28:23 +00:00
|
|
|
if (mutex) {
|
|
|
|
switch_mutex_unlock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2007-09-29 01:06:08 +00:00
|
|
|
|
2011-06-09 17:17:32 +00:00
|
|
|
SWITCH_DECLARE(void *) switch_core_hash_find_rdlock(switch_hash_t *hash, const char *key, switch_thread_rwlock_t *rwlock)
|
|
|
|
{
|
|
|
|
void *val;
|
|
|
|
|
|
|
|
if (rwlock) {
|
|
|
|
switch_thread_rwlock_rdlock(rwlock);
|
|
|
|
}
|
|
|
|
|
2014-03-08 19:37:09 +00:00
|
|
|
val = switch_core_hash_find(hash, key);
|
2011-06-09 17:17:32 +00:00
|
|
|
|
|
|
|
if (rwlock) {
|
|
|
|
switch_thread_rwlock_unlock(rwlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-10-05 16:11:12 +00:00
|
|
|
SWITCH_DECLARE(switch_hash_index_t *) switch_core_hash_first(switch_hash_t *hash)
|
2007-09-29 01:06:08 +00:00
|
|
|
{
|
2014-03-08 19:37:09 +00:00
|
|
|
return switch_hashtable_first(hash);
|
2007-09-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 16:11:12 +00:00
|
|
|
SWITCH_DECLARE(switch_hash_index_t *) switch_core_hash_next(switch_hash_index_t *hi)
|
2007-09-29 01:06:08 +00:00
|
|
|
{
|
2014-03-08 19:37:09 +00:00
|
|
|
return switch_hashtable_next(hi);
|
2007-09-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 16:11:12 +00:00
|
|
|
SWITCH_DECLARE(void) switch_core_hash_this(switch_hash_index_t *hi, const void **key, switch_ssize_t *klen, void **val)
|
2007-09-29 01:06:08 +00:00
|
|
|
{
|
2014-03-08 19:37:09 +00:00
|
|
|
switch_hashtable_this(hi, key, klen, val);
|
2012-10-05 16:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-27 05:02:52 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-02-03 22:14:57 +00:00
|
|
|
* indent-tabs-mode:t
|
2008-01-27 05:02:52 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2013-06-25 16:50:17 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
|
2008-01-27 05:02:52 +00:00
|
|
|
*/
|