select_buf.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include <ctype.h>
  39. #include "dprint.h"
  40. #include "mem/mem.h"
  41. #include "str.h"
  42. #include "ut.h"
  43. /*
  44. * Placeholder for the buffer
  45. *
  46. * two buffers are actually used to cover the different size requests
  47. * assuming that resize can move the result to newly allocated space
  48. * and comparing two selects from the script could require two static buffers
  49. *
  50. * if more static buffers need to be valid at the same time change
  51. * the following constant
  52. */
  53. #define MAX_BUFFERS 2
  54. #define BUFFER_GRANULARITY 256
  55. typedef struct stat_buffer_ {
  56. char *b;
  57. int size;
  58. int offset;
  59. } stat_buffer_t;
  60. static stat_buffer_t buffer[MAX_BUFFERS];
  61. static int active_buffer=-1;
  62. #define ALLOC_SIZE(req_size) (((req_size/BUFFER_GRANULARITY)+1)*BUFFER_GRANULARITY)
  63. static int allocate_buffer(int req_size) {
  64. void *b;
  65. int size=ALLOC_SIZE(req_size);
  66. if (buffer[active_buffer].b == NULL) {
  67. if ((buffer[active_buffer].b=pkg_malloc(size))==NULL)
  68. return 0;
  69. buffer[active_buffer].size=size;
  70. buffer[active_buffer].offset=0;
  71. return 1;
  72. }
  73. active_buffer = (active_buffer?active_buffer:MAX_BUFFERS)-1;
  74. if (buffer[active_buffer].size >= req_size) {
  75. buffer[active_buffer].offset = 0;
  76. return 1;
  77. }
  78. if ((b=pkg_realloc(buffer[active_buffer].b,size))) {
  79. buffer[active_buffer].b=b;
  80. buffer[active_buffer].size=size;
  81. buffer[active_buffer].offset=0;
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. /*
  87. * Request for space from buffer
  88. *
  89. * Returns: NULL memory allocation failure (no more space)
  90. * pointer to the space on success
  91. */
  92. char* get_static_buffer(int req_size) {
  93. char *p = NULL;
  94. if ((buffer[active_buffer].size >= buffer[active_buffer].offset + req_size)
  95. || (allocate_buffer(req_size))) {
  96. /* enough space in current buffer or allocation successful */
  97. p = buffer[active_buffer].b+buffer[active_buffer].offset;
  98. buffer[active_buffer].offset += req_size;
  99. return p;
  100. }
  101. return NULL;
  102. }
  103. /* Internal function - called before request is going to be processed
  104. *
  105. * Reset offset to unused space
  106. */
  107. int reset_static_buffer() {
  108. int i;
  109. if (active_buffer == -1) {
  110. memset(buffer, 0, sizeof(buffer));
  111. } else {
  112. for (i=0; i<MAX_BUFFERS; i++)
  113. buffer[i].offset=0;
  114. }
  115. active_buffer=0;
  116. return 0;
  117. }
  118. int str_to_static_buffer(str* res, str* s)
  119. {
  120. res->s = get_static_buffer(s->len);
  121. if (!res->s) return -1;
  122. memcpy(res->s, s->s, s->len);
  123. res->len = s->len;
  124. return 0;
  125. }
  126. int int_to_static_buffer(str* res, int val)
  127. {
  128. char *c;
  129. c = int2str(abs(val), &res->len);
  130. res->s = get_static_buffer(res->len+(val<0)?1:0);
  131. if (!res->s) return -1;
  132. if (val < 0) {
  133. res->s[0] = '-';
  134. memcpy(res->s+1, c, res->len);
  135. res->len++;
  136. }
  137. else {
  138. memcpy(res->s, c, res->len);
  139. }
  140. return 0;
  141. }
  142. int uint_to_static_buffer(str* res, unsigned int val)
  143. {
  144. char *c;
  145. c = int2str(val, &res->len);
  146. res->s = get_static_buffer(res->len);
  147. if (!res->s) return -1;
  148. memcpy(res->s, c, res->len);
  149. return 0;
  150. }
  151. int uint_to_static_buffer_ex(str* res, unsigned int val, int base, int pad)
  152. {
  153. char *c;
  154. c = int2str_base_0pad(val, &res->len, base, pad);
  155. res->s = get_static_buffer(res->len);
  156. if (!res->s) return -1;
  157. memcpy(res->s, c, res->len);
  158. return 0;
  159. }