summaryrefslogtreecommitdiff
path: root/pjlib
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2010-01-29 11:20:43 +0000
committerBenny Prijono <bennylp@teluu.com>2010-01-29 11:20:43 +0000
commitf3d584101fb40816718221330df4a729080340f9 (patch)
tree6e72ab8abaea21fd2da18b45e23008f9c880e132 /pjlib
parent8e54a92b6eb6302761fd9a1949086baa26e0007a (diff)
More ticket #1037:
- bug in aligning pointer if sizeof(long) is less than sizeof(void*). Thanks John Ridges for pointing this out git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@3082 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib')
-rw-r--r--pjlib/src/pj/pool.c15
-rw-r--r--pjlib/src/pjlib-test/pool.c104
2 files changed, 47 insertions, 72 deletions
diff --git a/pjlib/src/pj/pool.c b/pjlib/src/pj/pool.c
index 22df21c6..800b86c6 100644
--- a/pjlib/src/pj/pool.c
+++ b/pjlib/src/pj/pool.c
@@ -31,7 +31,8 @@
# include <pj/pool_i.h>
#endif
-#define LOG(expr) PJ_LOG(6,expr)
+#define LOG(expr) PJ_LOG(6,expr)
+#define ALIGN_PTR(PTR,ALIGNMENT) (PTR + (-(long)(PTR) & (ALIGNMENT-1)))
PJ_DEF_DATA(int) PJ_NO_MEMORY_EXCEPTION;
@@ -71,9 +72,7 @@ static pj_pool_block *pj_pool_create_block( pj_pool_t *pool, pj_size_t size)
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));
+ block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT);
/* Insert in the front of the list. */
pj_list_insert_after(&pool->block_list, block);
@@ -216,9 +215,7 @@ PJ_DEF(pj_pool_t*) pj_pool_create_int( pj_pool_factory *f, const char *name,
block->end = buffer + initial_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));
+ block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT);
pj_list_insert_after(&pool->block_list, block);
@@ -262,9 +259,7 @@ static void reset_pool(pj_pool_t *pool)
block = pool->block_list.next;
/* Set the start pointer, aligning it as needed */
- block->cur = (unsigned char*)
- (((unsigned long)block->buf + PJ_POOL_ALIGNMENT - 1) &
- ~(PJ_POOL_ALIGNMENT - 1));
+ block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT);
pool->capacity = block->end - (unsigned char*)pool;
}
diff --git a/pjlib/src/pjlib-test/pool.c b/pjlib/src/pjlib-test/pool.c
index 930e552e..21b2205a 100644
--- a/pjlib/src/pjlib-test/pool.c
+++ b/pjlib/src/pjlib-test/pool.c
@@ -84,51 +84,41 @@ static int pool_alignment_test(void)
{
pj_pool_t *pool;
void *ptr;
+ enum { MEMSIZE = 64, LOOP = 100 };
+ unsigned i;
PJ_LOG(3,("test", "...alignment test"));
- pool = pj_pool_create(mem, NULL, PJ_POOL_SIZE+64, 64, NULL);
+ pool = pj_pool_create(mem, NULL, PJ_POOL_SIZE+MEMSIZE, MEMSIZE, NULL);
if (!pool)
return -300;
#define IS_ALIGNED(p) ((((unsigned long)p) & (PJ_POOL_ALIGNMENT-1)) == 0)
- /* Test first allocation */
- ptr = pj_pool_alloc(pool, 1);
- if (!IS_ALIGNED(ptr)) {
- pj_pool_release(pool);
- return -310;
- }
-
- /* Test subsequent allocation */
- ptr = pj_pool_alloc(pool, 1);
- if (!IS_ALIGNED(ptr)) {
- pj_pool_release(pool);
- return -320;
- }
+ for (i=0; i<LOOP; ++i) {
+ /* Test first allocation */
+ ptr = pj_pool_alloc(pool, 1);
+ if (!IS_ALIGNED(ptr)) {
+ pj_pool_release(pool);
+ return -310;
+ }
- /* Test allocation after new block is created */
- ptr = pj_pool_alloc(pool, 127);
- if (!IS_ALIGNED(ptr)) {
- pj_pool_release(pool);
- return -330;
- }
+ /* Test subsequent allocation */
+ ptr = pj_pool_alloc(pool, 1);
+ if (!IS_ALIGNED(ptr)) {
+ pj_pool_release(pool);
+ return -320;
+ }
- /* Reset the pool */
- pj_pool_reset(pool);
+ /* Test allocation after new block is created */
+ ptr = pj_pool_alloc(pool, MEMSIZE*2+1);
+ if (!IS_ALIGNED(ptr)) {
+ pj_pool_release(pool);
+ return -330;
+ }
- /* Retest first allocation */
- ptr = pj_pool_alloc(pool, 1);
- if (!IS_ALIGNED(ptr)) {
- pj_pool_release(pool);
- return -340;
- }
-
- /* Retest subsequent allocation */
- ptr = pj_pool_alloc(pool, 1);
- if (!IS_ALIGNED(ptr)) {
- pj_pool_release(pool);
- return -350;
+ /* Reset the pool */
+ pj_pool_reset(pool);
}
/* Done */
@@ -141,8 +131,10 @@ static int pool_alignment_test(void)
static int pool_buf_alignment_test(void)
{
pj_pool_t *pool;
- char buf[256];
+ char buf[512];
void *ptr;
+ enum { LOOP = 100 };
+ unsigned i;
PJ_LOG(3,("test", "...pool_buf alignment test"));
@@ -150,35 +142,23 @@ static int pool_buf_alignment_test(void)
if (!pool)
return -400;
- /* Test first allocation */
- ptr = pj_pool_alloc(pool, 1);
- if (!IS_ALIGNED(ptr)) {
- pj_pool_release(pool);
- return -410;
- }
-
- /* Test subsequent allocation */
- ptr = pj_pool_alloc(pool, 1);
- if (!IS_ALIGNED(ptr)) {
- pj_pool_release(pool);
- return -420;
- }
-
- /* Reset the pool */
- pj_pool_reset(pool);
+ for (i=0; i<LOOP; ++i) {
+ /* Test first allocation */
+ ptr = pj_pool_alloc(pool, 1);
+ if (!IS_ALIGNED(ptr)) {
+ pj_pool_release(pool);
+ return -410;
+ }
- /* Retest first allocation */
- ptr = pj_pool_alloc(pool, 1);
- if (!IS_ALIGNED(ptr)) {
- pj_pool_release(pool);
- return -430;
- }
+ /* Test subsequent allocation */
+ ptr = pj_pool_alloc(pool, 1);
+ if (!IS_ALIGNED(ptr)) {
+ pj_pool_release(pool);
+ return -420;
+ }
- /* Retest subsequent allocation */
- ptr = pj_pool_alloc(pool, 1);
- if (!IS_ALIGNED(ptr)) {
- pj_pool_release(pool);
- return -440;
+ /* Reset the pool */
+ pj_pool_reset(pool);
}
/* Done */