select_buf.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005-2006 iptelorg GmbH
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * History:
  28. * --------
  29. * 2006-06-16 static buffer for select results (mma)
  30. * each process owns a separate space
  31. * each request starts using the buffer from the start
  32. *
  33. */
  34. /*!
  35. * \file
  36. * \brief SIP-router core ::
  37. * \ingroup core
  38. * Module: \ref core
  39. */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include <ctype.h>
  45. #include "dprint.h"
  46. #include "mem/mem.h"
  47. #include "str.h"
  48. #include "ut.h"
  49. /*
  50. * Placeholder for the buffer
  51. *
  52. * two buffers are actually used to cover the different size requests
  53. * assuming that resize can move the result to newly allocated space
  54. * and comparing two selects from the script could require two static buffers
  55. *
  56. * if more static buffers need to be valid at the same time change
  57. * the following constant
  58. */
  59. #define MAX_BUFFERS 2
  60. #define BUFFER_GRANULARITY 256
  61. typedef struct stat_buffer_ {
  62. char *b;
  63. int size;
  64. int offset;
  65. } stat_buffer_t;
  66. static stat_buffer_t buffer[MAX_BUFFERS];
  67. static int active_buffer=-1;
  68. #define ALLOC_SIZE(req_size) (((req_size/BUFFER_GRANULARITY)+1)*BUFFER_GRANULARITY)
  69. static int allocate_buffer(int req_size) {
  70. void *b;
  71. int size=ALLOC_SIZE(req_size);
  72. if (buffer[active_buffer].b == NULL) {
  73. if ((buffer[active_buffer].b=pkg_malloc(size))==NULL)
  74. return 0;
  75. buffer[active_buffer].size=size;
  76. buffer[active_buffer].offset=0;
  77. return 1;
  78. }
  79. active_buffer = (active_buffer?active_buffer:MAX_BUFFERS)-1;
  80. if (buffer[active_buffer].size >= req_size) {
  81. buffer[active_buffer].offset = 0;
  82. return 1;
  83. }
  84. if ((b=pkg_realloc(buffer[active_buffer].b,size))) {
  85. buffer[active_buffer].b=b;
  86. buffer[active_buffer].size=size;
  87. buffer[active_buffer].offset=0;
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. /*
  93. * Request for space from buffer
  94. *
  95. * Returns: NULL memory allocation failure (no more space)
  96. * pointer to the space on success
  97. */
  98. char* get_static_buffer(int req_size) {
  99. char *p = NULL;
  100. #ifdef EXTRA_DEBUG
  101. if ((active_buffer < 0) || (active_buffer > MAX_BUFFERS-1)) {
  102. LM_CRIT("buffers have not been initialized yet. "
  103. "Call reset_static_buffer() before executing "
  104. "a route block.\n");
  105. abort();
  106. }
  107. #endif
  108. if ((buffer[active_buffer].size >= buffer[active_buffer].offset + req_size)
  109. || (allocate_buffer(req_size))) {
  110. /* enough space in current buffer or allocation successful */
  111. p = buffer[active_buffer].b+buffer[active_buffer].offset;
  112. buffer[active_buffer].offset += req_size;
  113. return p;
  114. }
  115. return NULL;
  116. }
  117. /* Internal function - called before request is going to be processed
  118. *
  119. * Reset offset to unused space
  120. */
  121. int reset_static_buffer(void) {
  122. int i;
  123. if (active_buffer == -1) {
  124. memset(buffer, 0, sizeof(buffer));
  125. } else {
  126. for (i=0; i<MAX_BUFFERS; i++)
  127. buffer[i].offset=0;
  128. }
  129. active_buffer=0;
  130. return 0;
  131. }
  132. int str_to_static_buffer(str* res, str* s)
  133. {
  134. res->s = get_static_buffer(s->len);
  135. if (!res->s) return -1;
  136. memcpy(res->s, s->s, s->len);
  137. res->len = s->len;
  138. return 0;
  139. }
  140. int int_to_static_buffer(str* res, int val)
  141. {
  142. char *c;
  143. c = int2str(abs(val), &res->len);
  144. res->s = get_static_buffer(res->len+((val<0)?1:0));
  145. if (!res->s) return -1;
  146. if (val < 0) {
  147. res->s[0] = '-';
  148. memcpy(res->s+1, c, res->len);
  149. res->len++;
  150. }
  151. else {
  152. memcpy(res->s, c, res->len);
  153. }
  154. return 0;
  155. }
  156. int uint_to_static_buffer(str* res, unsigned int val)
  157. {
  158. char *c;
  159. c = int2str(val, &res->len);
  160. res->s = get_static_buffer(res->len);
  161. if (!res->s) return -1;
  162. memcpy(res->s, c, res->len);
  163. return 0;
  164. }
  165. int uint_to_static_buffer_ex(str* res, unsigned int val, int base, int pad)
  166. {
  167. char *c;
  168. c = int2str_base_0pad(val, &res->len, base, pad);
  169. res->s = get_static_buffer(res->len);
  170. if (!res->s) return -1;
  171. memcpy(res->s, c, res->len);
  172. return 0;
  173. }