Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wsfalloc.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  *
59  * wsfalloc.c
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  * All rights reserved.
65  *
66  * Fast memory allocation routines.
67  *
68  */
69 
70 #include "wsint.h"
71 
72 /********************* Global functions *********************************/
73 
74 WsFastMalloc *ws_f_create(size_t block_size)
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 }
85 
86 
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 }
100 
101 
102 void *ws_f_malloc(WsFastMalloc *pool, size_t size)
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 }
148 
149 
150 void *ws_f_calloc(WsFastMalloc *pool, size_t num, size_t size)
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 }
161 
162 
163 void *ws_f_memdup(WsFastMalloc *pool, const void *ptr, size_t size)
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 }
175 
176 
177 void *ws_f_strdup(WsFastMalloc *pool, const char *str)
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 }
size_t block_size
Definition: wsfalloc.h:88
void * ws_calloc(size_t num, size_t size)
Definition: wsalloc.c:83
int size
Definition: wsasm.c:84
WsFastMalloc * ws_f_create(size_t block_size)
Definition: wsfalloc.c:74
void ws_free(void *ptr)
Definition: wsalloc.c:139
void ws_f_destroy(WsFastMalloc *pool)
Definition: wsfalloc.c:87
size_t size
Definition: wsfalloc.h:97
void * ws_f_memdup(WsFastMalloc *pool, const void *ptr, size_t size)
Definition: wsfalloc.c:163
size_t len
Definition: wsbuffer.h:79
struct WsFastMallocBlockRec WsFastMallocBlock
Definition: wsfalloc.h:81
unsigned char * ptr
Definition: wsfalloc.h:94
size_t user_bytes_allocated
Definition: wsfalloc.h:91
void * ws_f_strdup(WsFastMalloc *pool, const char *str)
Definition: wsfalloc.c:177
struct WsFastMallocBlockRec * next
Definition: wsfalloc.h:77
void * ws_f_calloc(WsFastMalloc *pool, size_t num, size_t size)
Definition: wsfalloc.c:150
void * ws_f_malloc(WsFastMalloc *pool, size_t size)
Definition: wsfalloc.c:102
void * ws_malloc(size_t size)
Definition: wsalloc.c:77
WsFastMallocBlock * blocks
Definition: wsfalloc.h:85
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.