Debian Codegraph @

Xpjproject/pjlib/src/pj/pool.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/pool.h>
20 #include <pj/log.h>
21 #include <pj/except.h>
22 #include <pj/assert.h>
23 #include <pj/os.h>
24
25 #if !PJ_HAS_POOL_ALT_API
26
27
28 /* Include inline definitions when inlining is disabled. */
29 #if !PJ_FUNCTIONS_ARE_INLINED
30 # include <pj/pool_i.h>
31 #endif
32
33 #define LOG(expr) PJ_LOG(6,expr)
34
35 PJ_DEF_DATA(int) PJ_NO_MEMORY_EXCEPTION;
36
37 PJ_DEF(int) pj_NO_MEMORY_EXCEPTION()
38 {
39 return PJ_NO_MEMORY_EXCEPTION;
40 }
41
42 /*
43 * Create new block.
44 * Create a new big chunk of memory block, from which user allocation will be
45 * taken from.
46 */
47 static pj_pool_block *pj_pool_create_block( pj_pool_t *pool, pj_size_t size)
48 {
49 pj_pool_block *block;
50
51 PJ_CHECK_STACK();
52 pj_assert(size >= sizeof(pj_pool_block));
53
54 LOG((pool->obj_name, "create_block(sz=%lu), cur.cap=%lu, cur.used=%lu",
55 (unsigned long)size, (unsigned long)pool->capacity,
56 (unsigned long)pj_pool_get_used_size(pool)));
57
58 /* Request memory from allocator. */
59 block = (pj_pool_block*)
60 (*pool->factory->policy.block_alloc)(pool->factory, size);
61 if (block == NULL) {
62 (*pool->callback)(pool, size);
63 return NULL;
64 }
65
66 /* Add capacity. */
67 pool->capacity += size;
68
69 /* Set start and end of buffer. */
70 block->buf = ((unsigned char*)block) + sizeof(pj_pool_block);
71 block->end = ((unsigned char*)block) + size;
72
73 /* Set the start pointer, unaligned! */
74 block->cur = block->buf;
75
76 /* Insert in the front of the list. */
77 pj_list_insert_after(&pool->block_list, block);
78
79 LOG((pool->obj_name," block created, buffer=%p-%p",block->buf, block->end));
80
81 return block;
82 }
83
84 /*
85 * Allocate memory chunk for user from available blocks.
86 * This will iterate through block list to find space to allocate the chunk.
87 * If no space is available in all the blocks (or
88 * PJ_POOL_MAX_SEARCH_BLOCK_COUNT blocks if the config is > 0),
89 * a new block might be created (depending on whether the pool is allowed
90 * to resize).
91 */
92 PJ_DEF(void*) pj_pool_allocate_find(pj_pool_t *pool, pj_size_t alignment,
93 pj_size_t size)
94 {
95 pj_pool_block *block = pool->block_list.next;
96 void *p;
97 pj_size_t block_size;
98 unsigned i = 0;
99
100 PJ_CHECK_STACK();
101 pj_assert(PJ_IS_POWER_OF_TWO(alignment));
102
103 while (block != &pool->block_list) {
104 p = pj_pool_alloc_from_block(block, alignment, size);
105 if (p != NULL)
106 return p;
107
108 #if PJ_POOL_MAX_SEARCH_BLOCK_COUNT > 0
109 if (i >= PJ_POOL_MAX_SEARCH_BLOCK_COUNT) {
110 break;
111 }
112 #endif
113
114 i++;
115 block = block->next;
116 }
117 /* No available space in all blocks. */
118
119 /* If pool is configured NOT to expand, return error. */
120 if (pool->increment_size == 0) {
121 LOG((pool->obj_name, "Can't expand pool to allocate %lu bytes "
122 "(used=%lu, cap=%lu)",
123 (unsigned long)size, (unsigned long)pj_pool_get_used_size(pool),
124 (unsigned long)pool->capacity));
125 (*pool->callback)(pool, size);
126 return NULL;
127 }
128
129 /* If pool is configured to expand, but the increment size
130 * is less than the required size, expand the pool by multiple
131 * increment size. Also count the size wasted due to aligning
132 * the block.
133 */
134 if (pool->increment_size <
135 sizeof(pj_pool_block)+ /*block header, itself may be unaligned*/
136 alignment-1 + /* gap [0:alignment-1] to align first allocation*/
137 size) /* allocation size (NOT aligned!) */
138 {
139 pj_size_t count;
140 count = (pool->increment_size +
141 sizeof(pj_pool_block) + alignment-1 + size) /
142 pool->increment_size;
143 block_size = count * pool->increment_size;
144 } else {
145 block_size = pool->increment_size;
146 }
147
148 LOG((pool->obj_name,
149 "%lu bytes requested, resizing pool by %lu bytes (used=%lu, cap=%lu)",
150 (unsigned long)size, (unsigned long)block_size,
151 (unsigned long)pj_pool_get_used_size(pool),
152 (unsigned long)pool->capacity));
153
154 block = pj_pool_create_block(pool, block_size);
155 if (!block)
156 return NULL;
157
158 p = pj_pool_alloc_from_block(block, alignment, size);
159 pj_assert(p != NULL);
160 #if PJ_DEBUG
161 if (p == NULL) {
162 PJ_UNUSED_ARG(p);
163 }
164 #endif
165 return p;
166 }
167
168 /*
169 * Internal function to initialize pool.
170 */
171 PJ_DEF(void) pj_pool_init_int(pj_pool_t *pool,
172 const char *name,
173 pj_size_t increment_size,
174 pj_size_t alignment,
175 pj_pool_callback *callback)
176 {
177
178 PJ_CHECK_STACK();
179 pj_assert(!alignment || PJ_IS_POWER_OF_TWO(alignment));
180
181 pool->increment_size = increment_size;
182 pool->callback = callback;
183 pool->alignment = !alignment ? PJ_POOL_ALIGNMENT : alignment;
184
185 if (name) {
186 char *p = pj_ansi_strchr(name, '%');
187 if (p && *(p+1)=='p' && *(p+2)=='\0') {
188 /* Special name with "%p" suffix */
189 pj_ansi_snprintf(pool->obj_name, sizeof(pool->obj_name),
190 name, pool);
191 } else {
192 pj_ansi_strxcpy(pool->obj_name, name, PJ_MAX_OBJ_NAME);
193 }
194 } else {
195 pool->obj_name[0] = '\0';
196 }
197 }
198
199 /*
200 * Create new memory pool.
201 */
202 PJ_DEF(pj_pool_t*) pj_pool_create_int( pj_pool_factory *f, const char *name,
203 pj_size_t initial_size,
204 pj_size_t increment_size,
205 pj_size_t alignment,
206 pj_pool_callback *callback)
207 {
208 pj_pool_t *pool;
209 pj_pool_block *block;
210 pj_uint8_t *buffer;
211
212 PJ_CHECK_STACK();
213
214 /* Size must be at least sizeof(pj_pool)+sizeof(pj_pool_block) */
215 PJ_ASSERT_RETURN(initial_size >= sizeof(pj_pool_t)+sizeof(pj_pool_block),
216 NULL);
217 PJ_ASSERT_RETURN(!alignment || PJ_IS_POWER_OF_TWO(alignment), NULL);
218
219 /* If callback is NULL, set calback from the policy */
220 if (callback == NULL)
221 callback = f->policy.callback;
222
223 /* Allocate initial block */
224 buffer = (pj_uint8_t*) (*f->policy.block_alloc)(f, initial_size);
225 if (!buffer)
226 return NULL;
227
228 /* Set pool administrative data. */
229 pool = (pj_pool_t*)buffer;
230 pj_bzero(pool, sizeof(*pool));
231
232 pj_list_init(&pool->block_list);
233 pool->factory = f;
234
235 /* Create the first block from the memory. */
236 block = (pj_pool_block*) (buffer + sizeof(*pool));
237 block->buf = ((unsigned char*)block) + sizeof(pj_pool_block);
238 block->end = buffer + initial_size;
239
240 /* Set the start pointer, unaligned! */
241 block->cur = block->buf;
242
243 pj_list_insert_after(&pool->block_list, block);
244
245 pj_pool_init_int(pool, name, increment_size, alignment, callback);
246
247 /* Pool initial capacity and used size */
248 pool->capacity = initial_size;
249
250 LOG((pool->obj_name, "pool created, size=%lu",
251 (unsigned long)pool->capacity));
252 return pool;
253 }
254
255 /*
256 * Reset the pool to the state when it was created.
257 * All blocks will be deallocated except the first block. All memory areas
258 * are marked as free.
259 */
260 static void reset_pool(pj_pool_t *pool)
261 {
262 pj_pool_block *block;
263
264 PJ_CHECK_STACK();
265
266 block = pool->block_list.prev;
267 if (block == &pool->block_list)
268 return;
269
270 /* Skip the first block because it is occupying the same memory
271 as the pool itself.
272 */
273 block = block->prev;
274
275 while (block != &pool->block_list) {
276 pj_pool_block *prev = block->prev;
277 pj_list_erase(block);
278 (*pool->factory->policy.block_free)(pool->factory, block,
279 block->end - (unsigned char*)block);
280 block = prev;
281 }
282
283 block = pool->block_list.next;
284
285 /* Set the start pointer, unaligned! */
286 block->cur = block->buf;
287
288 pool->capacity = block->end - (unsigned char*)pool;
289 }
290
291 /*
292 * The public function to reset pool.
293 */
294 PJ_DEF(void) pj_pool_reset(pj_pool_t *pool)
295 {
296 LOG((pool->obj_name, "reset(): cap=%lu, used=%lu(%lu%%)",
297 (unsigned long)pool->capacity,
298 (unsigned long)pj_pool_get_used_size(pool),
299 (unsigned long)(pj_pool_get_used_size(pool)*100/pool->capacity)));
300
301 reset_pool(pool);
302 }
303
304 /*
305 * Destroy the pool.
306 */
307 PJ_DEF(void) pj_pool_destroy_int(pj_pool_t *pool)
308 {
309 pj_size_t initial_size;
310
311 LOG((pool->obj_name, "destroy(): cap=%lu, used=%lu(%lu%%), block0=%p-%p",
312 (unsigned long)pool->capacity,
313 (unsigned long)pj_pool_get_used_size(pool),
314 (unsigned long)(pj_pool_get_used_size(pool)*100/pool->capacity),
315 ((pj_pool_block*)pool->block_list.next)->buf,
316 ((pj_pool_block*)pool->block_list.next)->end));
317
318 reset_pool(pool);
319 initial_size = ((pj_pool_block*)pool->block_list.next)->end -
320 (unsigned char*)pool;
321 if (pool->factory->policy.block_free)
322 (*pool->factory->policy.block_free)(pool->factory, pool, initial_size);
323 }
324
325
326 #endif /* PJ_HAS_POOL_ALT_API */
327