diff options
Diffstat (limited to 'pjlib/src/pjlib-test')
-rw-r--r-- | pjlib/src/pjlib-test/pool.c | 114 | ||||
-rw-r--r-- | pjlib/src/pjlib-test/pool_perf.c | 6 |
2 files changed, 118 insertions, 2 deletions
diff --git a/pjlib/src/pjlib-test/pool.c b/pjlib/src/pjlib-test/pool.c index ce91ac6e..930e552e 100644 --- a/pjlib/src/pjlib-test/pool.c +++ b/pjlib/src/pjlib-test/pool.c @@ -79,6 +79,112 @@ static int capacity_test(void) return 0; } +/* Test that the alignment works. */ +static int pool_alignment_test(void) +{ + pj_pool_t *pool; + void *ptr; + + PJ_LOG(3,("test", "...alignment test")); + + pool = pj_pool_create(mem, NULL, PJ_POOL_SIZE+64, 64, 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; + } + + /* Test allocation after new block is created */ + ptr = pj_pool_alloc(pool, 127); + if (!IS_ALIGNED(ptr)) { + pj_pool_release(pool); + return -330; + } + + /* Reset the pool */ + pj_pool_reset(pool); + + /* 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; + } + + /* Done */ + pj_pool_release(pool); + + return 0; +} + +/* Test that the alignment works for pool on buf. */ +static int pool_buf_alignment_test(void) +{ + pj_pool_t *pool; + char buf[256]; + void *ptr; + + PJ_LOG(3,("test", "...pool_buf alignment test")); + + pool = pj_pool_create_on_buf(NULL, buf, sizeof(buf)); + 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); + + /* Retest first allocation */ + ptr = pj_pool_alloc(pool, 1); + if (!IS_ALIGNED(ptr)) { + pj_pool_release(pool); + return -430; + } + + /* Retest subsequent allocation */ + ptr = pj_pool_alloc(pool, 1); + if (!IS_ALIGNED(ptr)) { + pj_pool_release(pool); + return -440; + } + + /* Done */ + return 0; +} + /* Test function to drain the pool's space. */ static int drain_test(pj_size_t size, pj_size_t increment) @@ -149,7 +255,7 @@ static int pool_buf_test(void) enum { STATIC_BUF_SIZE = 40 }; /* 16 is the internal struct in pool_buf */ static char buf[ STATIC_BUF_SIZE + sizeof(pj_pool_t) + - sizeof(pj_pool_block) + 16]; + sizeof(pj_pool_block) + 2 * PJ_POOL_ALIGNMENT]; pj_pool_t *pool; void *p; PJ_USE_EXCEPTION; @@ -200,6 +306,12 @@ int pool_test(void) rc = capacity_test(); if (rc) return rc; + rc = pool_alignment_test(); + if (rc) return rc; + + rc = pool_buf_alignment_test(); + if (rc) return rc; + for (loop=0; loop<LOOP; ++loop) { /* Test that the pool should grow automaticly. */ rc = drain_test(SIZE, SIZE); diff --git a/pjlib/src/pjlib-test/pool_perf.c b/pjlib/src/pjlib-test/pool_perf.c index 8bd5635f..d73f7a5b 100644 --- a/pjlib/src/pjlib-test/pool_perf.c +++ b/pjlib/src/pjlib-test/pool_perf.c @@ -127,8 +127,12 @@ int pool_perf_test() /* Initialize size of chunks to allocate in for the test. */ for (i=0; i<COUNT; ++i) { + unsigned aligned_size; sizes[i] = MIN_SIZE + (pj_rand() % MAX_SIZE); - total_size += sizes[i]; + aligned_size = sizes[i]; + if (aligned_size & (PJ_POOL_ALIGNMENT-1)) + aligned_size = ((aligned_size + PJ_POOL_ALIGNMENT - 1)) & ~(PJ_POOL_ALIGNMENT - 1); + total_size += aligned_size; } /* Add some more for pool admin area */ |