summaryrefslogtreecommitdiff
path: root/pjlib
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2009-10-24 02:06:40 +0000
committerBenny Prijono <bennylp@teluu.com>2009-10-24 02:06:40 +0000
commitbb3691a6a11378599885cc42e65c41bbcae1b787 (patch)
tree02eee26243a480672963d6b65bf822b160ee76ef /pjlib
parentaf73c1c337f9831cf66bc980333b390340f71e39 (diff)
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
Diffstat (limited to 'pjlib')
-rw-r--r--pjlib/src/pj/pool.c19
1 files changed, 14 insertions, 5 deletions
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;