Browse code

sca: call unsafe find entry function to avoid infinite recursion

Daniel-Constantin Mierla authored on 08/09/2021 11:30:31
Showing 1 changed files
... ...
@@ -205,7 +205,7 @@ sca_hash_entry *sca_hash_table_slot_kv_find_entry(sca_hash_slot *slot, str *key)
205 205
 	sca_hash_entry *e;
206 206
 
207 207
 	lock_get(&slot->lock);
208
-	e = sca_hash_table_slot_kv_find_entry(slot, key);
208
+	e = sca_hash_table_slot_kv_find_entry_unsafe(slot, key);
209 209
 	lock_release(&slot->lock);
210 210
 
211 211
 	return (e);
Browse code

sca: clang format code

Victor Seva authored on 16/06/2020 07:31:55
Showing 1 changed files
... ...
@@ -31,14 +31,14 @@ int sca_hash_table_create(sca_hash_table **ht, unsigned int size)
31 31
 	assert(ht != NULL);
32 32
 
33 33
 	*ht = shm_malloc(sizeof(sca_hash_table));
34
-	if (*ht == NULL) {
34
+	if(*ht == NULL) {
35 35
 		LM_ERR("Failed to shm_malloc space for hash table\n");
36 36
 		return (-1);
37 37
 	}
38 38
 
39 39
 	(*ht)->size = size;
40
-	(*ht)->slots = (sca_hash_slot *) shm_malloc(size * sizeof(sca_hash_slot));
41
-	if ((*ht)->slots == NULL) {
40
+	(*ht)->slots = (sca_hash_slot *)shm_malloc(size * sizeof(sca_hash_slot));
41
+	if((*ht)->slots == NULL) {
42 42
 		LM_ERR("Failed to shm_malloc hash table slots\n");
43 43
 		shm_free(*ht);
44 44
 		*ht = NULL;
... ...
@@ -46,8 +46,8 @@ int sca_hash_table_create(sca_hash_table **ht, unsigned int size)
46 46
 	}
47 47
 	memset((*ht)->slots, 0, size * sizeof(sca_hash_slot));
48 48
 
49
-	for (i = 0; i < (*ht)->size; i++) {
50
-		if (lock_init(&(*ht)->slots[i].lock) == NULL) {
49
+	for(i = 0; i < (*ht)->size; i++) {
50
+		if(lock_init(&(*ht)->slots[i].lock) == NULL) {
51 51
 			LM_ERR("Failed to initialized lock in hash table slot %d\n", i);
52 52
 			shm_free(*ht);
53 53
 			*ht = NULL;
... ...
@@ -69,8 +69,8 @@ int sca_hash_table_slot_kv_insert_unsafe(sca_hash_slot *slot, void *value,
69 69
 	assert(value != NULL);
70 70
 	assert(e_free != NULL);
71 71
 
72
-	new_entry = (sca_hash_entry *) shm_malloc(sizeof(sca_hash_entry));
73
-	if (new_entry == NULL) {
72
+	new_entry = (sca_hash_entry *)shm_malloc(sizeof(sca_hash_entry));
73
+	if(new_entry == NULL) {
74 74
 		LM_ERR("Failed to shm_malloc new hash table entry for slot %p\n", slot);
75 75
 		return (-1);
76 76
 	}
... ...
@@ -95,8 +95,8 @@ int sca_hash_table_slot_kv_insert(sca_hash_slot *slot, void *value,
95 95
 
96 96
 	lock_get(&slot->lock);
97 97
 
98
-	rc = sca_hash_table_slot_kv_insert_unsafe(slot, value, e_compare,
99
-			e_description, e_free);
98
+	rc = sca_hash_table_slot_kv_insert_unsafe(
99
+			slot, value, e_compare, e_description, e_free);
100 100
 
101 101
 	lock_release(&slot->lock);
102 102
 
... ...
@@ -111,8 +111,8 @@ int sca_hash_table_index_kv_insert(sca_hash_table *ht, int slot_idx,
111 111
 	assert(ht->slots != NULL);
112 112
 	assert(slot_idx >= 0 && slot_idx < ht->size);
113 113
 
114
-	return (sca_hash_table_slot_kv_insert(&ht->slots[slot_idx], value,
115
-			e_compare, e_description, e_free));
114
+	return (sca_hash_table_slot_kv_insert(
115
+			&ht->slots[slot_idx], value, e_compare, e_description, e_free));
116 116
 }
117 117
 
118 118
 int sca_hash_table_kv_insert(sca_hash_table *ht, str *key, void *value,
... ...
@@ -125,8 +125,8 @@ int sca_hash_table_kv_insert(sca_hash_table *ht, str *key, void *value,
125 125
 	assert(ht != NULL && !SCA_STR_EMPTY(key) && value != NULL);
126 126
 
127 127
 	hash_idx = sca_hash_table_index_for_key(ht, key);
128
-	rc = sca_hash_table_index_kv_insert(ht, hash_idx, value, e_compare,
129
-			e_description, e_free);
128
+	rc = sca_hash_table_index_kv_insert(
129
+			ht, hash_idx, value, e_compare, e_description, e_free);
130 130
 
131 131
 	return (rc);
132 132
 }
... ...
@@ -138,8 +138,8 @@ void *sca_hash_table_slot_kv_find_unsafe(sca_hash_slot *slot, str *key)
138 138
 
139 139
 	assert(slot != NULL && !SCA_STR_EMPTY(key));
140 140
 
141
-	for (e = slot->entries; e != NULL; e = e->next) {
142
-		if (e->compare(key, e->value) == 0) {
141
+	for(e = slot->entries; e != NULL; e = e->next) {
142
+		if(e->compare(key, e->value) == 0) {
143 143
 			value = e->value;
144 144
 		}
145 145
 	}
... ...
@@ -158,8 +158,8 @@ void *sca_hash_table_slot_kv_find(sca_hash_slot *slot, str *key)
158 158
 	return (value);
159 159
 }
160 160
 
161
-void *sca_hash_table_index_kv_find_unsafe(sca_hash_table *ht, int slot_idx,
162
-		str *key)
161
+void *sca_hash_table_index_kv_find_unsafe(
162
+		sca_hash_table *ht, int slot_idx, str *key)
163 163
 {
164 164
 	assert(ht != NULL && !SCA_STR_EMPTY(key));
165 165
 	assert(slot_idx >= 0 && slot_idx < ht->size);
... ...
@@ -167,8 +167,7 @@ void *sca_hash_table_index_kv_find_unsafe(sca_hash_table *ht, int slot_idx,
167 167
 	return (sca_hash_table_slot_kv_find_unsafe(&ht->slots[slot_idx], key));
168 168
 }
169 169
 
170
-void *
171
-sca_hash_table_index_kv_find(sca_hash_table *ht, int slot_idx, str *key)
170
+void *sca_hash_table_index_kv_find(sca_hash_table *ht, int slot_idx, str *key)
172 171
 {
173 172
 	assert(ht != NULL && !SCA_STR_EMPTY(key));
174 173
 	assert(slot_idx >= 0 && slot_idx < ht->size);
... ...
@@ -185,15 +184,15 @@ void *sca_hash_table_kv_find(sca_hash_table *ht, str *key)
185 184
 	return (sca_hash_table_index_kv_find(ht, slot_idx, key));
186 185
 }
187 186
 
188
-sca_hash_entry *sca_hash_table_slot_kv_find_entry_unsafe(sca_hash_slot *slot,
189
-		str *key)
187
+sca_hash_entry *sca_hash_table_slot_kv_find_entry_unsafe(
188
+		sca_hash_slot *slot, str *key)
190 189
 {
191 190
 	sca_hash_entry *e = NULL;
192 191
 
193 192
 	assert(slot != NULL && !SCA_STR_EMPTY(key));
194 193
 
195
-	for (e = slot->entries; e != NULL; e = e->next) {
196
-		if (e->compare(key, e->value) == 0) {
194
+	for(e = slot->entries; e != NULL; e = e->next) {
195
+		if(e->compare(key, e->value) == 0) {
197 196
 			break;
198 197
 		}
199 198
 	}
... ...
@@ -220,16 +219,16 @@ void sca_hash_entry_free(sca_hash_entry *e)
220 219
 	shm_free(e);
221 220
 }
222 221
 
223
-sca_hash_entry *sca_hash_table_slot_unlink_entry_unsafe(sca_hash_slot *slot,
224
-		sca_hash_entry *e)
222
+sca_hash_entry *sca_hash_table_slot_unlink_entry_unsafe(
223
+		sca_hash_slot *slot, sca_hash_entry *e)
225 224
 {
226 225
 	sca_hash_entry **cur_e;
227 226
 
228 227
 	assert(slot != NULL);
229 228
 	assert(e != NULL);
230 229
 
231
-	for (cur_e = &slot->entries; *cur_e != NULL; cur_e = &(*cur_e)->next) {
232
-		if (*cur_e == e) {
230
+	for(cur_e = &slot->entries; *cur_e != NULL; cur_e = &(*cur_e)->next) {
231
+		if(*cur_e == e) {
233 232
 			*cur_e = e->next;
234 233
 
235 234
 			/* ensure any attempted traversal using this entry goes nowhere */
... ...
@@ -248,12 +247,12 @@ int sca_hash_table_slot_kv_delete_unsafe(sca_hash_slot *slot, str *key)
248 247
 	sca_hash_entry *e;
249 248
 
250 249
 	e = sca_hash_table_slot_kv_find_entry_unsafe(slot, key);
251
-	if (e == NULL) {
250
+	if(e == NULL) {
252 251
 		return (-1);
253 252
 	}
254 253
 
255 254
 	e = sca_hash_table_slot_unlink_entry_unsafe(slot, e);
256
-	if (e) {
255
+	if(e) {
257 256
 		e->free_entry(e->value);
258 257
 		shm_free(e);
259 258
 	}
... ...
@@ -290,8 +289,8 @@ static void sca_hash_slot_print(sca_hash_slot *hs)
290 289
 {
291 290
 	sca_hash_entry *e;
292 291
 
293
-	for (e = hs->entries; e != NULL; e = e->next) {
294
-		if (e->description != NULL) {
292
+	for(e = hs->entries; e != NULL; e = e->next) {
293
+		if(e->description != NULL) {
295 294
 			e->description(e->value);
296 295
 		} else {
297 296
 			LM_DBG("0x%p\n", e->value);
... ...
@@ -303,7 +302,7 @@ void sca_hash_table_print(sca_hash_table *ht)
303 302
 {
304 303
 	unsigned int i;
305 304
 
306
-	for (i = 0; i < ht->size; i++) {
305
+	for(i = 0; i < ht->size; i++) {
307 306
 		LM_DBG("SLOT %d:\n", i);
308 307
 		sca_hash_slot_print(&ht->slots[i]);
309 308
 	}
... ...
@@ -314,18 +313,18 @@ void sca_hash_table_free(sca_hash_table *ht)
314 313
 	sca_hash_entry *e, *e_tmp;
315 314
 	unsigned int i;
316 315
 
317
-	if (ht == NULL) {
316
+	if(ht == NULL) {
318 317
 		return;
319 318
 	}
320 319
 
321
-	for (i = 0; i < ht->size; i++) {
322
-		if (ht->slots[i].entries == NULL) {
320
+	for(i = 0; i < ht->size; i++) {
321
+		if(ht->slots[i].entries == NULL) {
323 322
 			continue;
324 323
 		}
325 324
 
326 325
 		sca_hash_table_lock_index(ht, i);
327 326
 
328
-		for (e = ht->slots[i].entries; e != NULL; e = e_tmp) {
327
+		for(e = ht->slots[i].entries; e != NULL; e = e_tmp) {
329 328
 			e_tmp = e->next;
330 329
 
331 330
 			e->free_entry(e->value);
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,344 @@
1
+/*
2
+ * Copyright (C) 2012 Andrew Mortensen
3
+ *
4
+ * This file is part of the sca module for Kamailio, a free SIP server.
5
+ *
6
+ * The sca module is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * The sca module is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA. 02110-1301 USA
19
+ */
20
+#include "sca_common.h"
21
+
22
+#include <assert.h>
23
+
24
+#include "sca.h"
25
+#include "sca_hash.h"
26
+
27
+int sca_hash_table_create(sca_hash_table **ht, unsigned int size)
28
+{
29
+	int i;
30
+
31
+	assert(ht != NULL);
32
+
33
+	*ht = shm_malloc(sizeof(sca_hash_table));
34
+	if (*ht == NULL) {
35
+		LM_ERR("Failed to shm_malloc space for hash table\n");
36
+		return (-1);
37
+	}
38
+
39
+	(*ht)->size = size;
40
+	(*ht)->slots = (sca_hash_slot *) shm_malloc(size * sizeof(sca_hash_slot));
41
+	if ((*ht)->slots == NULL) {
42
+		LM_ERR("Failed to shm_malloc hash table slots\n");
43
+		shm_free(*ht);
44
+		*ht = NULL;
45
+		return (-1);
46
+	}
47
+	memset((*ht)->slots, 0, size * sizeof(sca_hash_slot));
48
+
49
+	for (i = 0; i < (*ht)->size; i++) {
50
+		if (lock_init(&(*ht)->slots[i].lock) == NULL) {
51
+			LM_ERR("Failed to initialized lock in hash table slot %d\n", i);
52
+			shm_free(*ht);
53
+			*ht = NULL;
54
+			return (-1);
55
+		}
56
+	}
57
+
58
+	return (0);
59
+}
60
+
61
+int sca_hash_table_slot_kv_insert_unsafe(sca_hash_slot *slot, void *value,
62
+		int (*e_compare)(str *, void *), void (*e_description)(void *),
63
+		void (*e_free)(void *))
64
+{
65
+	sca_hash_entry *new_entry;
66
+	sca_hash_entry **cur_entry;
67
+
68
+	assert(slot != NULL);
69
+	assert(value != NULL);
70
+	assert(e_free != NULL);
71
+
72
+	new_entry = (sca_hash_entry *) shm_malloc(sizeof(sca_hash_entry));
73
+	if (new_entry == NULL) {
74
+		LM_ERR("Failed to shm_malloc new hash table entry for slot %p\n", slot);
75
+		return (-1);
76
+	}
77
+	new_entry->value = value;
78
+	new_entry->compare = e_compare;
79
+	new_entry->description = e_description;
80
+	new_entry->free_entry = e_free;
81
+	new_entry->slot = slot;
82
+
83
+	cur_entry = &slot->entries;
84
+	new_entry->next = *cur_entry;
85
+	*cur_entry = new_entry;
86
+
87
+	return (0);
88
+}
89
+
90
+int sca_hash_table_slot_kv_insert(sca_hash_slot *slot, void *value,
91
+		int (*e_compare)(str *, void *), void (*e_description)(void *),
92
+		void (*e_free)(void *))
93
+{
94
+	int rc;
95
+
96
+	lock_get(&slot->lock);
97
+
98
+	rc = sca_hash_table_slot_kv_insert_unsafe(slot, value, e_compare,
99
+			e_description, e_free);
100
+
101
+	lock_release(&slot->lock);
102
+
103
+	return (rc);
104
+}
105
+
106
+int sca_hash_table_index_kv_insert(sca_hash_table *ht, int slot_idx,
107
+		void *value, int (*e_compare)(str *, void *),
108
+		void (*e_description)(void *), void (*e_free)(void *))
109
+{
110
+	assert(ht != NULL);
111
+	assert(ht->slots != NULL);
112
+	assert(slot_idx >= 0 && slot_idx < ht->size);
113
+
114
+	return (sca_hash_table_slot_kv_insert(&ht->slots[slot_idx], value,
115
+			e_compare, e_description, e_free));
116
+}
117
+
118
+int sca_hash_table_kv_insert(sca_hash_table *ht, str *key, void *value,
119
+		int (*e_compare)(str *, void *), void (*e_description)(void *),
120
+		void (*e_free)(void *))
121
+{
122
+	int hash_idx;
123
+	int rc;
124
+
125
+	assert(ht != NULL && !SCA_STR_EMPTY(key) && value != NULL);
126
+
127
+	hash_idx = sca_hash_table_index_for_key(ht, key);
128
+	rc = sca_hash_table_index_kv_insert(ht, hash_idx, value, e_compare,
129
+			e_description, e_free);
130
+
131
+	return (rc);
132
+}
133
+
134
+void *sca_hash_table_slot_kv_find_unsafe(sca_hash_slot *slot, str *key)
135
+{
136
+	sca_hash_entry *e;
137
+	void *value = NULL;
138
+
139
+	assert(slot != NULL && !SCA_STR_EMPTY(key));
140
+
141
+	for (e = slot->entries; e != NULL; e = e->next) {
142
+		if (e->compare(key, e->value) == 0) {
143
+			value = e->value;
144
+		}
145
+	}
146
+
147
+	return (value);
148
+}
149
+
150
+void *sca_hash_table_slot_kv_find(sca_hash_slot *slot, str *key)
151
+{
152
+	void *value;
153
+
154
+	lock_get(&slot->lock);
155
+	value = sca_hash_table_slot_kv_find_unsafe(slot, key);
156
+	lock_release(&slot->lock);
157
+
158
+	return (value);
159
+}
160
+
161
+void *sca_hash_table_index_kv_find_unsafe(sca_hash_table *ht, int slot_idx,
162
+		str *key)
163
+{
164
+	assert(ht != NULL && !SCA_STR_EMPTY(key));
165
+	assert(slot_idx >= 0 && slot_idx < ht->size);
166
+
167
+	return (sca_hash_table_slot_kv_find_unsafe(&ht->slots[slot_idx], key));
168
+}
169
+
170
+void *
171
+sca_hash_table_index_kv_find(sca_hash_table *ht, int slot_idx, str *key)
172
+{
173
+	assert(ht != NULL && !SCA_STR_EMPTY(key));
174
+	assert(slot_idx >= 0 && slot_idx < ht->size);
175
+
176
+	return (sca_hash_table_slot_kv_find(&ht->slots[slot_idx], key));
177
+}
178
+
179
+void *sca_hash_table_kv_find(sca_hash_table *ht, str *key)
180
+{
181
+	int slot_idx;
182
+
183
+	slot_idx = sca_hash_table_index_for_key(ht, key);
184
+
185
+	return (sca_hash_table_index_kv_find(ht, slot_idx, key));
186
+}
187
+
188
+sca_hash_entry *sca_hash_table_slot_kv_find_entry_unsafe(sca_hash_slot *slot,
189
+		str *key)
190
+{
191
+	sca_hash_entry *e = NULL;
192
+
193
+	assert(slot != NULL && !SCA_STR_EMPTY(key));
194
+
195
+	for (e = slot->entries; e != NULL; e = e->next) {
196
+		if (e->compare(key, e->value) == 0) {
197
+			break;
198
+		}
199
+	}
200
+
201
+	return (e);
202
+}
203
+
204
+sca_hash_entry *sca_hash_table_slot_kv_find_entry(sca_hash_slot *slot, str *key)
205
+{
206
+	sca_hash_entry *e;
207
+
208
+	lock_get(&slot->lock);
209
+	e = sca_hash_table_slot_kv_find_entry(slot, key);
210
+	lock_release(&slot->lock);
211
+
212
+	return (e);
213
+}
214
+
215
+void sca_hash_entry_free(sca_hash_entry *e)
216
+{
217
+	assert(e != NULL);
218
+
219
+	e->free_entry(e->value);
220
+	shm_free(e);
221
+}
222
+
223
+sca_hash_entry *sca_hash_table_slot_unlink_entry_unsafe(sca_hash_slot *slot,
224
+		sca_hash_entry *e)
225
+{
226
+	sca_hash_entry **cur_e;
227
+
228
+	assert(slot != NULL);
229
+	assert(e != NULL);
230
+
231
+	for (cur_e = &slot->entries; *cur_e != NULL; cur_e = &(*cur_e)->next) {
232
+		if (*cur_e == e) {
233
+			*cur_e = e->next;
234
+
235
+			/* ensure any attempted traversal using this entry goes nowhere */
236
+			e->next = NULL;
237
+			e->slot = NULL;
238
+
239
+			break;
240
+		}
241
+	}
242
+
243
+	return (e);
244
+}
245
+
246
+int sca_hash_table_slot_kv_delete_unsafe(sca_hash_slot *slot, str *key)
247
+{
248
+	sca_hash_entry *e;
249
+
250
+	e = sca_hash_table_slot_kv_find_entry_unsafe(slot, key);
251
+	if (e == NULL) {
252
+		return (-1);
253
+	}
254
+
255
+	e = sca_hash_table_slot_unlink_entry_unsafe(slot, e);
256
+	if (e) {
257
+		e->free_entry(e->value);
258
+		shm_free(e);
259
+	}
260
+
261
+	return (0);
262
+}
263
+
264
+int sca_hash_table_slot_kv_delete(sca_hash_slot *slot, str *key)
265
+{
266
+	int rc;
267
+
268
+	lock_get(&slot->lock);
269
+	rc = sca_hash_table_slot_kv_delete_unsafe(slot, key);
270
+	lock_release(&slot->lock);
271
+
272
+	return (rc);
273
+}
274
+
275
+int sca_hash_table_index_kv_delete(sca_hash_table *ht, int slot_idx, str *key)
276
+{
277
+	return (sca_hash_table_slot_kv_delete(&ht->slots[slot_idx], key));
278
+}
279
+
280
+int sca_hash_table_kv_delete(sca_hash_table *ht, str *key)
281
+{
282
+	int slot_idx;
283
+
284
+	slot_idx = sca_hash_table_index_for_key(ht, key);
285
+
286
+	return (sca_hash_table_index_kv_delete(ht, slot_idx, key));
287
+}
288
+
289
+static void sca_hash_slot_print(sca_hash_slot *hs)
290
+{
291
+	sca_hash_entry *e;
292
+
293
+	for (e = hs->entries; e != NULL; e = e->next) {
294
+		if (e->description != NULL) {
295
+			e->description(e->value);
296
+		} else {
297
+			LM_DBG("0x%p\n", e->value);
298
+		}
299
+	}
300
+}
301
+
302
+void sca_hash_table_print(sca_hash_table *ht)
303
+{
304
+	unsigned int i;
305
+
306
+	for (i = 0; i < ht->size; i++) {
307
+		LM_DBG("SLOT %d:\n", i);
308
+		sca_hash_slot_print(&ht->slots[i]);
309
+	}
310
+}
311
+
312
+void sca_hash_table_free(sca_hash_table *ht)
313
+{
314
+	sca_hash_entry *e, *e_tmp;
315
+	unsigned int i;
316
+
317
+	if (ht == NULL) {
318
+		return;
319
+	}
320
+
321
+	for (i = 0; i < ht->size; i++) {
322
+		if (ht->slots[i].entries == NULL) {
323
+			continue;
324
+		}
325
+
326
+		sca_hash_table_lock_index(ht, i);
327
+
328
+		for (e = ht->slots[i].entries; e != NULL; e = e_tmp) {
329
+			e_tmp = e->next;
330
+
331
+			e->free_entry(e->value);
332
+
333
+			shm_free(e);
334
+		}
335
+
336
+		sca_hash_table_unlock_index(ht, i);
337
+
338
+		lock_destroy(&ht->slots[i].lock);
339
+		lock_dealloc(&ht->slots[i].lock);
340
+	}
341
+
342
+	shm_free(ht->slots);
343
+	shm_free(ht);
344
+}