From bb3691a6a11378599885cc42e65c41bbcae1b787 Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Sat, 24 Oct 2009 02:06:40 +0000 Subject: Fixed ticket #980: Memory pool alignment error when alignment is set to be greater than 4 bytes (thanks John Ridges for the report) git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2963 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/src/pj/pool.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'pjlib/src') diff --git a/pjlib/src/pj/pool.c b/pjlib/src/pj/pool.c index dcded853..5b022a8c 100644 --- a/pjlib/src/pj/pool.c +++ b/pjlib/src/pj/pool.c @@ -66,10 +66,15 @@ static pj_pool_block *pj_pool_create_block( pj_pool_t *pool, pj_size_t size) /* Add capacity. */ pool->capacity += size; - /* Set block attribytes. */ - block->cur = block->buf = ((unsigned char*)block) + sizeof(pj_pool_block); + /* Set start and end of buffer. */ + block->buf = ((unsigned char*)block) + sizeof(pj_pool_block); block->end = ((unsigned char*)block) + size; + /* Set the start pointer, aligning it as needed */ + block->cur = (unsigned char*) + (((unsigned long)block->buf + PJ_POOL_ALIGNMENT - 1) & + ~(PJ_POOL_ALIGNMENT - 1)); + /* Insert in the front of the list. */ pj_list_insert_after(&pool->block_list, block); @@ -111,11 +116,15 @@ PJ_DEF(void*) pj_pool_allocate_find(pj_pool_t *pool, unsigned size) /* If pool is configured to expand, but the increment size * is less than the required size, expand the pool by multiple - * increment size + * increment size. Also count the size wasted due to aligning + * the block. */ - if (pool->increment_size < size + sizeof(pj_pool_block)) { + if (pool->increment_size < + size + sizeof(pj_pool_block) + PJ_POOL_ALIGNMENT) + { unsigned count; - count = (size + pool->increment_size + sizeof(pj_pool_block)) / + count = (size + pool->increment_size + sizeof(pj_pool_block) + + PJ_POOL_ALIGNMENT) / pool->increment_size; block_size = count * pool->increment_size; -- cgit v1.2.3