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