Changeset 1333 for pjproject


Ignore:
Timestamp:
Jun 1, 2007 7:26:21 AM (17 years ago)
Author:
bennylp
Message:

Implement ticket #314: Added PJ_SAFE_POOL configuration in PJLIB to track down memory corruptions

Location:
pjproject/trunk/pjlib
Files:
1 added
4 edited

Legend:

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

    r1246 r1333  
    389389 
    390390/** 
     391 * Set this flag to non-zero to enable various checking for pool 
     392 * operations. When this flag is set, assertion must be enabled 
     393 * in the application. 
     394 * 
     395 * This will slow down pool creation and destruction and will add 
     396 * few bytes of overhead, so application would normally want to  
     397 * disable this feature on release build. 
     398 * 
     399 * Default: 0 
     400 */ 
     401#ifndef PJ_SAFE_POOL 
     402#   define PJ_SAFE_POOL             0 
     403#endif 
     404 
     405 
     406/** 
    391407 * If pool debugging is used, then each memory allocation from the pool 
    392408 * will call malloc(), and pool will release all memory chunks when it 
  • pjproject/trunk/pjlib/src/pj/pool_caching.c

    r1235 r1333  
    100100        pj_pool_t *next = pool->next; 
    101101        pj_list_erase(pool); 
     102        PJ_LOG(4,(pool->obj_name,  
     103                  "Pool is not released by application, releasing now")); 
    102104        pj_pool_destroy_int(pool); 
    103105        pool = next; 
     
    198200    PJ_CHECK_STACK(); 
    199201 
     202    PJ_ASSERT_ON_FAIL(pf && pool, return); 
     203 
    200204    pj_lock_acquire(cp->lock); 
     205 
     206#if PJ_SAFE_POOL 
     207    /* Make sure pool is still in our used list */ 
     208    if (pj_list_find_node(&cp->used_list, pool) != pool) { 
     209        pj_assert(!"Attempt to destroy pool that has been destroyed before"); 
     210        return; 
     211    } 
     212#endif 
    201213 
    202214    /* Erase from the used list. */ 
  • pjproject/trunk/pjlib/src/pj/pool_policy_malloc.c

    r974 r1333  
    2727 * This file contains pool default policy definition and implementation. 
    2828 */ 
     29#include "pool_signature.h" 
    2930 
    3031 
     
    4243    } 
    4344 
    44     p = malloc(size); 
     45    p = malloc(size+(SIG_SIZE << 1)); 
    4546 
    4647    if (p == NULL) { 
    4748        if (factory->on_block_free)  
    4849            factory->on_block_free(factory, size); 
     50    } else { 
     51        /* Apply signature when PJ_SAFE_POOL is set. It will move 
     52         * "p" pointer forward. 
     53         */ 
     54        APPLY_SIG(p, size); 
    4955    } 
    5056 
     
    5965    if (factory->on_block_free)  
    6066        factory->on_block_free(factory, size); 
     67 
     68    /* Check and remove signature when PJ_SAFE_POOL is set. It will 
     69     * move "mem" pointer backward. 
     70     */ 
     71    REMOVE_SIG(mem, size); 
     72 
     73    /* Note that when PJ_SAFE_POOL is set, the actual size of the block 
     74     * is size + SIG_SIZE*2. 
     75     */ 
    6176 
    6277    free(mem); 
  • pjproject/trunk/pjlib/src/pj/pool_policy_new.cpp

    r1235 r1333  
    2626 * This file contains pool default policy definition and implementation. 
    2727 */ 
     28#include "pool_signature.h" 
    2829  
    2930 
    3031static void *operator_new(pj_pool_factory *factory, pj_size_t size) 
    3132{ 
     33    void *mem; 
     34 
    3235    PJ_CHECK_STACK(); 
    3336    PJ_UNUSED_ARG(factory); 
    3437    PJ_UNUSED_ARG(size); 
    3538 
    36     return new char[size]; 
     39    mem = (void*) new char[size+(SIG_SIZE << 1)]; 
     40     
     41    /* Exception for new operator may be disabled, so.. */ 
     42    if (mem) { 
     43        /* Apply signature when PJ_SAFE_POOL is set. It will move 
     44         * "mem" pointer forward. 
     45         */ 
     46        APPLY_SIG(mem, size); 
     47    } 
     48 
     49    return mem; 
    3750} 
    3851 
     
    4255    PJ_UNUSED_ARG(factory); 
    4356    PJ_UNUSED_ARG(size); 
     57 
     58    /* Check and remove signature when PJ_SAFE_POOL is set. It will 
     59     * move "mem" pointer backward. 
     60     */ 
     61    REMOVE_SIG(mem, size); 
     62 
     63    /* Note that when PJ_SAFE_POOL is set, the actual size of the block 
     64     * is size + SIG_SIZE*2. 
     65     */ 
    4466 
    4567    char *p = (char*)mem; 
Note: See TracChangeset for help on using the changeset viewer.