Changeset 3081


Ignore:
Timestamp:
Jan 29, 2010 10:10:49 AM (14 years ago)
Author:
bennylp
Message:

Fixed ticket #1037: Memory pool alignment error when alignment is set to be greater than the default (thanks John Ridges for the report):

  • fixed the pool allocation routines in PJLIB,
  • add alignment test in pjlib-test (only useful if PJ_POOL_ALIGNMENT is configured in config_site.h),
  • fixed other pool tests in pjlib-test which are broken when PJ_POOL_ALIGNMENT is enlarged
Location:
pjproject/trunk/pjlib/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/pool.c

    r2963 r3081  
    213213    /* Create the first block from the memory. */ 
    214214    block = (pj_pool_block*) (buffer + sizeof(*pool)); 
    215     block->cur = block->buf = ((unsigned char*)block) + sizeof(pj_pool_block); 
     215    block->buf = ((unsigned char*)block) + sizeof(pj_pool_block); 
    216216    block->end = buffer + initial_size; 
     217 
     218    /* Set the start pointer, aligning it as needed */ 
     219    block->cur = (unsigned char*) 
     220                 (((unsigned long)block->buf + PJ_POOL_ALIGNMENT - 1) & 
     221                  ~(PJ_POOL_ALIGNMENT - 1)); 
     222 
    217223    pj_list_insert_after(&pool->block_list, block); 
    218224 
     
    255261 
    256262    block = pool->block_list.next; 
    257     block->cur = block->buf; 
     263 
     264    /* Set the start pointer, aligning it as needed */ 
     265    block->cur = (unsigned char*) 
     266                 (((unsigned long)block->buf + PJ_POOL_ALIGNMENT - 1) & 
     267                  ~(PJ_POOL_ALIGNMENT - 1)); 
     268 
    258269    pool->capacity = block->end - (unsigned char*)pool; 
    259270} 
  • pjproject/trunk/pjlib/src/pjlib-test/pool.c

    r2394 r3081  
    7777 
    7878    pj_pool_release(pool); 
     79    return 0; 
     80} 
     81 
     82/* Test that the alignment works. */ 
     83static int pool_alignment_test(void) 
     84{ 
     85    pj_pool_t *pool; 
     86    void *ptr; 
     87 
     88    PJ_LOG(3,("test", "...alignment test")); 
     89 
     90    pool = pj_pool_create(mem, NULL, PJ_POOL_SIZE+64, 64, NULL); 
     91    if (!pool) 
     92        return -300; 
     93 
     94#define IS_ALIGNED(p)   ((((unsigned long)p) & (PJ_POOL_ALIGNMENT-1)) == 0) 
     95 
     96    /* Test first allocation */ 
     97    ptr = pj_pool_alloc(pool, 1); 
     98    if (!IS_ALIGNED(ptr)) { 
     99        pj_pool_release(pool); 
     100        return -310; 
     101    } 
     102 
     103    /* Test subsequent allocation */ 
     104    ptr = pj_pool_alloc(pool, 1); 
     105    if (!IS_ALIGNED(ptr)) { 
     106        pj_pool_release(pool); 
     107        return -320; 
     108    } 
     109 
     110    /* Test allocation after new block is created */ 
     111    ptr = pj_pool_alloc(pool, 127); 
     112    if (!IS_ALIGNED(ptr)) { 
     113        pj_pool_release(pool); 
     114        return -330; 
     115    } 
     116 
     117    /* Reset the pool */ 
     118    pj_pool_reset(pool); 
     119 
     120    /* Retest first allocation */ 
     121    ptr = pj_pool_alloc(pool, 1); 
     122    if (!IS_ALIGNED(ptr)) { 
     123        pj_pool_release(pool); 
     124        return -340; 
     125    } 
     126 
     127    /* Retest subsequent allocation */ 
     128    ptr = pj_pool_alloc(pool, 1); 
     129    if (!IS_ALIGNED(ptr)) { 
     130        pj_pool_release(pool); 
     131        return -350; 
     132    } 
     133 
     134    /* Done */ 
     135    pj_pool_release(pool); 
     136 
     137    return 0; 
     138} 
     139 
     140/* Test that the alignment works for pool on buf. */ 
     141static int pool_buf_alignment_test(void) 
     142{ 
     143    pj_pool_t *pool; 
     144    char buf[256]; 
     145    void *ptr; 
     146 
     147    PJ_LOG(3,("test", "...pool_buf alignment test")); 
     148 
     149    pool = pj_pool_create_on_buf(NULL, buf, sizeof(buf)); 
     150    if (!pool) 
     151        return -400; 
     152 
     153    /* Test first allocation */ 
     154    ptr = pj_pool_alloc(pool, 1); 
     155    if (!IS_ALIGNED(ptr)) { 
     156        pj_pool_release(pool); 
     157        return -410; 
     158    } 
     159 
     160    /* Test subsequent allocation */ 
     161    ptr = pj_pool_alloc(pool, 1); 
     162    if (!IS_ALIGNED(ptr)) { 
     163        pj_pool_release(pool); 
     164        return -420; 
     165    } 
     166 
     167    /* Reset the pool */ 
     168    pj_pool_reset(pool); 
     169 
     170    /* Retest first allocation */ 
     171    ptr = pj_pool_alloc(pool, 1); 
     172    if (!IS_ALIGNED(ptr)) { 
     173        pj_pool_release(pool); 
     174        return -430; 
     175    } 
     176 
     177    /* Retest subsequent allocation */ 
     178    ptr = pj_pool_alloc(pool, 1); 
     179    if (!IS_ALIGNED(ptr)) { 
     180        pj_pool_release(pool); 
     181        return -440; 
     182    } 
     183 
     184    /* Done */ 
    79185    return 0; 
    80186} 
     
    150256    /* 16 is the internal struct in pool_buf */ 
    151257    static char buf[ STATIC_BUF_SIZE + sizeof(pj_pool_t) +  
    152                      sizeof(pj_pool_block) + 16]; 
     258                     sizeof(pj_pool_block) + 2 * PJ_POOL_ALIGNMENT]; 
    153259    pj_pool_t *pool; 
    154260    void *p; 
     
    201307    if (rc) return rc; 
    202308 
     309    rc = pool_alignment_test(); 
     310    if (rc) return rc; 
     311 
     312    rc = pool_buf_alignment_test(); 
     313    if (rc) return rc; 
     314 
    203315    for (loop=0; loop<LOOP; ++loop) { 
    204316        /* Test that the pool should grow automaticly. */ 
  • pjproject/trunk/pjlib/src/pjlib-test/pool_perf.c

    r2394 r3081  
    128128    /* Initialize size of chunks to allocate in for the test. */ 
    129129    for (i=0; i<COUNT; ++i) { 
     130        unsigned aligned_size; 
    130131        sizes[i] = MIN_SIZE + (pj_rand() % MAX_SIZE); 
    131         total_size += sizes[i]; 
     132        aligned_size = sizes[i]; 
     133        if (aligned_size & (PJ_POOL_ALIGNMENT-1)) 
     134            aligned_size = ((aligned_size + PJ_POOL_ALIGNMENT - 1)) & ~(PJ_POOL_ALIGNMENT - 1); 
     135        total_size += aligned_size; 
    132136    } 
    133137 
Note: See TracChangeset for help on using the changeset viewer.