Benny Prijono | 4766ffe | 2005-11-01 17:56:59 +0000 | [diff] [blame^] | 1 | /* $Id$ |
| 2 | * |
| 3 | */ |
Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame] | 4 | |
| 5 | |
| 6 | #include <pj/string.h> |
| 7 | |
| 8 | PJ_DECL(void*) pj_pool_allocate_find(pj_pool_t *pool, unsigned size); |
| 9 | |
| 10 | PJ_IDEF(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool ) |
| 11 | { |
| 12 | return pool->capacity; |
| 13 | } |
| 14 | |
| 15 | PJ_IDEF(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool ) |
| 16 | { |
| 17 | return pool->used_size; |
| 18 | } |
| 19 | |
| 20 | PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_t *pool, |
| 21 | pj_pool_block *block, pj_size_t size ) |
| 22 | { |
| 23 | /* The operation below is valid for size==0. |
| 24 | * When size==0, the function will return the pointer to the pool |
| 25 | * memory address, but no memory will be allocated. |
| 26 | */ |
| 27 | if (size & (PJ_POOL_ALIGNMENT-1)) { |
| 28 | size &= ~(PJ_POOL_ALIGNMENT-1); |
| 29 | size += PJ_POOL_ALIGNMENT; |
| 30 | } |
| 31 | if ((unsigned)(block->end - block->cur) >= size) { |
| 32 | void *ptr = block->cur; |
| 33 | block->cur += size; |
| 34 | pool->used_size += size; |
| 35 | return ptr; |
| 36 | } |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size) |
| 41 | { |
| 42 | pj_pool_block *block = pool->block_list.next; |
| 43 | void *ptr = pj_pool_alloc_from_block(pool, block, size); |
| 44 | if (!ptr) |
| 45 | ptr = pj_pool_allocate_find(pool, size); |
| 46 | return ptr; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | PJ_IDEF(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count, pj_size_t size) |
| 51 | { |
| 52 | void *buf = pj_pool_alloc( pool, size*count); |
| 53 | if (buf) |
| 54 | pj_memset(buf, 0, size * count); |
| 55 | return buf; |
| 56 | } |
| 57 | |
| 58 | PJ_IDEF(const char *) pj_pool_getobjname( const pj_pool_t *pool ) |
| 59 | { |
| 60 | return pool->obj_name; |
| 61 | } |
| 62 | |
| 63 | PJ_IDEF(pj_pool_t*) pj_pool_create( pj_pool_factory *f, |
| 64 | const char *name, |
| 65 | pj_size_t initial_size, |
| 66 | pj_size_t increment_size, |
| 67 | pj_pool_callback *callback) |
| 68 | { |
| 69 | return (*f->create_pool)(f, name, initial_size, increment_size, callback); |
| 70 | } |
| 71 | |
| 72 | PJ_IDEF(void) pj_pool_release( pj_pool_t *pool ) |
| 73 | { |
| 74 | (*pool->factory->release_pool)(pool->factory, pool); |
| 75 | } |
| 76 | |