Kannel: Open Source WAP and SMS gateway  svn-r5335
wsfalloc.c File Reference
#include "wsint.h"

Go to the source code of this file.

Functions

WsFastMallocws_f_create (size_t block_size)
 
void ws_f_destroy (WsFastMalloc *pool)
 
void * ws_f_malloc (WsFastMalloc *pool, size_t size)
 
void * ws_f_calloc (WsFastMalloc *pool, size_t num, size_t size)
 
void * ws_f_memdup (WsFastMalloc *pool, const void *ptr, size_t size)
 
void * ws_f_strdup (WsFastMalloc *pool, const char *str)
 

Function Documentation

◆ ws_f_calloc()

void* ws_f_calloc ( WsFastMalloc pool,
size_t  num,
size_t  size 
)

Definition at line 150 of file wsfalloc.c.

References size, and ws_f_malloc().

Referenced by asm_alloc(), expr_alloc(), stmt_alloc(), ws_list_append(), ws_list_new(), and ws_stmt_linearize().

151 {
152  void *p = ws_f_malloc(pool, num * size);
153 
154  if (p == NULL)
155  return p;
156 
157  memset(p, 0, num * size);
158 
159  return p;
160 }
int size
Definition: wsasm.c:84
void * ws_f_malloc(WsFastMalloc *pool, size_t size)
Definition: wsfalloc.c:102

◆ ws_f_create()

WsFastMalloc* ws_f_create ( size_t  block_size)

Definition at line 74 of file wsfalloc.c.

References WsFastMallocRec::block_size, and ws_calloc().

Referenced by compile_stream().

75 {
76  WsFastMalloc *pool = ws_calloc(1, sizeof(WsFastMalloc));
77 
78  if (pool == NULL)
79  return NULL;
80 
81  pool->block_size = block_size;
82 
83  return pool;
84 }
size_t block_size
Definition: wsfalloc.h:88
void * ws_calloc(size_t num, size_t size)
Definition: wsalloc.c:83

◆ ws_f_destroy()

void ws_f_destroy ( WsFastMalloc pool)

Definition at line 87 of file wsfalloc.c.

References WsFastMallocRec::blocks, WsFastMallocBlockRec::next, and ws_free().

Referenced by compile_stream().

88 {
89  WsFastMallocBlock *b, *bnext;
90 
91  if (pool == NULL)
92  return;
93 
94  for (b = pool->blocks; b; b = bnext) {
95  bnext = b->next;
96  ws_free(b);
97  }
98  ws_free(pool);
99 }
void ws_free(void *ptr)
Definition: wsalloc.c:139
struct WsFastMallocBlockRec * next
Definition: wsfalloc.h:77
WsFastMallocBlock * blocks
Definition: wsfalloc.h:85

◆ ws_f_malloc()

void* ws_f_malloc ( WsFastMalloc pool,
size_t  size 
)

Definition at line 102 of file wsfalloc.c.

References WsFastMallocRec::block_size, WsFastMallocRec::blocks, WsFastMallocBlockRec::next, WsFastMallocRec::ptr, size, WsFastMallocRec::size, WsFastMallocRec::user_bytes_allocated, and ws_malloc().

Referenced by ws_f_calloc(), ws_f_memdup(), ws_f_strdup(), ws_formal_parameter(), and ws_variable_declaration().

103 {
104  unsigned char *result;
105 
106  /* Keep the blocks aligned, because this function is used to allocate
107  * space for structures containing longs and such. */
108 
109  if (size % sizeof(long) != 0) {
110  size += sizeof(long) - (size % sizeof(long));
111  }
112 
113  if (pool->size < size) {
114  size_t alloc_size;
116 
117  /* Must allocate a fresh block. */
118  alloc_size = pool->block_size;
119  if (alloc_size < size)
120  alloc_size = size;
121 
122  /* Allocate the block and remember to add the header size. */
123  b = ws_malloc(alloc_size + sizeof(WsFastMallocBlock));
124 
125  if (b == NULL)
126  /* No memory available. */
127  return NULL;
128 
129  /* Add this block to the memory pool. */
130  b->next = pool->blocks;
131  pool->blocks = b;
132 
133  pool->ptr = ((unsigned char *) b) + sizeof(WsFastMallocBlock);
134  pool->size = alloc_size;
135  }
136 
137  /* Now we can allocate `size' bytes of data from this pool. */
138 
139  result = pool->ptr;
140 
141  pool->ptr += size;
142  pool->size -= size;
143 
144  pool->user_bytes_allocated += size;
145 
146  return result;
147 }
size_t block_size
Definition: wsfalloc.h:88
int size
Definition: wsasm.c:84
size_t size
Definition: wsfalloc.h:97
struct WsFastMallocBlockRec WsFastMallocBlock
Definition: wsfalloc.h:81
unsigned char * ptr
Definition: wsfalloc.h:94
size_t user_bytes_allocated
Definition: wsfalloc.h:91
struct WsFastMallocBlockRec * next
Definition: wsfalloc.h:77
void * ws_malloc(size_t size)
Definition: wsalloc.c:77
WsFastMallocBlock * blocks
Definition: wsfalloc.h:85

◆ ws_f_memdup()

void* ws_f_memdup ( WsFastMalloc pool,
const void *  ptr,
size_t  size 
)

Definition at line 163 of file wsfalloc.c.

References size, and ws_f_malloc().

Referenced by ws_expr_const_string().

164 {
165  unsigned char *d = ws_f_malloc(pool, size + 1);
166 
167  if (d == NULL)
168  return NULL;
169 
170  memcpy(d, ptr, size);
171  d[size] = '\0';
172 
173  return d;
174 }
int size
Definition: wsasm.c:84
void * ws_f_malloc(WsFastMalloc *pool, size_t size)
Definition: wsfalloc.c:102

◆ ws_f_strdup()

void* ws_f_strdup ( WsFastMalloc pool,
const char *  str 
)

Definition at line 177 of file wsfalloc.c.

References WsBufferRec::len, and ws_f_malloc().

Referenced by ws_expr_assign(), ws_expr_call(), ws_expr_postfix_var(), ws_expr_symbol(), ws_expr_unary_var(), and yyparse().

178 {
179  size_t len;
180  char *s;
181 
182  if (str == NULL)
183  return NULL;
184 
185  len = strlen(str) + 1;
186  s = ws_f_malloc(pool, len);
187 
188  if (s == NULL)
189  return NULL;
190 
191  memcpy(s, str, len);
192 
193  return s;
194 }
void * ws_f_malloc(WsFastMalloc *pool, size_t size)
Definition: wsfalloc.c:102
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.