RTS API Documentation  1.10.11
Functions
switch_core_hash.c File Reference
#include <switch.h>
#include "private/switch_core_pvt.h"
#include "private/switch_hashtable_private.h"
+ Include dependency graph for switch_core_hash.c:

Go to the source code of this file.

Functions

switch_status_t switch_core_hash_init_case (switch_hash_t **hash, switch_bool_t case_sensitive)
 
switch_status_t switch_core_hash_destroy (switch_hash_t **hash)
 
switch_status_t switch_core_hash_insert_pointer (switch_hash_t *hash, const void *data)
 Insert data into a hash with an auto-generated key based on the data pointer. More...
 
switch_status_t switch_core_hash_insert_auto_free (switch_hash_t *hash, const char *key, const void *data)
 Insert data into a hash and set flags so the value is automatically freed on delete. More...
 
switch_status_t switch_core_hash_insert_dup_auto_free (switch_hash_t *hash, const char *key, const char *str)
 Insert strdup(str) into a hash and set flags so the value is automatically freed on delete. More...
 
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)
 Allocate memory and insert into a hash. More...
 
switch_status_t switch_core_hash_insert_dup_destructor (switch_hash_t *hash, const char *key, const char *str, hashtable_destructor_t destructor)
 
switch_status_t switch_core_hash_insert_destructor (switch_hash_t *hash, const char *key, const void *data, hashtable_destructor_t destructor)
 
switch_status_t switch_core_hash_insert_locked (switch_hash_t *hash, const char *key, const void *data, switch_mutex_t *mutex)
 
switch_status_t switch_core_hash_insert_wrlock (switch_hash_t *hash, const char *key, const void *data, switch_thread_rwlock_t *rwlock)
 Retrieve data from a given hash. More...
 
void * switch_core_hash_delete (switch_hash_t *hash, const char *key)
 
