MCPcopy Create free account
hub / github.com/OpenSIPS/opensips / init_rl_table

Function init_rl_table

modules/ratelimit/ratelimit_helper.c:212–275  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

210}
211
212int init_rl_table(unsigned int size)
213{
214 unsigned int i;
215
216 rl_htable.maps = shm_malloc(sizeof(map_t) * size);
217 if (!rl_htable.maps) {
218 LM_ERR("no more shm memory\n");
219 return -1;
220 }
221
222 memset(rl_htable.maps, 0, sizeof(map_t) * size);
223 for (i = 0; i < size; i++) {
224 rl_htable.maps[i] = map_create(AVLMAP_SHARED);
225 if (!rl_htable.maps[i]) {
226 LM_ERR("cannot create map index %d\n", i);
227 goto error;
228 }
229 rl_htable.size++;
230 }
231
232 if (!rl_default_algo_s.s) {
233 LM_ERR("Default algorithm was not specified\n");
234 return -1;
235 }
236 rl_default_algo_s.len = strlen(rl_default_algo_s.s);
237 /* resolve the default algorithm */
238 rl_default_algo = get_rl_algo(rl_default_algo_s);
239 if (rl_default_algo == PIPE_ALGO_NOP) {
240 LM_ERR("unknown algorithm <%.*s>\n", rl_default_algo_s.len,
241 rl_default_algo_s.s);
242 return -1;
243 }
244 LM_DBG("default algorithm is %.*s [ %d ]\n",
245 rl_default_algo_s.len, rl_default_algo_s.s, rl_default_algo);
246
247 /* if at least 25% of the size locks can't be allocated
248 * we return an error */
249 for (i = size; i > size / 4; i--) {
250 rl_htable.locks = lock_set_alloc(i);
251 if (!rl_htable.locks)
252 continue;
253 if (!lock_set_init(rl_htable.locks)) {
254 lock_set_dealloc(rl_htable.locks);
255 rl_htable.locks = 0;
256 continue;
257 }
258 break;
259 }
260
261 if (!rl_htable.locks) {
262 LM_ERR("unable to allocted at least %d locks for the hash table\n",
263 size / 4);
264 goto error;
265 }
266 rl_htable.locks_no = i;
267
268 LM_DBG("%d locks allocated for %d hashsize\n",
269 rl_htable.locks_no, rl_htable.size);

Callers 1

mod_initFunction · 0.85

Calls 6

shm_mallocFunction · 0.85
map_createFunction · 0.85
get_rl_algoFunction · 0.85
lock_set_allocFunction · 0.85
lock_set_initFunction · 0.85
mod_destroyFunction · 0.70

Tested by

no test coverage detected