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
|
/*
* $Id$
*
* Copyright (C) 2005-2006 iptelorg GmbH
*
* This file is part of ser, a free SIP server.
*
* ser 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
*
* For a license to use the ser software under conditions
* other than those described here, or to purchase support for this
* software, please contact iptel.org by e-mail at the following addresses:
* info@iptel.org
*
* ser 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
*
* History:
* --------
* 2006-06-16 static buffer for select results (mma)
* each process owns a separate space
* each request starts using the buffer from the start
*
*/
/*!
* \file
* \brief SIP-router core ::
* \ingroup core
* Module: \ref core
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "dprint.h"
#include "mem/mem.h"
#include "str.h"
#include "ut.h"
/*
* Placeholder for the buffer
*
* two buffers are actually used to cover the different size requests
* assuming that resize can move the result to newly allocated space
* and comparing two selects from the script could require two static buffers
*
* if more static buffers need to be valid at the same time change
* the following constant
*/
#define MAX_BUFFERS 2
#define BUFFER_GRANULARITY 256
typedef struct stat_buffer_ {
char *b;
int size;
int offset;
} stat_buffer_t;
static stat_buffer_t buffer[MAX_BUFFERS];
static int active_buffer=-1;
#define ALLOC_SIZE(req_size) (((req_size/BUFFER_GRANULARITY)+1)*BUFFER_GRANULARITY)
static int allocate_buffer(int req_size) {
void *b;
int size=ALLOC_SIZE(req_size);
if (buffer[active_buffer].b == NULL) {
if ((buffer[active_buffer].b=pkg_malloc(size))==NULL)
return 0;
buffer[active_buffer].size=size;
buffer[active_buffer].offset=0;
return 1;
}
active_buffer = (active_buffer?active_buffer:MAX_BUFFERS)-1;
if (buffer[active_buffer].size >= req_size) {
buffer[active_buffer].offset = 0;
return 1;
}
if ((b=pkg_realloc(buffer[active_buffer].b,size))) {
buffer[active_buffer].b=b;
buffer[active_buffer].size=size;
buffer[active_buffer].offset=0;
return 1;
}
return 0;
}
/*
* Request for space from buffer
*
* Returns: NULL memory allocation failure (no more space)
* pointer to the space on success
*/
char* get_static_buffer(int req_size) {
char *p = NULL;
#ifdef EXTRA_DEBUG
if ((active_buffer < 0) || (active_buffer > MAX_BUFFERS-1)) {
LM_CRIT("buffers have not been initialized yet. "
"Call reset_static_buffer() before executing "
"a route block.\n");
abort();
}
#endif
if ((buffer[active_buffer].size >= buffer[active_buffer].offset + req_size)
|| (allocate_buffer(req_size))) {
/* enough space in current buffer or allocation successful */
p = buffer[active_buffer].b+buffer[active_buffer].offset;
buffer[active_buffer].offset += req_size;
return p;
}
return NULL;
}
/* Internal function - called before request is going to be processed
*
* Reset offset to unused space
*/
int reset_static_buffer(void) {
int i;
if (active_buffer == -1) {
memset(buffer, 0, sizeof(buffer));
} else {
for (i=0; i<MAX_BUFFERS; i++)
buffer[i].offset=0;
}
active_buffer=0;
return 0;
}
int str_to_static_buffer(str* res, str* s)
{
res->s = get_static_buffer(s->len);
if (!res->s) return -1;
memcpy(res->s, s->s, s->len);
res->len = s->len;
return 0;
}
int int_to_static_buffer(str* res, int val)
{
char *c;
c = int2str(abs(val), &res->len);
res->s = get_static_buffer(res->len+((val<0)?1:0));
if (!res->s) return -1;
if (val < 0) {
res->s[0] = '-';
memcpy(res->s+1, c, res->len);
res->len++;
}
else {
memcpy(res->s, c, res->len);
}
return 0;
}
int uint_to_static_buffer(str* res, unsigned int val)
{
char *c;
c = int2str(val, &res->len);
res->s = get_static_buffer(res->len);
if (!res->s) return -1;
memcpy(res->s, c, res->len);
return 0;
}
int uint_to_static_buffer_ex(str* res, unsigned int val, int base, int pad)
{
char *c;
c = int2str_base_0pad(val, &res->len, base, pad);
res->s = get_static_buffer(res->len);
if (!res->s) return -1;
memcpy(res->s, c, res->len);
return 0;
}
|