/src/opensips/cachedb/cachedb_pool.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2011 OpenSIPS Solutions |
3 | | * |
4 | | * This file is part of opensips, a free SIP server. |
5 | | * |
6 | | * opensips 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 | | * opensips 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 | | * |
21 | | * history: |
22 | | * --------- |
23 | | * 2011-09-xx created (vlad-paiu) |
24 | | */ |
25 | | |
26 | | |
27 | | #include "../dprint.h" |
28 | | #include "../mem/mem.h" |
29 | | #include "cachedb_pool.h" |
30 | | #include <string.h> |
31 | | |
32 | | static cachedb_pool_con *cachedb_pool = NULL; |
33 | | |
34 | | cachedb_pool_con* cachedb_pool_get(struct cachedb_id *id) |
35 | 0 | { |
36 | 0 | cachedb_pool_con *it; |
37 | |
|
38 | 0 | for (it=cachedb_pool;it;it=it->next) |
39 | 0 | if (cmp_cachedb_id(id,it->id)) { |
40 | 0 | it->ref++; |
41 | 0 | return it; |
42 | 0 | } |
43 | | |
44 | 0 | return 0; |
45 | 0 | } |
46 | | |
47 | | cachedb_pool_con** filter_pool_by_scheme(str *scheme,int* lst_size) |
48 | 0 | { |
49 | 0 | cachedb_pool_con *it; |
50 | 0 | cachedb_pool_con **lst=NULL; |
51 | 0 | int size = 0; |
52 | 0 | int alloc_size = 0; |
53 | | |
54 | 0 | for (it=cachedb_pool;it;it=it->next) { |
55 | 0 | if (memcmp(scheme->s,it->id->scheme,scheme->len) == 0) { |
56 | 0 | if (alloc_size - size == 0) { |
57 | 0 | alloc_size=(alloc_size==0)?2:2*alloc_size; |
58 | 0 | lst = pkg_realloc(lst,alloc_size * sizeof(cachedb_pool_con*)); |
59 | 0 | if (lst == NULL) { |
60 | 0 | LM_ERR("No more pkg \n"); |
61 | 0 | *lst_size = 0; |
62 | 0 | return NULL; |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | 0 | lst[size]=it; |
67 | 0 | size++; |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | 0 | *lst_size = size; |
72 | 0 | return lst; |
73 | 0 | } |
74 | | |
75 | | void cachedb_pool_insert(cachedb_pool_con *con) |
76 | 0 | { |
77 | 0 | if (!con) |
78 | 0 | return; |
79 | | |
80 | 0 | con->next = cachedb_pool; |
81 | 0 | cachedb_pool = con; |
82 | 0 | } |
83 | | |
84 | | int cachedb_pool_remove(cachedb_pool_con *con) |
85 | 0 | { |
86 | 0 | cachedb_pool_con *it; |
87 | |
|
88 | 0 | if (!con) |
89 | 0 | return -2; |
90 | | |
91 | 0 | if (con->ref > 1) { |
92 | 0 | con->ref--; |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | 0 | if (cachedb_pool == con) { |
97 | 0 | cachedb_pool = cachedb_pool->next; |
98 | 0 | } else { |
99 | 0 | it = cachedb_pool; |
100 | 0 | while (it) { |
101 | 0 | if (it->next == con) |
102 | 0 | break; |
103 | 0 | it = it->next; |
104 | 0 | } |
105 | |
|
106 | 0 | if (!it) { |
107 | 0 | LM_ERR("BUG - conn not found in pool\n"); |
108 | 0 | return -1; |
109 | 0 | } |
110 | | |
111 | 0 | it->next = con->next; |
112 | 0 | } |
113 | | |
114 | 0 | return 1; |
115 | 0 | } |