Changeset 594


Ignore:
Timestamp:
Jul 9, 2006 10:05:46 AM (18 years ago)
Author:
bennylp
Message:

Added feature to report peak memory used in caching pool

Location:
pjproject/trunk/pjlib
Files:
3 edited

Legend:

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

    r588 r594  
    450450     */ 
    451451    void (*dump_status)( pj_pool_factory *factory, pj_bool_t detail ); 
     452 
     453    /** 
     454     * This is optional callback to be called by allocation policy when 
     455     * it allocates a new memory block. The factory may use this callback 
     456     * for example to keep track of the total number of memory blocks 
     457     * currently allocated by applications. 
     458     * 
     459     * @param factory       The pool factory. 
     460     * @param size          Size requested by application. 
     461     * 
     462     * @return              MUST return PJ_TRUE, otherwise the block 
     463     *                      allocation is cancelled. 
     464     */ 
     465    pj_bool_t (*on_block_alloc)(pj_pool_factory *factory, pj_size_t size); 
     466 
     467    /** 
     468     * This is optional callback to be called by allocation policy when 
     469     * it frees memory block. The factory may use this callback 
     470     * for example to keep track of the total number of memory blocks 
     471     * currently allocated by applications. 
     472     * 
     473     * @param factory       The pool factory. 
     474     * @param size          Size freed. 
     475     */ 
     476    void (*on_block_free)(pj_pool_factory *factory, pj_size_t size); 
     477 
    452478}; 
    453479 
     
    558584 
    559585    /** 
     586     * Total size of memory currently used by application. 
     587     */ 
     588    pj_size_t       used_size; 
     589 
     590    /** 
     591     * The maximum size of memory used by application throughout the life 
     592     * of the caching pool. 
     593     */ 
     594    pj_size_t       peak_used_size; 
     595 
     596    /** 
    560597     * Lists of pools in the cache, indexed by pool size. 
    561598     */ 
  • pjproject/trunk/pjlib/src/pj/pool_caching.c

    r588 r594  
    3232static void cpool_release_pool(pj_pool_factory *pf, pj_pool_t *pool); 
    3333static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail ); 
     34static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz); 
     35static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz); 
     36 
    3437 
    3538static pj_size_t pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE] =  
     
    6467    cp->factory.release_pool = &cpool_release_pool; 
    6568    cp->factory.dump_status = &cpool_dump_status; 
     69    cp->factory.on_block_alloc = &cpool_on_block_alloc; 
     70    cp->factory.on_block_free = &cpool_on_block_free; 
    6671 
    6772    cp->pool = pj_pool_create_int(&cp->factory, "cachingpool", 256,  
     
    274279} 
    275280 
     281 
     282static pj_bool_t cpool_on_block_alloc(pj_pool_factory *f, pj_size_t sz) 
     283{ 
     284    pj_caching_pool *cp = (pj_caching_pool*)f; 
     285 
     286    //Can't lock because mutex is not recursive 
     287    //if (cp->mutex) pj_mutex_lock(cp->mutex); 
     288 
     289    cp->used_size += sz; 
     290    if (cp->used_size > cp->peak_used_size) 
     291        cp->peak_used_size = cp->used_size; 
     292 
     293    //if (cp->mutex) pj_mutex_unlock(cp->mutex); 
     294 
     295    return PJ_TRUE; 
     296} 
     297 
     298 
     299static void cpool_on_block_free(pj_pool_factory *f, pj_size_t sz) 
     300{ 
     301    pj_caching_pool *cp = (pj_caching_pool*)f; 
     302 
     303    //pj_mutex_lock(cp->mutex); 
     304    cp->used_size -= sz; 
     305    //pj_mutex_unlock(cp->mutex); 
     306} 
     307 
     308 
    276309#endif  /* PJ_HAS_POOL_ALT_API */ 
    277310 
  • pjproject/trunk/pjlib/src/pj/pool_policy_malloc.c

    r364 r594  
    3131static void *default_block_alloc(pj_pool_factory *factory, pj_size_t size) 
    3232{ 
     33    void *p; 
     34 
    3335    PJ_CHECK_STACK(); 
    34     PJ_UNUSED_ARG(factory); 
    35     PJ_UNUSED_ARG(size); 
    3636 
    37     return malloc(size); 
     37    if (factory->on_block_alloc) { 
     38        int rc; 
     39        rc = factory->on_block_alloc(factory, size); 
     40        if (!rc) 
     41            return NULL; 
     42    } 
     43 
     44    p = malloc(size); 
     45 
     46    if (p == NULL) { 
     47        if (factory->on_block_free)  
     48            factory->on_block_free(factory, size); 
     49    } 
     50 
     51    return p; 
    3852} 
    3953 
    40 static void default_block_free(pj_pool_factory *factory, void *mem, pj_size_t size) 
     54static void default_block_free(pj_pool_factory *factory, void *mem,  
     55                               pj_size_t size) 
    4156{ 
    4257    PJ_CHECK_STACK(); 
    43     PJ_UNUSED_ARG(factory); 
    44     PJ_UNUSED_ARG(size); 
     58 
     59    if (factory->on_block_free)  
     60        factory->on_block_free(factory, size); 
    4561 
    4662    free(mem); 
Note: See TracChangeset for help on using the changeset viewer.