/* * Create new memory pool. */ PJ_DEF(pj_pool_t*) pj_pool_create_int( pj_pool_factory *f, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback) { pj_pool_t *pool; pj_pool_block *block; pj_uint8_t *buffer; PJ_CHECK_STACK(); /* Size must be at least sizeof(pj_pool)+sizeof(pj_pool_block) */ PJ_ASSERT_RETURN(initial_size >= sizeof(pj_pool_t)+sizeof(pj_pool_block), NULL); /* If callback is NULL, set calback from the policy */ if (callback == NULL) callback = f->policy.callback; /* Allocate initial block */ buffer = (pj_uint8_t*) (*f->policy.block_alloc)(f, initial_size); if (!buffer) return NULL; /* Set pool administrative data. */ pool = (pj_pool_t*)buffer; pj_bzero(pool, sizeof(*pool)); pj_list_init(&pool->block_list); pool->factory = f; /* Create the first block from the memory. */ block = (pj_pool_block*) (buffer + sizeof(*pool)); block->buf = ((unsigned char*)block) + sizeof(pj_pool_block); block->end = buffer + initial_size; /* Set the start pointer, aligning it as needed */ block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT); pj_list_insert_after(&pool->block_list, block); pj_pool_init_int(pool, name, increment_size, callback); /* Pool initial capacity and used size */ pool->capacity = initial_size; LOG((pool->obj_name, "pool created, size=%u", pool->capacity)); return pool; }
static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, const char *name, pj_size_t initial_size, pj_size_t increment_sz, pj_pool_callback *callback) { pj_caching_pool *cp = (pj_caching_pool*)pf; pj_pool_t *pool; int idx; PJ_CHECK_STACK(); pj_lock_acquire(cp->lock); /* Use pool factory's policy when callback is NULL */ if (callback == NULL) { callback = pf->policy.callback; } /* Search the suitable size for the pool. * We'll just do linear search to the size array, as the array size itself * is only a few elements. Binary search I suspect will be less efficient * for this purpose. */ if (initial_size <= pool_sizes[START_SIZE]) { for (idx=START_SIZE-1; idx >= 0 && pool_sizes[idx] >= initial_size; --idx) ; ++idx; } else { for (idx=START_SIZE+1; idx < PJ_CACHING_POOL_ARRAY_SIZE && pool_sizes[idx] < initial_size; ++idx) ; } /* Check whether there's a pool in the list. */ if (idx==PJ_CACHING_POOL_ARRAY_SIZE || pj_list_empty(&cp->free_list[idx])) { /* No pool is available. */ /* Set minimum size. */ if (idx < PJ_CACHING_POOL_ARRAY_SIZE) initial_size = pool_sizes[idx]; /* Create new pool */ pool = pj_pool_create_int(&cp->factory, name, initial_size, increment_sz, callback); if (!pool) { pj_lock_release(cp->lock); return NULL; } } else { /* Get one pool from the list. */ pool = (pj_pool_t*) cp->free_list[idx].next; pj_list_erase(pool); /* Initialize the pool. */ pj_pool_init_int(pool, name, increment_sz, callback); /* Update pool manager's free capacity. */ cp->capacity -= pj_pool_get_capacity(pool); PJ_LOG(6, (pool->obj_name, "pool reused, size=%u", pool->capacity)); } /* Put in used list. */ pj_list_insert_before( &cp->used_list, pool ); /* Mark factory data */ pool->factory_data = (void*) (long) idx; /* Increment used count. */ ++cp->used_count; pj_lock_release(cp->lock); return pool; }