1 /*
2 * Copyright (C) 2005-2006 iptelorg GmbH
3 *
4 * This file is part of Kamailio, a free SIP server.
5 *
6 * Kamailio 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 * Kamailio 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
22 /*!
23 * \file
24 * \brief Kamailio core :: static buffer for select results (mma)
25 * each process owns a separate space
26 * each request starts using the buffer from the start
27 * \ingroup core
28 * Module: \ref core
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <ctype.h>
36
37 #include "dprint.h"
38 #include "mem/mem.h"
39 #include "str.h"
40 #include "ut.h"
41
42 /*
43 * Placeholder for the buffer
44 *
45 * two buffers are actually used to cover the different size requests
46 * assuming that resize can move the result to newly allocated space
47 * and comparing two selects from the script could require two static buffers
48 *
49 * if more static buffers need to be valid at the same time change
50 * the following constant
51 */
52
53 #define MAX_BUFFERS 2
54 #define BUFFER_GRANULARITY 256
55
56 typedef struct stat_buffer_ {
57 char *b;
58 int size;
59 int offset;
60 } stat_buffer_t;
61
62 static stat_buffer_t buffer[MAX_BUFFERS];
63 static int active_buffer=-1;
64
65 #define ALLOC_SIZE(req_size) (((req_size/BUFFER_GRANULARITY)+1)*BUFFER_GRANULARITY)
66
allocate_buffer(int req_size)67 static int allocate_buffer(int req_size) {
68 void *b;
69 int size=ALLOC_SIZE(req_size);
70
71 if (buffer[active_buffer].b == NULL) {
72 if ((buffer[active_buffer].b=pkg_malloc(size))==NULL) {
73 PKG_MEM_ERROR;
74 return 0;
75 }
76 buffer[active_buffer].size=size;
77 buffer[active_buffer].offset=0;
78 return 1;
79 }
80
81 active_buffer = (active_buffer?active_buffer:MAX_BUFFERS)-1;
82 if (buffer[active_buffer].size >= req_size) {
83 buffer[active_buffer].offset = 0;
84 return 1;
85 }
86
87 if ((b=pkg_realloc(buffer[active_buffer].b,size))) {
88 buffer[active_buffer].b=b;
89 buffer[active_buffer].size=size;
90 buffer[active_buffer].offset=0;
91 return 1;
92 }
93
94 return 0;
95 }
96
97 /*
98 * Request for space from buffer
99 *
100 * Returns: NULL memory allocation failure (no more space)
101 * pointer to the space on success
102 */
103
get_static_buffer(int req_size)104 char* get_static_buffer(int req_size) {
105 char *p = NULL;
106
107 #ifdef EXTRA_DEBUG
108 if ((active_buffer < 0) || (active_buffer > MAX_BUFFERS-1)) {
109 LM_CRIT("buffers have not been initialized yet. "
110 "Call reset_static_buffer() before executing "
111 "a route block.\n");
112 abort();
113 }
114 #endif
115 if ((buffer[active_buffer].size >= buffer[active_buffer].offset + req_size)
116 || (allocate_buffer(req_size))) {
117 /* enough space in current buffer or allocation successful */
118 p = buffer[active_buffer].b+buffer[active_buffer].offset;
119 buffer[active_buffer].offset += req_size;
120 return p;
121 }
122 return NULL;
123 }
124
125 /* Internal function - called before request is going to be processed
126 *
127 * Reset offset to unused space
128 */
129
reset_static_buffer(void)130 int reset_static_buffer(void) {
131 int i;
132
133 if (active_buffer == -1) {
134 memset(buffer, 0, sizeof(buffer));
135 } else {
136 for (i=0; i<MAX_BUFFERS; i++)
137 buffer[i].offset=0;
138 }
139 active_buffer=0;
140 return 0;
141 }
142
str_to_static_buffer(str * res,str * s)143 int str_to_static_buffer(str* res, str* s)
144 {
145 res->s = get_static_buffer(s->len);
146 if (!res->s) return -1;
147 memcpy(res->s, s->s, s->len);
148 res->len = s->len;
149 return 0;
150 }
151
int_to_static_buffer(str * res,int val)152 int int_to_static_buffer(str* res, int val)
153 {
154 char *c;
155 c = int2str(abs(val), &res->len);
156 res->s = get_static_buffer(res->len+((val<0)?1:0));
157 if (!res->s) return -1;
158 if (val < 0) {
159 res->s[0] = '-';
160 memcpy(res->s+1, c, res->len);
161 res->len++;
162 }
163 else {
164 memcpy(res->s, c, res->len);
165 }
166 return 0;
167 }
168
uint_to_static_buffer(str * res,unsigned int val)169 int uint_to_static_buffer(str* res, unsigned int val)
170 {
171 char *c;
172 c = int2str(val, &res->len);
173 res->s = get_static_buffer(res->len);
174 if (!res->s) return -1;
175 memcpy(res->s, c, res->len);
176 return 0;
177 }
178
uint_to_static_buffer_ex(str * res,unsigned int val,int base,int pad)179 int uint_to_static_buffer_ex(str* res, unsigned int val, int base, int pad)
180 {
181 char *c;
182 c = int2str_base_0pad(val, &res->len, base, pad);
183 res->s = get_static_buffer(res->len);
184 if (!res->s) return -1;
185 memcpy(res->s, c, res->len);
186 return 0;
187 }
188
189