RTS API Documentation  1.10.11
switch_core_hash.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Michael Jerris <mike@jerris.com>
28  * Paul D. Tinsley <pdt at jackhammer.org>
29  *
30  *
31  * switch_core_hash.c -- Main Core Library (hash functions)
32  *
33  */
34 
35 #include <switch.h>
38 
40 {
41  if (case_sensitive) {
43  } else {
45  }
46 }
47 
48 
50 {
51  switch_assert(hash != NULL && *hash != NULL);
52 
54 
55  return SWITCH_STATUS_SUCCESS;
56 }
57 
59 {
60  size_t bytes_required = snprintf(NULL, 0, "%p", data) + 1;
61  char *dkey = malloc(bytes_required);
62  size_t bytes_written = snprintf(dkey, bytes_required, "%p", data);
63 
64  if (bytes_written > 0 && bytes_written < bytes_required) {
66  return SWITCH_STATUS_SUCCESS;
67  }
68  }
69 
70  switch_safe_free(dkey);
71 
72  return SWITCH_STATUS_FALSE;
73 }
74 
76 {
77  char *dkey = strdup(key);
78 
80  return SWITCH_STATUS_SUCCESS;
81  }
82 
83  switch_safe_free(dkey);
84 
85  return SWITCH_STATUS_FALSE;
86 }
87 
89 {
90  char *dkey = strdup(key);
91  char *dup = strdup(str);
92 
93  assert(dup);
94 
96  return SWITCH_STATUS_SUCCESS;
97  }
98 
99  switch_safe_free(dup);
100  switch_safe_free(dkey);
101 
102  return SWITCH_STATUS_FALSE;
103 }
104 
106  char *dkey;
107  void *data;
108 
109  if (!size) return NULL;
110 
111  dkey = strdup(key);
112  data = malloc(size);
113 
114  assert(data);
115 
117  memset(data, 0, size);
118  return data;
119  }
120 
121  free(data);
122  switch_safe_free(dkey);
123 
124  return NULL;
125 }
126 
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 }
143 
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 }
156 
158 {
160 
161  if (mutex) {
162  switch_mutex_lock(mutex);
163  }
164 
165  status = switch_core_hash_insert(hash, key, data);
166 
167  if (mutex) {
168  switch_mutex_unlock(mutex);
169  }
170 
171  return status;
172 }
173 
175 {
177 
178  if (rwlock) {
180  }
181 
182  status = switch_core_hash_insert(hash, key, data);
183 
184  if (rwlock) {
186  }
187 
188  return status;
189 }
190 
192 {
193  return switch_hashtable_remove(hash, (void *)key);
194 }
195 
197 {
198  void *ret = NULL;
199 
200  if (mutex) {
201  switch_mutex_lock(mutex);
202  }
203 
204  ret = switch_core_hash_delete(hash, key);
205 
206  if (mutex) {
207  switch_mutex_unlock(mutex);
208  }
209 
210  return ret;
211 }
212 
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 }
229 
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 }
264 
265 
267 {
268  return switch_hashtable_search(hash, (void *)key);
269 }
270 
272 {
273  void *val;
274 
275  if (mutex) {
276  switch_mutex_lock(mutex);
277  }
278 
279  val = switch_core_hash_find(hash, key);
280 
281 
282  if (mutex) {
283  switch_mutex_unlock(mutex);
284  }
285 
286  return val;
287 }
288 
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 }
305 
307 {
309 
310  if (hi) {
311  free(hi);
312  return SWITCH_FALSE;
313  }
314 
315  return SWITCH_TRUE;
316 
317 }
318 
320 {
321  return switch_hashtable_first_iter(hash, hi);
322 }
323 
325 {
326  return switch_hashtable_next(hi);
327 }
328 
329 SWITCH_DECLARE(void) switch_core_hash_this(switch_hash_index_t *hi, const void **key, switch_ssize_t *klen, void **val)
330 {
331  switch_hashtable_this(hi, key, klen, val);
332 }
333 
335 {
336  switch_hashtable_this_val(hi, val);
337 }
338 
339 
341 {
343 }
344 
346 {
348 
349  return SWITCH_STATUS_SUCCESS;
350 }
351 
353 {
354  uint32_t *k = NULL;
355  int r = 0;
356 
357  switch_zmalloc(k, sizeof(*k));
358  *k = key;
360 
362 }
363 
365 {
366  return switch_hashtable_remove(hash, (void *)&key);
367 }
368 
370 {
371  return switch_hashtable_search(hash, (void *)&key);
372 }
373 
374 
375 /* For Emacs:
376  * Local Variables:
377  * mode:c
378  * indent-tabs-mode:t
379  * tab-width:4
380  * c-basic-offset:4
381  * End:
382  * For VIM:
383  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
384  */
void * switch_core_hash_find_locked(switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
switch_status_t switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:286
switch_status_t switch_core_inthash_init(switch_inthash_t **hash)
switch_status_t switch_core_hash_init_case(switch_hash_t **hash, switch_bool_t case_sensitive)
switch_bool_t(* switch_hash_delete_callback_t)(_In_ const void *key, _In_ const void *val, _In_opt_ void *pData)
void * switch_hashtable_search(switch_hashtable_t *h, void *k)
switch_bool_t
Definition: switch_types.h:437
switch_status_t switch_core_hash_destroy(switch_hash_t **hash)
void(* hashtable_destructor_t)(void *ptr)
void * switch_core_hash_find_rdlock(switch_hash_t *hash, const char *key, switch_thread_rwlock_t *rwlock)
Representation of an event.
Definition: switch_event.h:80
void * switch_core_inthash_find(switch_inthash_t *hash, uint32_t key)
An event Header.
Definition: switch_event.h:65
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.
switch_status_t switch_core_hash_insert_locked(switch_hash_t *hash, const char *key, const void *data, switch_mutex_t *mutex)
#define switch_event_create_subclass(_e, _eid, _sn)
Definition: switch_event.h:153
switch_hash_t * hash
Definition: switch_event.c:76
void switch_core_hash_this(switch_hash_index_t *hi, const void **key, switch_ssize_t *klen, void **val)
switch_hash_index_t * switch_core_hash_first_iter(switch_hash_t *hash, switch_hash_index_t *hi)
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.
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_thread_rwlock_rdlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:250
switch_status_t switch_core_inthash_destroy(switch_inthash_t **hash)
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...
static int switch_hash_equalkeys_ci(void *k1, void *k2)
switch_status_t switch_thread_rwlock_wrlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:260
void * switch_core_hash_delete_wrlock(switch_hash_t *hash, const char *key, switch_thread_rwlock_t *rwlock)
void * switch_core_hash_find(switch_hash_t *hash, const char *key)
void * switch_core_inthash_delete(switch_inthash_t *hash, uint32_t key)
static uint32_t switch_hash_default_ci(void *ky)
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
void switch_core_hash_this_val(switch_hash_index_t *hi, void *val)
intptr_t switch_ssize_t
switch_hashtable_iterator_t * switch_hashtable_first_iter(switch_hashtable_t *h, switch_hashtable_iterator_t *it)
int switch_hashtable_insert_destructor(switch_hashtable_t *h, void *k, void *v, hashtable_flag_t flags, hashtable_destructor_t destructor)
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.
#define switch_zmalloc(ptr, len)
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_inthash_insert(switch_inthash_t *hash, uint32_t key, const void *data)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
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.
#define _In_opt_
static uint32_t switch_hash_default_int(void *ky)
const cJSON *const const cJSON_bool case_sensitive
Definition: switch_cJSON.h:243
void switch_hashtable_this_val(switch_hashtable_iterator_t *i, void *val)
switch_status_t switch_core_hash_delete_multi(switch_hash_t *hash, switch_hash_delete_callback_t callback, void *pData)
#define _In_z_
switch_mutex_t * mutex
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.
struct fspr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_thread_rwlock_t * rwlock
Definition: switch_event.c:75
switch_status_t
Common return values.
struct switch_event_header * next
Definition: switch_event.h:76
switch_hashtable_iterator_t * switch_hashtable_next(switch_hashtable_iterator_t **iP)
#define switch_core_hash_insert(_h, _k, _d)
Definition: switch_core.h:1479
void switch_hashtable_destroy(switch_hashtable_t **h)
Main Library Header.
#define SWITCH_DECLARE(type)
void * switch_core_hash_delete_locked(switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
switch_status_t switch_create_hashtable(switch_hashtable_t **hp, unsigned int minsize, unsigned int(*hashfunction)(void *), int(*key_eq_fn)(void *, void *))
switch_status_t switch_core_hash_insert_destructor(switch_hash_t *hash, const char *key, const void *data, hashtable_destructor_t destructor)
void * switch_hashtable_remove(switch_hashtable_t *h, void *k)
static int switch_hash_equalkeys(void *k1, void *k2)
char * key
Definition: switch_msrp.c:64
static uint32_t switch_hash_default(void *ky)
void switch_event_destroy(switch_event_t **event)
Destroy an event.
switch_bool_t switch_core_hash_empty(switch_hash_t *hash)
tells if a hash is empty
void * switch_core_hash_delete(switch_hash_t *hash, const char *key)
#define switch_assert(expr)
struct fspr_thread_rwlock_t switch_thread_rwlock_t
Definition: switch_apr.h:436
#define switch_core_hash_first(_h)
Definition: switch_core.h:1592
void switch_hashtable_this(switch_hashtable_iterator_t *i, const void **key, switch_ssize_t *klen, void **val)
memset(buf, 0, buflen)
static int switch_hash_equalkeys_int(void *k1, void *k2)
switch_hash_index_t * switch_core_hash_next(switch_hash_index_t **hi)
#define _In_