FreeSWITCH API Documentation  1.7.0
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  int r = 0;
61 
62  r = switch_hashtable_insert_destructor(hash, strdup(key), (void *)data, HASHTABLE_FLAG_FREE_KEY | HASHTABLE_DUP_CHECK, destructor);
63 
65 }
66 
68 {
70 
71  if (mutex) {
72  switch_mutex_lock(mutex);
73  }
74 
75  status = switch_core_hash_insert(hash, key, data);
76 
77  if (mutex) {
78  switch_mutex_unlock(mutex);
79  }
80 
81  return status;
82 }
83 
85 {
87 
88  if (rwlock) {
90  }
91 
92  status = switch_core_hash_insert(hash, key, data);
93 
94  if (rwlock) {
96  }
97 
98  return status;
99 }
100 
102 {
103  return switch_hashtable_remove(hash, (void *)key);
104 }
105 
107 {
108  void *ret = NULL;
109 
110  if (mutex) {
111  switch_mutex_lock(mutex);
112  }
113 
114  switch_core_hash_delete(hash, key);
115 
116  if (mutex) {
117  switch_mutex_unlock(mutex);
118  }
119 
120  return ret;
121 }
122 
124 {
125  void *ret = NULL;
126 
127  if (rwlock) {
129  }
130 
131  ret = switch_core_hash_delete(hash, key);
132 
133  if (rwlock) {
135  }
136 
137  return ret;
138 }
139 
141 
142  switch_hash_index_t *hi = NULL;
143  switch_event_t *event = NULL;
144  switch_event_header_t *header = NULL;
146 
148  switch_assert(event);
149 
150  /* iterate through the hash, call callback, if callback returns NULL or true, put the key on the list (event)
151  When done, iterate through the list deleting hash entries
152  */
153 
154  for (hi = switch_core_hash_first(hash); hi; hi = switch_core_hash_next(&hi)) {
155  const void *key;
156  void *val;
157  switch_core_hash_this(hi, &key, NULL, &val);
158  if (!callback || callback(key, val, pData)) {
159  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "delete", (const char *) key);
160  }
161  }
162 
163  /* now delete them */
164  for (header = event->headers; header; header = header->next) {
165  if (switch_core_hash_delete(hash, header->value)) {
166  status = SWITCH_STATUS_SUCCESS;
167  }
168  }
169 
170  switch_event_destroy(&event);
171 
172  return status;
173 }
174 
175 
177 {
178  return switch_hashtable_search(hash, (void *)key);
179 }
180 
182 {
183  void *val;
184 
185  if (mutex) {
186  switch_mutex_lock(mutex);
187  }
188 
189  val = switch_core_hash_find(hash, key);
190 
191 
192  if (mutex) {
193  switch_mutex_unlock(mutex);
194  }
195 
196  return val;
197 }
198 
200 {
201  void *val;
202 
203  if (rwlock) {
205  }
206 
207  val = switch_core_hash_find(hash, key);
208 
209  if (rwlock) {
211  }
212 
213  return val;
214 }
215 
217 {
219 
220  if (hi) {
221  switch_safe_free(hi);
222  return SWITCH_FALSE;
223  }
224 
225  return SWITCH_TRUE;
226 
227 }
228 
230 {
231  return switch_hashtable_first_iter(hash, hi);
232 }
233 
235 {
236  return switch_hashtable_next(hi);
237 }
238 
239 SWITCH_DECLARE(void) switch_core_hash_this(switch_hash_index_t *hi, const void **key, switch_ssize_t *klen, void **val)
240 {
241  switch_hashtable_this(hi, key, klen, val);
242 }
243 
245 {
246  switch_hashtable_this_val(hi, val);
247 }
248 
249 
251 {
253 }
254 
256 {
258 
259  return SWITCH_STATUS_SUCCESS;
260 }
261 
263 {
264  uint32_t *k = NULL;
265  int r = 0;
266 
267  switch_zmalloc(k, sizeof(*k));
268  *k = key;
270 
272 }
273 
275 {
276  return switch_hashtable_remove(hash, (void *)&key);
277 }
278 
280 {
281  return switch_hashtable_search(hash, (void *)&key);
282 }
283 
284 
285 /* For Emacs:
286  * Local Variables:
287  * mode:c
288  * indent-tabs-mode:t
289  * tab-width:4
290  * c-basic-offset:4
291  * End:
292  * For VIM:
293  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
294  */
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:263
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)
void * switch_hashtable_search(switch_hashtable_t *h, void *k)
switch_bool_t
Definition: switch_types.h:405
switch_status_t switch_core_hash_destroy(switch_hash_t **hash)
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_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:74
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)
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_thread_rwlock_rdlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:227
switch_status_t switch_core_inthash_destroy(switch_inthash_t **hash)
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:237
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:285
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_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:789
struct apr_thread_rwlock_t switch_thread_rwlock_t
Definition: switch_apr.h:436
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.
static uint32_t switch_hash_default_int(void *ky)
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)
switch_mutex_t * mutex
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_thread_rwlock_t * rwlock
Definition: switch_event.c:73
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:1410
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)
switch_bool_t(* switch_hash_delete_callback_t)(_In_ const void *key, _In_ const void *val, _In_opt_ void *pData)
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)
#define switch_core_hash_first(_h)
Definition: switch_core.h:1501
void switch_hashtable_this(switch_hashtable_iterator_t *i, const void **key, switch_ssize_t *klen, void **val)
static int switch_hash_equalkeys_int(void *k1, void *k2)
switch_hash_index_t * switch_core_hash_next(switch_hash_index_t **hi)
void(* hashtable_destructor_t)(void *ptr)