-
Notifications
You must be signed in to change notification settings - Fork 909
/
tree.c
417 lines (317 loc) · 7.13 KB
/
tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Prefix Route Module - tree search implementation
*
* Copyright (C) 2007 Alfred E. Heggestad
* Copyright (C) 2008 Telio Telecom AS
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*! \file
* \brief Prefix Route Module - tree search implementation
* \ingroup prefix_route
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "../../core/atomic_ops.h"
#include "../../core/mem/shm_mem.h"
#include "../../core/str.h"
#include "../../core/lock_alloc.h"
#include "../../core/lock_ops.h"
#include "tree.h"
enum {
DIGITS = 10
};
/** Defines a route item in the prefix tree */
struct tree_item {
struct tree_item *digits[DIGITS]; /**< Child items for each digit */
char name[16]; /**< Route name (for dump) */
int route; /**< Valid route number if >0 */
};
/** Defines a locked prefix tree */
struct tree {
struct tree_item *root; /**< Root item of tree */
atomic_t refcnt; /**< Reference counting */
};
/* Local variables */
static struct tree **shared_tree = NULL;
static gen_lock_t* shared_tree_lock;
/**
* Allocate and initialize a new tree item
*/
struct tree_item *tree_item_alloc(void)
{
struct tree_item *root;
int i;
root = (struct tree_item *)shm_malloc(sizeof(*root));
if (NULL == root) {
SHM_MEM_CRITICAL;
return NULL;
}
for (i=0; i<DIGITS; i++)
root->digits[i] = NULL;
root->route = 0;
return root;
}
/**
* Flush tree item
*/
void tree_item_free(struct tree_item *item)
{
int i;
if (NULL == item)
return;
for (i=0; i<DIGITS; i++) {
tree_item_free(item->digits[i]);
}
shm_free(item);
}
/**
* Add a route prefix rule to the tree
*/
int tree_item_add(struct tree_item *root, const char *prefix,
const char *route, int route_ix)
{
struct tree_item *item;
const char *p;
int err;
if (NULL == root || NULL == prefix || route_ix <= 0)
return -1;
item = root;
for (p = prefix; '\0' != *p; p++) {
int digit;
if (!isdigit(*p))
continue;
digit = *p - '0';
/* exist? */
if (!item->digits[digit]) {
item->digits[digit] = tree_item_alloc();
if (!item->digits[digit]) {
LM_CRIT("alloc failed\n");
err = -1;
goto out;
}
}
item = item->digits[digit];
}
if (NULL == item) {
LM_CRIT("internal error (no item)\n");
err = -1;
goto out;
}
if (item->route > 0) {
LM_ERR("prefix %s already set to %s\n", prefix, item->name);
}
/* Set route number for the tree item */
item->route = route_ix;
/* Copy the route name (used in tree dump) */
strncpy(item->name, route, sizeof(item->name)-1);
item->name[sizeof(item->name)-1] = '\0';
err = 0;
out:
return err;
}
/**
* Get route number from username
*/
int tree_item_get(const struct tree_item *root, const str *user)
{
const struct tree_item *item;
const char *p, *pmax;
int route = 0;
if (NULL == root || NULL == user || NULL == user->s || !user->len)
return -1;
pmax = user->s + user->len;
item = root;
for (p = user->s; p < pmax ; p++) {
int digit;
if (!isdigit(*p)) {
continue;
}
digit = *p - '0';
/* Update route with best match so far */
if (item->route > 0) {
route = item->route;
}
/* exist? */
if (NULL == item->digits[digit]) {
break;
}
item = item->digits[digit];
}
return route;
}
/**
* Print one tree item to a file handle
*/
void tree_item_print(const struct tree_item *item, FILE *f, int level)
{
int i;
if (NULL == item || NULL == f)
return;
if (item->route > 0) {
fprintf(f, " \t--> route[%s] ", item->name);
}
for (i=0; i<DIGITS; i++) {
int j;
if (!item->digits[i]) {
continue;
}
fputc('\n', f);
for (j=0; j<level; j++)
fputc(' ', f);
fprintf(f, "%d ", i);
tree_item_print(item->digits[i], f, level+1);
}
}
/**
* Allocate a new tree structure
*/
static struct tree *tree_alloc(void)
{
struct tree *tree;
tree = (struct tree *)shm_malloc(sizeof(*tree));
if (NULL == tree) {
SHM_MEM_CRITICAL;
return NULL;
}
tree->root = NULL;
atomic_set(&tree->refcnt, 0);
return tree;
}
/**
* Flush the tree
*/
static void tree_flush(struct tree *tree)
{
if (NULL == tree)
return;
/* Wait for old tree to be released */
for (;;) {
const int refcnt = atomic_get(&tree->refcnt);
if (refcnt <= 0)
break;
LM_NOTICE("waiting refcnt=%d\n", refcnt);
usleep(100000);
};
tree_item_free(tree->root);
shm_free(tree);
}
/**
* Access the shared tree and optionally increment/decrement the
* reference count.
*/
static struct tree *tree_get(void)
{
struct tree *tree;
lock_get(shared_tree_lock);
tree = *shared_tree;
lock_release(shared_tree_lock);
return tree;
}
static struct tree *tree_ref(void)
{
struct tree *tree;
lock_get(shared_tree_lock);
tree = *shared_tree;
atomic_inc(&tree->refcnt);
lock_release(shared_tree_lock);
return tree;
}
struct tree *tree_deref(struct tree *tree)
{
if (tree)
atomic_dec(&tree->refcnt);
return tree;
}
int tree_init(void)
{
/* Initialize lock */
shared_tree_lock = lock_alloc();
if (NULL == shared_tree_lock) {
return -1;
}
lock_init(shared_tree_lock);
/* Pointer to global tree must be in shared memory */
shared_tree = (struct tree **)shm_malloc(sizeof(*shared_tree));
if (NULL == shared_tree) {
SHM_MEM_ERROR;
lock_destroy(shared_tree_lock);
lock_dealloc(shared_tree_lock);
shared_tree_lock=0;
return -1;
}
*shared_tree = NULL;
return 0;
}
void tree_close(void)
{
if (shared_tree)
tree_flush(tree_get());
shared_tree = NULL;
if (shared_tree_lock) {
lock_destroy(shared_tree_lock);
lock_dealloc(shared_tree_lock);
shared_tree_lock=0;
}
}
int tree_swap(struct tree_item *root)
{
struct tree *new_tree, *old_tree;
new_tree = tree_alloc();
if (NULL == new_tree)
return -1;
new_tree->root = root;
/* Save old tree */
old_tree = tree_get();
/* Critical - swap trees */
lock_get(shared_tree_lock);
*shared_tree = new_tree;
lock_release(shared_tree_lock);
/* Flush old tree */
tree_flush(old_tree);
return 0;
}
int tree_route_get(const str *user)
{
struct tree *tree;
int route;
/* Find match in tree */
tree = tree_ref();
if (NULL == tree) {
return -1;
}
route = tree_item_get(tree->root, user);
tree_deref(tree);
return route;
}
void tree_print(FILE *f)
{
struct tree *tree;
tree = tree_ref();
fprintf(f, "Prefix route tree:\n");
if (tree) {
fprintf(f, " reference count: %d\n",
atomic_get(&tree->refcnt));
tree_item_print(tree->root, f, 0);
}
else {
fprintf(f, " (no tree)\n");
}
tree_deref(tree);
}