Changeset 69


Ignore:
Timestamp:
Nov 21, 2005 4:58:03 PM (17 years ago)
Author:
bennylp
Message:

Small optimization in pool (removed used_size)

Location:
pjproject/trunk/pjlib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/include/pj/pool.h

    r66 r69  
    151151    /** Current capacity allocated by the pool. */ 
    152152    pj_size_t       capacity; 
    153  
    154     /** Number of memory used/allocated. */ 
    155     pj_size_t       used_size; 
    156153 
    157154    /** Size of memory block to be allocated when the pool runs out of memory */ 
  • pjproject/trunk/pjlib/include/pj/pool_i.h

    r66 r69  
    3030PJ_IDEF(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool ) 
    3131{ 
    32     return pool->used_size; 
     32    pj_pool_block *b = pool->block_list.next; 
     33    pj_size_t used_size = sizeof(pj_pool_t); 
     34    while (b != &pool->block_list) { 
     35        used_size += (b->cur - b->buf) + sizeof(pj_pool_block); 
     36        b = b->next; 
     37    } 
     38    return used_size; 
    3339} 
    3440 
    35 PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_t *pool, 
    36                                          pj_pool_block *block, pj_size_t size ) 
     41PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t size ) 
    3742{ 
    3843    /* The operation below is valid for size==0.  
     
    4146     */ 
    4247    if (size & (PJ_POOL_ALIGNMENT-1)) { 
    43         size &= ~(PJ_POOL_ALIGNMENT-1); 
    44         size += PJ_POOL_ALIGNMENT; 
     48        size = (size + PJ_POOL_ALIGNMENT) & ~(PJ_POOL_ALIGNMENT-1); 
    4549    } 
    4650    if ((unsigned)(block->end - block->cur) >= size) { 
    4751        void *ptr = block->cur; 
    4852        block->cur += size; 
    49         pool->used_size += size; 
    5053        return ptr; 
    5154    } 
     
    5558PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size) 
    5659{ 
    57     pj_pool_block *block = pool->block_list.next; 
    58     void *ptr = pj_pool_alloc_from_block(pool, block, size); 
     60    void *ptr = pj_pool_alloc_from_block(pool->block_list.next, size); 
    5961    if (!ptr) 
    6062        ptr = pj_pool_allocate_find(pool, size); 
  • pjproject/trunk/pjlib/src/pj/pool.c

    r66 r69  
    4646 
    4747    LOG((pool->obj_name, "create_block(sz=%u), cur.cap=%u, cur.used=%u",  
    48          size, pool->capacity, pool->used_size)); 
     48         size, pool->capacity, pj_pool_get_used_size(pool))); 
    4949 
    5050    /* Request memory from allocator. */ 
     
    5858    /* Add capacity. */ 
    5959    pool->capacity += size; 
    60     pool->used_size += sizeof(pj_pool_block); 
    6160 
    6261    /* Set block attribytes. */ 
     
    8786 
    8887    while (block != &pool->block_list) { 
    89         p = pj_pool_alloc_from_block(pool, block, size); 
     88        p = pj_pool_alloc_from_block(block, size); 
    9089        if (p != NULL) 
    9190            return p; 
     
    9897        LOG((pool->obj_name, "Can't expand pool to allocate %u bytes " 
    9998             "(used=%u, cap=%u)", 
    100              size, pool->used_size, pool->capacity)); 
     99             size, pj_pool_get_used_size(pool), pool->capacity)); 
    101100        (*pool->callback)(pool, size); 
    102101        return NULL; 
     
    119118    LOG((pool->obj_name,  
    120119         "%u bytes requested, resizing pool by %u bytes (used=%u, cap=%u)", 
    121          size, block_size, pool->used_size, pool->capacity)); 
     120         size, block_size, pj_pool_get_used_size(pool), pool->capacity)); 
    122121 
    123122    block = pj_pool_create_block(pool, block_size); 
     
    125124        return NULL; 
    126125 
    127     p = pj_pool_alloc_from_block(pool, block, size); 
     126    p = pj_pool_alloc_from_block(block, size); 
    128127    pj_assert(p != NULL); 
    129128#if PJ_DEBUG 
     
    143142                                pj_pool_callback *callback) 
    144143{ 
    145     pj_pool_block *block; 
    146  
    147144    PJ_CHECK_STACK(); 
    148145 
    149146    pool->increment_size = increment_size; 
    150147    pool->callback = callback; 
    151     pool->used_size = sizeof(*pool); 
    152     block = pool->block_list.next; 
    153     while (block != &pool->block_list) { 
    154         pool->used_size += sizeof(pj_pool_block); 
    155         block = block->next; 
    156     } 
    157148 
    158149    if (name) { 
     
    238229    block->cur = block->buf; 
    239230    pool->capacity = block->end - (unsigned char*)pool; 
    240     pool->used_size = 0; 
    241231} 
    242232 
     
    247237{ 
    248238    LOG((pool->obj_name, "reset(): cap=%d, used=%d(%d%%)",  
    249         pool->capacity, pool->used_size, pool->used_size*100/pool->capacity)); 
     239        pool->capacity, pj_pool_get_used_size(pool),  
     240        pj_pool_get_used_size(pool)*100/pool->capacity)); 
    250241 
    251242    reset_pool(pool); 
     
    260251 
    261252    LOG((pool->obj_name, "destroy(): cap=%d, used=%d(%d%%), block0=%p-%p",  
    262         pool->capacity, pool->used_size, pool->used_size*100/pool->capacity, 
     253        pool->capacity, pj_pool_get_used_size(pool),  
     254        pj_pool_get_used_size(pool)*100/pool->capacity, 
    263255        ((pj_pool_block*)pool->block_list.next)->buf,  
    264256        ((pj_pool_block*)pool->block_list.next)->end)); 
  • pjproject/trunk/pjlib/src/pj/pool_caching.c

    r66 r69  
    175175    /* Reset pool. */ 
    176176    PJ_LOG(4, (pool->obj_name, "recycle(): cap=%d, used=%d(%d%%)",  
    177                pool->capacity, pool->used_size, pool->used_size*100/pool->capacity)); 
     177               pool->capacity, pj_pool_get_used_size(pool),  
     178               pj_pool_get_used_size(pool)*100/pool->capacity)); 
    178179    pj_pool_reset(pool); 
    179180 
     
    208209        while (pool != (void*)&cp->used_list) { 
    209210            PJ_LOG(3,("cachpool", "   %12s: %8d of %8d (%d%%) used", pool->obj_name,  
    210                                   pool->used_size, pool->capacity, 
    211                                   pool->used_size*100/pool->capacity)); 
    212             total_used += pool->used_size; 
     211                                  pj_pool_get_used_size(pool), pool->capacity, 
     212                                  pj_pool_get_used_size(pool)*100/pool->capacity)); 
     213            total_used += pj_pool_get_used_size(pool); 
    213214            total_capacity += pool->capacity; 
    214215            pool = pool->next; 
Note: See TracChangeset for help on using the changeset viewer.