Changeset 3082


Ignore:
Timestamp:
01/29/10 05:20:43 (9 years ago)
Author:
bennylp
Message:

More ticket #1037:

  • bug in aligning pointer if sizeof(long) is less than sizeof(void*). Thanks John Ridges for pointing this out
Location:
pjproject/trunk/pjlib/src
Files:
2 edited

Legend:

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

    r3081 r3082  
    3232#endif 
    3333 
    34 #define LOG(expr)   PJ_LOG(6,expr) 
     34#define LOG(expr)                       PJ_LOG(6,expr) 
     35#define ALIGN_PTR(PTR,ALIGNMENT)        (PTR + (-(long)(PTR) & (ALIGNMENT-1))) 
    3536 
    3637PJ_DEF_DATA(int) PJ_NO_MEMORY_EXCEPTION; 
     
    7273 
    7374    /* Set the start pointer, aligning it as needed */ 
    74     block->cur = (unsigned char*) 
    75                  (((unsigned long)block->buf + PJ_POOL_ALIGNMENT - 1) &  
    76                   ~(PJ_POOL_ALIGNMENT - 1)); 
     75    block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT); 
    7776 
    7877    /* Insert in the front of the list. */ 
     
    217216 
    218217    /* 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)); 
     218    block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT); 
    222219 
    223220    pj_list_insert_after(&pool->block_list, block); 
     
    263260 
    264261    /* 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)); 
     262    block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT); 
    268263 
    269264    pool->capacity = block->end - (unsigned char*)pool; 
  • pjproject/trunk/pjlib/src/pjlib-test/pool.c

    r3081 r3082  
    8585    pj_pool_t *pool; 
    8686    void *ptr; 
     87    enum { MEMSIZE = 64, LOOP = 100 }; 
     88    unsigned i; 
    8789 
    8890    PJ_LOG(3,("test", "...alignment test")); 
    8991 
    90     pool = pj_pool_create(mem, NULL, PJ_POOL_SIZE+64, 64, NULL); 
     92    pool = pj_pool_create(mem, NULL, PJ_POOL_SIZE+MEMSIZE, MEMSIZE, NULL); 
    9193    if (!pool) 
    9294        return -300; 
     
    9496#define IS_ALIGNED(p)   ((((unsigned long)p) & (PJ_POOL_ALIGNMENT-1)) == 0) 
    9597 
    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; 
     98    for (i=0; i<LOOP; ++i) { 
     99        /* Test first allocation */ 
     100        ptr = pj_pool_alloc(pool, 1); 
     101        if (!IS_ALIGNED(ptr)) { 
     102            pj_pool_release(pool); 
     103            return -310; 
     104        } 
     105 
     106        /* Test subsequent allocation */ 
     107        ptr = pj_pool_alloc(pool, 1); 
     108        if (!IS_ALIGNED(ptr)) { 
     109            pj_pool_release(pool); 
     110            return -320; 
     111        } 
     112 
     113        /* Test allocation after new block is created */ 
     114        ptr = pj_pool_alloc(pool, MEMSIZE*2+1); 
     115        if (!IS_ALIGNED(ptr)) { 
     116            pj_pool_release(pool); 
     117            return -330; 
     118        } 
     119 
     120        /* Reset the pool */ 
     121        pj_pool_reset(pool); 
    132122    } 
    133123 
     
    142132{ 
    143133    pj_pool_t *pool; 
    144     char buf[256]; 
     134    char buf[512]; 
    145135    void *ptr; 
     136    enum { LOOP = 100 }; 
     137    unsigned i; 
    146138 
    147139    PJ_LOG(3,("test", "...pool_buf alignment test")); 
     
    151143        return -400; 
    152144 
    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; 
     145    for (i=0; i<LOOP; ++i) { 
     146        /* Test first allocation */ 
     147        ptr = pj_pool_alloc(pool, 1); 
     148        if (!IS_ALIGNED(ptr)) { 
     149            pj_pool_release(pool); 
     150            return -410; 
     151        } 
     152 
     153        /* Test subsequent allocation */ 
     154        ptr = pj_pool_alloc(pool, 1); 
     155        if (!IS_ALIGNED(ptr)) { 
     156            pj_pool_release(pool); 
     157            return -420; 
     158        } 
     159 
     160        /* Reset the pool */ 
     161        pj_pool_reset(pool); 
    182162    } 
    183163 
Note: See TracChangeset for help on using the changeset viewer.