void * switch_core_hash_delete_locked (switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
 
void * switch_core_hash_delete_wrlock (switch_hash_t *hash, const char *key, switch_thread_rwlock_t *rwlock)
 
switch_status_t switch_core_hash_delete_multi (switch_hash_t *hash, switch_hash_delete_callback_t callback, void *pData)
 
void * switch_core_hash_find (switch_hash_t *hash, const char *key)
 
void * switch_core_hash_find_locked (switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
 
void * switch_core_hash_find_rdlock (switch_hash_t *hash, const char *key, switch_thread_rwlock_t *rwlock)
 
switch_bool_t switch_core_hash_empty (switch_hash_t *hash)
 tells if a hash is empty More...
 
switch_hash_index_tswitch_core_hash_first_iter (switch_hash_t *hash, switch_hash_index_t *hi)
 
switch_hash_index_tswitch_core_hash_next (switch_hash_index_t **hi)
 
void switch_core_hash_this (switch_hash_index_t *hi, const void **key, switch_ssize_t *klen, void **val)
 
void switch_core_hash_this_val (switch_hash_index_t *hi, void *val)
 
switch_status_t switch_core_inthash_init (switch_inthash_t **hash)
 
switch_status_t switch_core_inthash_destroy (switch_inthash_t **hash)
 
switch_status_t switch_core_inthash_insert (switch_inthash_t *hash, uint32_t key, const void *data)
 
void * switch_core_inthash_delete (switch_inthash_t *hash, uint32_t key)
 
void * switch_core_inthash_find (switch_inthash_t *hash, uint32_t key)
 

Function Documentation

◆ switch_core_hash_delete()

void* switch_core_hash_delete ( switch_hash_t hash,
const char *  key 
)

Definition at line 191 of file switch_core_hash.c.

References switch_hashtable_remove().

Referenced by switch_core_hash_delete_locked(), switch_core_hash_delete_multi(), and switch_core_hash_delete_wrlock().

192 {
193  return switch_hashtable_remove(hash, (void *)key);
194 }
void * switch_hashtable_remove(switch_hashtable_t *h, void *k)
char * key
Definition: switch_msrp.c:64

◆ switch_core_hash_delete_locked()

void* switch_core_hash_delete_locked ( switch_hash_t hash,
const char *  key,
switch_mutex_t mutex 
)

Definition at line 196 of file switch_core_hash.c.

References switch_core_hash_delete(), switch_mutex_lock(), and switch_mutex_unlock().

197 {
198  void *ret = NULL;
199 
200  if (mutex) {
202  }
203 
204  ret = switch_core_hash_delete(hash, key);
205 
206  if (mutex) {
208  }
209 
210  return ret;
211 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
switch_mutex_t * mutex
char * key
Definition: switch_msrp.c:64
void * switch_core_hash_delete(switch_hash_t *hash, const char *key)

◆ switch_core_hash_delete_multi()

switch_status_t switch_core_hash_delete_multi ( switch_hash_t hash,
switch_hash_delete_callback_t  callback,
void *  pData 
)

Definition at line 230 of file switch_core_hash.c.

References key, switch_event_header::next, switch_assert, switch_core_hash_delete(), switch_core_hash_first, switch_core_hash_next(), switch_core_hash_this(), switch_event_add_header_string(), SWITCH_EVENT_CLONE, switch_event_create_subclass, switch_event_destroy(), SWITCH_STACK_BOTTOM, SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, and switch_event_header::value.

230  {
231 
232  switch_hash_index_t *hi = NULL;
233  switch_event_t *event = NULL;
234  switch_event_header_t *header = NULL;
236 
238  switch_assert(event);
239 
240  /* iterate through the hash, call callback, if callback returns NULL or true, put the key on the list (event)
241  When done, iterate through the list deleting hash entries
242  */
243 
244  for (hi = switch_core_hash_first(hash); hi; hi = switch_core_hash_next(&hi)) {
245  const void *key;
246  void *val;
247  switch_core_hash_this(hi, &key, NULL, &val);
248  if (!callback || callback(key, val, pData)) {
249  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "delete", (const char *) key);
250  }
251  }
252 
253  /* now delete them */
254  for (header = event->headers; header; header = header->next) {
255  if (switch_core_hash_delete(hash, header->value)) {
256  status = SWITCH_STATUS_SUCCESS;
257  }
258  }
259 
260  switch_event_destroy(&event);
261 
262  return status;
263 }
Representation of an event.
Definition: switch_event.h:80
An event Header.
Definition: switch_event.h:65
#define switch_event_create_subclass(_e, _eid, _sn)
Definition: switch_event.h:153
void switch_core_hash_this(switch_hash_index_t *hi, const void **key, switch_ssize_t *klen, void **val)
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
switch_status_t
Common return values.
struct switch_event_header * next
Definition: switch_event.h:76
char * key
Definition: switch_msrp.c:64
void switch_event_destroy(switch_event_t **event)
Destroy an event.
void * switch_core_hash_delete(switch_hash_t *hash, const char *key)
#define switch_assert(expr)
#define switch_core_hash_first(_h)
Definition: switch_core.h:1592
switch_hash_index_t * switch_core_hash_next(switch_hash_index_t **hi)

◆ switch_core_hash_delete_wrlock()

void* switch_core_hash_delete_wrlock ( switch_hash_t hash,
const char *  key,
switch_thread_rwlock_t rwlock 
)

Definition at line 213 of file switch_core_hash.c.

References switch_core_hash_delete(), switch_thread_rwlock_unlock(), and switch_thread_rwlock_wrlock().

214 {
215  void *ret = NULL;
216 
217  if (rwlock) {
219  }
220 
221  ret = switch_core_hash_delete(hash, key);
222 
223  if (rwlock) {
225  }
226 
227  return ret;
228 }
switch_status_t switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:286
switch_status_t switch_thread_rwlock_wrlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:260
switch_thread_rwlock_t * rwlock
Definition: switch_event.c:75
char * key
Definition: switch_msrp.c:64
void * switch_core_hash_delete(switch_hash_t *hash, const char *key)

◆ switch_core_hash_destroy()

switch_status_t switch_core_hash_destroy ( switch_hash_t **  hash)

Definition at line 49 of file switch_core_hash.c.

References switch_assert, switch_hashtable_destroy(), and SWITCH_STATUS_SUCCESS.

50 {
51  switch_assert(hash != NULL && *hash != NULL);
52 
54 
55  return SWITCH_STATUS_SUCCESS;
56 }
void switch_hashtable_destroy(switch_hashtable_t **h)
#define switch_assert(expr)

◆ switch_core_hash_find()

void* switch_core_hash_find ( switch_hash_t hash,
const char *  key 
)

Definition at line 266 of file switch_core_hash.c.

References switch_hashtable_search().

Referenced by switch_core_hash_find_locked(), and switch_core_hash_find_rdlock().

267 {
268  return switch_hashtable_search(hash, (void *)key);
269 }
void * switch_hashtable_search(switch_hashtable_t *h, void *k)
char * key
Definition: switch_msrp.c:64

◆ switch_core_hash_find_locked()

void* switch_core_hash_find_locked ( switch_hash_t hash,
const char *  key,
switch_mutex_t mutex 
)

Definition at line 271 of file switch_core_hash.c.

References switch_core_hash_find(), switch_mutex_lock(), and switch_mutex_unlock().

272 {
273  void *val;
274 
275  if (mutex) {
277  }
278 
279  val = switch_core_hash_find(hash, key);
280 
281 
282  if (mutex) {
284  }
285 
286  return val;
287 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
void * switch_core_hash_find(switch_hash_t *hash, const char *key)
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
switch_mutex_t * mutex
char * key
Definition: switch_msrp.c:64

◆ switch_core_hash_find_rdlock()

void* switch_core_hash_find_rdlock ( switch_hash_t hash,
const char *  key,
switch_thread_rwlock_t rwlock 
)

Definition at line 289 of file switch_core_hash.c.

References switch_core_hash_find(), switch_thread_rwlock_rdlock(), and switch_thread_rwlock_unlock().

290 {
291  void *val;
292 
293  if (rwlock) {
295  }
296 
297  val = switch_core_hash_find(hash, key);
298 
299  if (rwlock) {
301  }
302 
303  return val;
304 }
switch_status_t switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:286
switch_status_t switch_thread_rwlock_rdlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:250
void * switch_core_hash_find(switch_hash_t *hash, const char *key)
switch_thread_rwlock_t * rwlock
Definition: switch_event.c:75
char * key
Definition: switch_msrp.c:64

◆ switch_core_hash_first_iter()

switch_hash_index_t* switch_core_hash_first_iter ( switch_hash_t hash,
switch_hash_index_t hi 
)

Definition at line 319 of file switch_core_hash.c.

References switch_hashtable_first_iter().

320 {
321  return switch_hashtable_first_iter(hash, hi);
322 }
switch_hashtable_iterator_t * switch_hashtable_first_iter(switch_hashtable_t *h, switch_hashtable_iterator_t *it)

◆ switch_core_hash_init_case()

switch_status_t switch_core_hash_init_case ( switch_hash_t **  hash,
switch_bool_t  case_sensitive 
)

Definition at line 39 of file switch_core_hash.c.

References switch_create_hashtable(), switch_hash_default(), switch_hash_default_ci(), switch_hash_equalkeys(), and switch_hash_equalkeys_ci().

40 {
41  if (case_sensitive) {
43  } else {
45  }
46 }
static int switch_hash_equalkeys_ci(void *k1, void *k2)
static uint32_t switch_hash_default_ci(void *ky)
const cJSON *const const cJSON_bool case_sensitive
Definition: switch_cJSON.h:243
switch_status_t switch_create_hashtable(switch_hashtable_t **hp, unsigned int minsize, unsigned int(*hashfunction)(void *), int(*key_eq_fn)(void *, void *))
static int switch_hash_equalkeys(void *k1, void *k2)
static uint32_t switch_hash_default(void *ky)

◆ switch_core_hash_insert_destructor()

switch_status_t switch_core_hash_insert_destructor ( switch_hash_t hash,
const char *  key,
const void *  data,
hashtable_destructor_t  destructor 
)

Definition at line 144 of file switch_core_hash.c.

References HASHTABLE_DUP_CHECK, HASHTABLE_FLAG_FREE_KEY, switch_hashtable_insert_destructor(), switch_safe_free, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

145 {
146  char *dkey = strdup(key);
147 
148  if (switch_hashtable_insert_destructor(hash, dkey, (void *)data, HASHTABLE_FLAG_FREE_KEY | HASHTABLE_DUP_CHECK, destructor)) {
149  return SWITCH_STATUS_SUCCESS;
150  }
151 
152  switch_safe_free(dkey);
153 
154  return SWITCH_STATUS_FALSE;
155 }
int switch_hashtable_insert_destructor(switch_hashtable_t *h, void *k, void *v, hashtable_flag_t flags, hashtable_destructor_t destructor)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
char * key
Definition: switch_msrp.c:64

◆ switch_core_hash_insert_dup_destructor()

switch_status_t switch_core_hash_insert_dup_destructor ( switch_hash_t hash,
const char *  key,
const char *  str,
hashtable_destructor_t  destructor 
)

Definition at line 127 of file switch_core_hash.c.

References HASHTABLE_DUP_CHECK, HASHTABLE_FLAG_FREE_KEY, switch_hashtable_insert_destructor(), switch_safe_free, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

128 {
129  char *dkey = strdup(key);
130  char *dup = strdup(str);
131 
132  assert(dup);
133 
134  if (switch_hashtable_insert_destructor(hash, dkey, (void *)dup, HASHTABLE_FLAG_FREE_KEY | HASHTABLE_DUP_CHECK, destructor)) {
135  return SWITCH_STATUS_SUCCESS;
136  }
137 
138  switch_safe_free(dup);
139  switch_safe_free(dkey);
140 
141  return SWITCH_STATUS_FALSE;
142 }
int switch_hashtable_insert_destructor(switch_hashtable_t *h, void *k, void *v, hashtable_flag_t flags, hashtable_destructor_t destructor)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
char * key
Definition: switch_msrp.c:64

◆ switch_core_hash_insert_locked()

switch_status_t switch_core_hash_insert_locked ( switch_hash_t hash,
const char *  key,
const void *  data,
switch_mutex_t mutex 
)

Definition at line 157 of file switch_core_hash.c.

References switch_core_hash_insert, switch_mutex_lock(), switch_mutex_unlock(), and SWITCH_STATUS_FALSE.

158 {
160 
161  if (mutex) {
163  }
164 
165  status = switch_core_hash_insert(hash, key, data);
166 
167  if (mutex) {
169  }
170 
171  return status;
172 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
switch_mutex_t * mutex
switch_status_t
Common return values.
#define switch_core_hash_insert(_h, _k, _d)
Definition: switch_core.h:1479
char * key
Definition: switch_msrp.c:64

◆ switch_core_hash_next()

switch_hash_index_t* switch_core_hash_next ( switch_hash_index_t **  hi)

Definition at line 324 of file switch_core_hash.c.

References switch_hashtable_next().

Referenced by switch_core_hash_delete_multi().

325 {
326  return switch_hashtable_next(hi);
327 }
switch_hashtable_iterator_t * switch_hashtable_next(switch_hashtable_iterator_t **iP)

◆ switch_core_hash_this()

void switch_core_hash_this ( switch_hash_index_t hi,
const void **  key,
switch_ssize_t klen,
void **  val 
)

Definition at line 329 of file switch_core_hash.c.

References switch_hashtable_this().

Referenced by switch_core_hash_delete_multi().

330 {
331  switch_hashtable_this(hi, key, klen, val);
332 }
char * key
Definition: switch_msrp.c:64
void switch_hashtable_this(switch_hashtable_iterator_t *i, const void **key, switch_ssize_t *klen, void **val)