Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 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> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 24 | |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 25 | #if !PJ_HAS_POOL_ALT_API |
| 26 | |
| 27 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 28 | /* Include inline definitions when inlining is disabled. */ |
| 29 | #if !PJ_FUNCTIONS_ARE_INLINED |
| 30 | # include <pj/pool_i.h> |
| 31 | #endif |
| 32 | |
Benny Prijono | c81dfef | 2006-01-07 18:41:22 +0000 | [diff] [blame] | 33 | #define LOG(expr) PJ_LOG(6,expr) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 34 | |
| 35 | int PJ_NO_MEMORY_EXCEPTION; |
| 36 | |
| 37 | /* |
| 38 | * Create new block. |
| 39 | * Create a new big chunk of memory block, from which user allocation will be |
| 40 | * taken from. |
| 41 | */ |
| 42 | static pj_pool_block *pj_pool_create_block( pj_pool_t *pool, pj_size_t size) |
| 43 | { |
| 44 | pj_pool_block *block; |
| 45 | |
| 46 | PJ_CHECK_STACK(); |
| 47 | pj_assert(size >= sizeof(pj_pool_block)); |
| 48 | |
| 49 | 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] | 50 | size, pool->capacity, pj_pool_get_used_size(pool))); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 51 | |
| 52 | /* Request memory from allocator. */ |
| 53 | block = (pj_pool_block*) |
| 54 | (*pool->factory->policy.block_alloc)(pool->factory, size); |
| 55 | if (block == NULL) { |
| 56 | (*pool->callback)(pool, size); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | /* Add capacity. */ |
| 61 | pool->capacity += size; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 62 | |
| 63 | /* Set block attribytes. */ |
| 64 | block->cur = block->buf = ((unsigned char*)block) + sizeof(pj_pool_block); |
| 65 | block->end = ((unsigned char*)block) + size; |
| 66 | |
| 67 | /* Insert in the front of the list. */ |
| 68 | pj_list_insert_after(&pool->block_list, block); |
| 69 | |
| 70 | LOG((pool->obj_name," block created, buffer=%p-%p",block->buf, block->end)); |
| 71 | |
| 72 | return block; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Allocate memory chunk for user from available blocks. |
| 77 | * This will iterate through block list to find space to allocate the chunk. |
| 78 | * If no space is available in all the blocks, a new block might be created |
| 79 | * (depending on whether the pool is allowed to resize). |
| 80 | */ |
| 81 | PJ_DEF(void*) pj_pool_allocate_find(pj_pool_t *pool, unsigned size) |
| 82 | { |
| 83 | pj_pool_block *block = pool->block_list.next; |
| 84 | void *p; |
| 85 | unsigned block_size; |
| 86 | |
| 87 | PJ_CHECK_STACK(); |
| 88 | |
| 89 | while (block != &pool->block_list) { |
Benny Prijono | d493480 | 2005-11-21 16:58:03 +0000 | [diff] [blame] | 90 | p = pj_pool_alloc_from_block(block, size); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 91 | if (p != NULL) |
| 92 | return p; |
| 93 | block = block->next; |
| 94 | } |
| 95 | /* No available space in all blocks. */ |
| 96 | |
| 97 | /* If pool is configured NOT to expand, return error. */ |
| 98 | if (pool->increment_size == 0) { |
| 99 | LOG((pool->obj_name, "Can't expand pool to allocate %u bytes " |
| 100 | "(used=%u, cap=%u)", |
Benny Prijono | d493480 | 2005-11-21 16:58:03 +0000 | [diff] [blame] | 101 | size, pj_pool_get_used_size(pool), pool->capacity)); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 102 | (*pool->callback)(pool, size); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | /* If pool is configured to expand, but the increment size |
| 107 | * is less than the required size, expand the pool by multiple |
| 108 | * increment size |
| 109 | */ |
| 110 | if (pool->increment_size < size + sizeof(pj_pool_block)) { |
| 111 | unsigned count; |
| 112 | count = (size + pool->increment_size + sizeof(pj_pool_block)) / |
| 113 | pool->increment_size; |
| 114 | block_size = count * pool->increment_size; |
| 115 | |
| 116 | } else { |
| 117 | block_size = pool->increment_size; |
| 118 | } |
| 119 | |
| 120 | LOG((pool->obj_name, |
| 121 | "%u bytes requested, resizing pool by %u bytes (used=%u, cap=%u)", |
Benny Prijono | d493480 | 2005-11-21 16:58:03 +0000 | [diff] [blame] | 122 | size, block_size, pj_pool_get_used_size(pool), pool->capacity)); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 123 | |
| 124 | block = pj_pool_create_block(pool, block_size); |
| 125 | if (!block) |
| 126 | return NULL; |
| 127 | |
Benny Prijono | d493480 | 2005-11-21 16:58:03 +0000 | [diff] [blame] | 128 | p = pj_pool_alloc_from_block(block, size); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 129 | pj_assert(p != NULL); |
| 130 | #if PJ_DEBUG |
| 131 | if (p == NULL) { |
| 132 | p = p; |
| 133 | } |
| 134 | #endif |
| 135 | return p; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Internal function to initialize pool. |
| 140 | */ |
| 141 | PJ_DEF(void) pj_pool_init_int( pj_pool_t *pool, |
| 142 | const char *name, |
| 143 | pj_size_t increment_size, |
| 144 | pj_pool_callback *callback) |
| 145 | { |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 146 | PJ_CHECK_STACK(); |
| 147 | |
| 148 | pool->increment_size = increment_size; |
| 149 | pool->callback = callback; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 150 | |
| 151 | if (name) { |
| 152 | if (strchr(name, '%') != NULL) { |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 153 | pj_ansi_snprintf(pool->obj_name, sizeof(pool->obj_name), |
| 154 | name, pool); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 155 | } else { |
Benny Prijono | ed811d7 | 2006-03-10 12:57:12 +0000 | [diff] [blame] | 156 | pj_ansi_strncpy(pool->obj_name, name, PJ_MAX_OBJ_NAME); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 157 | } |
| 158 | } else { |
| 159 | pool->obj_name[0] = '\0'; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Create new memory pool. |
| 165 | */ |
| 166 | PJ_DEF(pj_pool_t*) pj_pool_create_int( pj_pool_factory *f, const char *name, |
| 167 | pj_size_t initial_size, |
| 168 | pj_size_t increment_size, |
| 169 | pj_pool_callback *callback) |
| 170 | { |
| 171 | pj_pool_t *pool; |
| 172 | pj_pool_block *block; |
| 173 | unsigned char *buffer; |
| 174 | |
| 175 | PJ_CHECK_STACK(); |
| 176 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 177 | /* If callback is NULL, set calback from the policy */ |
| 178 | if (callback == NULL) |
| 179 | callback = f->policy.callback; |
| 180 | |
| 181 | /* Allocate initial block */ |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 182 | buffer = (*f->policy.block_alloc)(f, initial_size); |
| 183 | if (!buffer) |
| 184 | return NULL; |
| 185 | |
| 186 | /* Set pool administrative data. */ |
| 187 | pool = (pj_pool_t*)buffer; |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 188 | pj_bzero(pool, sizeof(*pool)); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 189 | |
| 190 | pj_list_init(&pool->block_list); |
| 191 | pool->factory = f; |
| 192 | |
| 193 | /* Create the first block from the memory. */ |
| 194 | block = (pj_pool_block*) (buffer + sizeof(*pool)); |
| 195 | block->cur = block->buf = ((unsigned char*)block) + sizeof(pj_pool_block); |
| 196 | block->end = buffer + initial_size; |
| 197 | pj_list_insert_after(&pool->block_list, block); |
| 198 | |
| 199 | pj_pool_init_int(pool, name, increment_size, callback); |
| 200 | |
| 201 | /* Pool initial capacity and used size */ |
| 202 | pool->capacity = initial_size; |
| 203 | |
| 204 | LOG((pool->obj_name, "pool created, size=%u", pool->capacity)); |
| 205 | return pool; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Reset the pool to the state when it was created. |
| 210 | * All blocks will be deallocated except the first block. All memory areas |
| 211 | * are marked as free. |
| 212 | */ |
| 213 | static void reset_pool(pj_pool_t *pool) |
| 214 | { |
| 215 | pj_pool_block *block; |
| 216 | |
| 217 | PJ_CHECK_STACK(); |
| 218 | |
| 219 | block = pool->block_list.prev; |
| 220 | if (block == &pool->block_list) |
| 221 | return; |
| 222 | |
| 223 | /* Skip the first block because it is occupying the same memory |
| 224 | as the pool itself. |
| 225 | */ |
| 226 | block = block->prev; |
| 227 | |
| 228 | while (block != &pool->block_list) { |
| 229 | pj_pool_block *prev = block->prev; |
| 230 | pj_list_erase(block); |
| 231 | (*pool->factory->policy.block_free)(pool->factory, block, |
| 232 | block->end - (unsigned char*)block); |
| 233 | block = prev; |
| 234 | } |
| 235 | |
| 236 | block = pool->block_list.next; |
| 237 | block->cur = block->buf; |
| 238 | pool->capacity = block->end - (unsigned char*)pool; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
| 242 | * The public function to reset pool. |
| 243 | */ |
| 244 | PJ_DEF(void) pj_pool_reset(pj_pool_t *pool) |
| 245 | { |
| 246 | LOG((pool->obj_name, "reset(): cap=%d, used=%d(%d%%)", |
Benny Prijono | d493480 | 2005-11-21 16:58:03 +0000 | [diff] [blame] | 247 | pool->capacity, pj_pool_get_used_size(pool), |
| 248 | pj_pool_get_used_size(pool)*100/pool->capacity)); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 249 | |
| 250 | reset_pool(pool); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Destroy the pool. |
| 255 | */ |
| 256 | PJ_DEF(void) pj_pool_destroy_int(pj_pool_t *pool) |
| 257 | { |
| 258 | pj_size_t initial_size; |
| 259 | |
| 260 | 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] | 261 | pool->capacity, pj_pool_get_used_size(pool), |
| 262 | pj_pool_get_used_size(pool)*100/pool->capacity, |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 263 | ((pj_pool_block*)pool->block_list.next)->buf, |
| 264 | ((pj_pool_block*)pool->block_list.next)->end)); |
| 265 | |
| 266 | reset_pool(pool); |
| 267 | initial_size = ((pj_pool_block*)pool->block_list.next)->end - |
| 268 | (unsigned char*)pool; |
| 269 | (*pool->factory->policy.block_free)(pool->factory, pool, initial_size); |
| 270 | } |
| 271 | |
| 272 | |
Benny Prijono | 8508aa0 | 2006-03-30 15:56:01 +0000 | [diff] [blame] | 273 | #endif /* PJ_HAS_POOL_ALT_API */ |
| 274 | |