| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pj/pool.h> |
| 20 |
#include <pj/assert.h> |
| 21 |
#include <pj/log.h> |
| 22 |
#include <pj/string.h> |
| 23 |
#include <pj/assert.h> |
| 24 |
#include <pj/lock.h> |
| 25 |
#include <pj/os.h> |
| 26 |
#include <pj/pool_buf.h> |
| 27 |
|
| 28 |
#if !PJ_HAS_POOL_ALT_API |
| 29 |
|
| 30 |
static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, |
| 31 |
const char *name, |
| 32 |
pj_size_t initial_size, |
| 33 |
pj_size_t increment_sz, |
| 34 |
pj_size_t alignment, |
| 35 |
pj_pool_callback *callback); |
| 36 |
static void cpool_release_pool(pj_pool_factory *pf, pj_pool_t *pool); |
| 37 |
static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail ); |
| 38 |
static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz); |
| 39 |
static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz); |
| 40 |
|
| 41 |
|
| 42 |
static pj_size_t pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE] = |
| 43 |
{ |
| 44 |
256, 512, 1024, 2048, 4096, 8192, 12288, 16384, |
| 45 |
20480, 24576, 28672, 32768, 40960, 49152, 57344, 65536 |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#define START_SIZE 5 |
| 52 |
|
| 53 |
|
| 54 |
PJ_DEF(void) pj_caching_pool_init( pj_caching_pool *cp, |
| 55 |
const pj_pool_factory_policy *policy, |
| 56 |
pj_size_t max_capacity) |
| 57 |
{ |
| 58 |
int i; |
| 59 |
pj_pool_t *pool; |
| 60 |
pj_status_t status; |
| 61 |
|
| 62 |
PJ_CHECK_STACK(); |
| 63 |
|
| 64 |
pj_bzero(cp, sizeof(*cp)); |
| 65 |
|
| 66 |
cp->max_capacity = max_capacity; |
| 67 |
pj_list_init(&cp->used_list); |
| 68 |
for (i=0; i<PJ_CACHING_POOL_ARRAY_SIZE; ++i) |
| 69 |
pj_list_init(&cp->free_list[i]); |
| 70 |
|
| 71 |
if (policy == NULL) { |
| 72 |
policy = &pj_pool_factory_default_policy; |
| 73 |
} |
| 74 |
|
| 75 |
pj_memcpy(&cp->factory.policy, policy, sizeof(pj_pool_factory_policy)); |
| 76 |
cp->factory.create_pool = &cpool_create_pool; |
| 77 |
cp->factory.release_pool = &cpool_release_pool; |
| 78 |
cp->factory.dump_status = &cpool_dump_status; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
PJ_UNUSED_ARG(cpool_on_block_alloc); |
| 86 |
PJ_UNUSED_ARG(cpool_on_block_free); |
| 87 |
|
| 88 |
pool = pj_pool_create_on_buf("cachingpool", cp->pool_buf, sizeof(cp->pool_buf)); |
| 89 |
status = pj_lock_create_simple_mutex(pool, "cachingpool", &cp->lock); |
| 90 |
|
| 91 |
|
| 92 |
PJ_ASSERT_ON_FAIL(status==PJ_SUCCESS, return); |
| 93 |
} |
| 94 |
|
| 95 |
PJ_DEF(void) pj_caching_pool_destroy( pj_caching_pool *cp ) |
| 96 |
{ |
| 97 |
int i; |
| 98 |
pj_pool_t *pool; |
| 99 |
|
| 100 |
PJ_CHECK_STACK(); |
| 101 |
|
| 102 |
|
| 103 |
for (i=0; i < PJ_CACHING_POOL_ARRAY_SIZE; ++i) { |
| 104 |
pj_pool_t *next; |
| 105 |
pool = (pj_pool_t*) cp->free_list[i].next; |
| 106 |
for (; pool != (void*)&cp->free_list[i]; pool = next) { |
| 107 |
next = pool->next; |
| 108 |
pj_list_erase(pool); |
| 109 |
pj_pool_destroy_int(pool); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
pool = (pj_pool_t*) cp->used_list.next; |
| 115 |
while (pool != (pj_pool_t*) &cp->used_list) { |
| 116 |
pj_pool_t *next = pool->next; |
| 117 |
pj_list_erase(pool); |
| 118 |
PJ_LOG(4,(pool->obj_name, |
| 119 |
"Pool is not released by application, releasing now")); |
| 120 |
pj_pool_destroy_int(pool); |
| 121 |
pool = next; |
| 122 |
} |
| 123 |
|
| 124 |
if (cp->lock) { |
| 125 |
pj_status_t status; |
| 126 |
pj_lock_destroy(cp->lock); |
| 127 |
status = pj_lock_create_null_mutex(NULL, "cachingpool", &cp->lock); |
| 128 |
PJ_ASSERT_ON_FAIL(status==PJ_SUCCESS, return); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, |
| 133 |
const char *name, |
| 134 |
pj_size_t initial_size, |
| 135 |
pj_size_t increment_sz, |
| 136 |
pj_size_t alignment, |
| 137 |
pj_pool_callback *callback) |
| 138 |
{ |
| 139 |
pj_caching_pool *cp = (pj_caching_pool*)pf; |
| 140 |
pj_pool_t *pool; |
| 141 |
int idx; |
| 142 |
|
| 143 |
PJ_CHECK_STACK(); |
| 144 |
|
| 145 |
pj_lock_acquire(cp->lock); |
| 146 |
|
| 147 |
|
| 148 |
if (callback == NULL) { |
| 149 |
callback = pf->policy.callback; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if (initial_size <= pool_sizes[START_SIZE]) { |
| 158 |
for (idx=START_SIZE-1; |
| 159 |
idx >= 0 && pool_sizes[idx] >= initial_size; |
| 160 |
--idx) |
| 161 |
; |
| 162 |
++idx; |
| 163 |
} else { |
| 164 |
for (idx=START_SIZE+1; |
| 165 |
idx < PJ_CACHING_POOL_ARRAY_SIZE && |
| 166 |
pool_sizes[idx] < initial_size; |
| 167 |
++idx) |
| 168 |
; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
if (idx==PJ_CACHING_POOL_ARRAY_SIZE || pj_list_empty(&cp->free_list[idx])) { |
| 173 |
|
| 174 |
|
| 175 |
if (idx < PJ_CACHING_POOL_ARRAY_SIZE) |
| 176 |
initial_size = pool_sizes[idx]; |
| 177 |
|
| 178 |
|
| 179 |
pool = pj_pool_create_int(&cp->factory, name, initial_size, |
| 180 |
increment_sz, alignment, callback); |
| 181 |
if (!pool) { |
| 182 |
pj_lock_release(cp->lock); |
| 183 |
return NULL; |
| 184 |
} |
| 185 |
|
| 186 |
} else { |
| 187 |
|
| 188 |
pool = (pj_pool_t*) cp->free_list[idx].next; |
| 189 |
pj_list_erase(pool); |
| 190 |
|
| 191 |
|
| 192 |
pj_pool_init_int(pool, name, increment_sz, alignment, callback); |
| 193 |
|
| 194 |
|
| 195 |
if (cp->capacity > pj_pool_get_capacity(pool)) { |
| 196 |
cp->capacity -= pj_pool_get_capacity(pool); |
| 197 |
} else { |
| 198 |
cp->capacity = 0; |
| 199 |
} |
| 200 |
|
| 201 |
PJ_LOG(6, (pool->obj_name, "pool reused, size=%lu", |
| 202 |
(unsigned long)pool->capacity)); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
pj_list_insert_before( &cp->used_list, pool ); |
| 207 |
|
| 208 |
|
| 209 |
pool->factory_data = (void*) (pj_ssize_t) idx; |
| 210 |
|
| 211 |
|
| 212 |
++cp->used_count; |
| 213 |
|
| 214 |
pj_lock_release(cp->lock); |
| 215 |
return pool; |
| 216 |
} |
| 217 |
|
| 218 |
static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool) |
| 219 |
{ |
| 220 |
pj_caching_pool *cp = (pj_caching_pool*)pf; |
| 221 |
pj_size_t pool_capacity; |
| 222 |
unsigned i; |
| 223 |
|
| 224 |
PJ_CHECK_STACK(); |
| 225 |
|
| 226 |
PJ_ASSERT_ON_FAIL(pf && pool, return); |
| 227 |
|
| 228 |
pj_lock_acquire(cp->lock); |
| 229 |
|
| 230 |
#if PJ_SAFE_POOL |
| 231 |
|
| 232 |
if (pj_list_find_node(&cp->used_list, pool) != pool) { |
| 233 |
pj_lock_release(cp->lock); |
| 234 |
pj_assert(!"Attempt to destroy pool that has been destroyed before"); |
| 235 |
return; |
| 236 |
} |
| 237 |
#endif |
| 238 |
|
| 239 |
|
| 240 |
pj_list_erase(pool); |
| 241 |
|
| 242 |
|
| 243 |
--cp->used_count; |
| 244 |
|
| 245 |
pool_capacity = pj_pool_get_capacity(pool); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if (pool_capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] || |
| 252 |
cp->capacity + pool_capacity > cp->max_capacity) |
| 253 |
{ |
| 254 |
pj_pool_destroy_int(pool); |
| 255 |
pj_lock_release(cp->lock); |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
PJ_LOG(6, (pool->obj_name, "recycle(): cap=%lu, used=%lu(%lu%%)", |
| 261 |
(unsigned long)pool_capacity, |
| 262 |
(unsigned long)pj_pool_get_used_size(pool), |
| 263 |
(unsigned long)(pj_pool_get_used_size(pool)*100/ |
| 264 |
pool_capacity))); |
| 265 |
pj_pool_reset(pool); |
| 266 |
|
| 267 |
pool_capacity = pj_pool_get_capacity(pool); |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
i = (unsigned) (unsigned long) (pj_ssize_t) pool->factory_data; |
| 273 |
|
| 274 |
pj_assert(i<PJ_CACHING_POOL_ARRAY_SIZE); |
| 275 |
if (i >= PJ_CACHING_POOL_ARRAY_SIZE ) { |
| 276 |
|
| 277 |
pj_pool_destroy_int(pool); |
| 278 |
pj_lock_release(cp->lock); |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
pj_list_insert_after(&cp->free_list[i], pool); |
| 283 |
cp->capacity += pool_capacity; |
| 284 |
|
| 285 |
pj_lock_release(cp->lock); |
| 286 |
} |
| 287 |
|
| 288 |
static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail ) |
| 289 |
{ |
| 290 |
#if PJ_LOG_MAX_LEVEL >= 3 |
| 291 |
pj_caching_pool *cp = (pj_caching_pool*)factory; |
| 292 |
|
| 293 |
pj_lock_acquire(cp->lock); |
| 294 |
|
| 295 |
PJ_LOG(3,("cachpool", " Dumping caching pool:")); |
| 296 |
PJ_LOG(3,("cachpool", " Capacity=%lu, max_capacity=%lu, used_cnt=%lu", |
| 297 |
(unsigned long)cp->capacity, (unsigned long)cp->max_capacity, |
| 298 |
(unsigned long)cp->used_count)); |
| 299 |
if (detail) { |
| 300 |
pj_pool_t *pool = (pj_pool_t*) cp->used_list.next; |
| 301 |
pj_size_t total_used = 0, total_capacity = 0; |
| 302 |
PJ_LOG(3,("cachpool", " Dumping all active pools:")); |
| 303 |
while (pool != (void*)&cp->used_list) { |
| 304 |
pj_size_t pool_capacity = pj_pool_get_capacity(pool); |
| 305 |
pj_pool_block *block = pool->block_list.next; |
| 306 |
unsigned nblocks = 0; |
| 307 |
|
| 308 |
while (block != &pool->block_list) { |
| 309 |
#if 0 |
| 310 |
PJ_LOG(6, ("cachpool", " %16s block %u, size %ld", |
| 311 |
pj_pool_getobjname(pool), nblocks, |
| 312 |
(long)(block->end - block->buf + 1))); |
| 313 |
#endif |
| 314 |
nblocks++; |
| 315 |
block = block->next; |
| 316 |
} |
| 317 |
|
| 318 |
PJ_LOG(3,("cachpool", " %16s: %8lu of %8lu (%lu%%) used, " |
| 319 |
"nblocks: %d", |
| 320 |
pj_pool_getobjname(pool), |
| 321 |
(unsigned long)pj_pool_get_used_size(pool), |
| 322 |
(unsigned long)pool_capacity, |
| 323 |
(unsigned long)(pj_pool_get_used_size(pool)* |
| 324 |
100/pool_capacity), |
| 325 |
nblocks)); |
| 326 |
|
| 327 |
#if PJ_POOL_MAX_SEARCH_BLOCK_COUNT == 0 |
| 328 |
if (nblocks >= 10) { |
| 329 |
PJ_LOG(3,("cachpool", " %16s has too many blocks (%d), " |
| 330 |
"consider increasing its initial and/or " |
| 331 |
"increment size for better performance", |
| 332 |
pj_pool_getobjname(pool), nblocks)); |
| 333 |
} |
| 334 |
#endif |
| 335 |
|
| 336 |
total_used += pj_pool_get_used_size(pool); |
| 337 |
total_capacity += pool_capacity; |
| 338 |
pool = pool->next; |
| 339 |
} |
| 340 |
if (total_capacity) { |
| 341 |
PJ_LOG(3,("cachpool", " Total %9lu of %9lu (%lu %%) used!", |
| 342 |
(unsigned long)total_used, |
| 343 |
(unsigned long)total_capacity, |
| 344 |
(unsigned long)(total_used * 100 / |
| 345 |
total_capacity))); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
pj_lock_release(cp->lock); |
| 350 |
#else |
| 351 |
PJ_UNUSED_ARG(factory); |
| 352 |
PJ_UNUSED_ARG(detail); |
| 353 |
#endif |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz) |
| 358 |
{ |
| 359 |
pj_caching_pool *cp = (pj_caching_pool*)f; |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
cp->used_size += sz; |
| 365 |
if (cp->used_size > cp->peak_used_size) |
| 366 |
cp->peak_used_size = cp->used_size; |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
return PJ_TRUE; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz) |
| 375 |
{ |
| 376 |
pj_caching_pool *cp = (pj_caching_pool*)f; |
| 377 |
|
| 378 |
|
| 379 |
cp->used_size -= sz; |
| 380 |
|
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
#endif |
| 385 |
|