From ab9c3ac5334c2e0666a43da968fa175aaea7f281 Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Fri, 16 Feb 2007 21:44:36 +0000 Subject: Fixed ticket #105: unnecessary assert in fixed buffer based pool (pool_buf) on no memory condition git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@953 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/src/pj/pool.c | 4 ++++ pjlib/src/pj/pool_buf.c | 13 +++++++++-- pjlib/src/pjlib-test/pool.c | 55 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) (limited to 'pjlib/src') diff --git a/pjlib/src/pj/pool.c b/pjlib/src/pj/pool.c index dccb9c1b..16081aa1 100644 --- a/pjlib/src/pj/pool.c +++ b/pjlib/src/pj/pool.c @@ -174,6 +174,10 @@ PJ_DEF(pj_pool_t*) pj_pool_create_int( pj_pool_factory *f, const char *name, PJ_CHECK_STACK(); + /* Size must be at least sizeof(pj_pool)+sizeof(pj_pool_block) */ + PJ_ASSERT_RETURN(initial_size >= sizeof(pj_pool_t)+sizeof(pj_pool_block), + NULL); + /* If callback is NULL, set calback from the policy */ if (callback == NULL) callback = f->policy.callback; diff --git a/pjlib/src/pj/pool_buf.c b/pjlib/src/pj/pool_buf.c index c373fa75..ce787ce0 100644 --- a/pjlib/src/pj/pool_buf.c +++ b/pjlib/src/pj/pool_buf.c @@ -53,17 +53,26 @@ static pj_status_t pool_buf_initialize() static void* stack_alloc(pj_pool_factory *factory, pj_size_t size) { struct creation_param *param; + void *buf; PJ_UNUSED_ARG(factory); param = pj_thread_local_get(tls); - PJ_ASSERT_RETURN(param != NULL, NULL); + if (param == NULL) { + /* Don't assert(), this is normal no-memory situation */ + return NULL; + } pj_thread_local_set(tls, NULL); PJ_ASSERT_RETURN(size <= param->size, NULL); - return param->stack_buf; + buf = param->stack_buf; + + /* Prevent the buffer from being reused */ + param->stack_buf = NULL; + + return buf; } diff --git a/pjlib/src/pjlib-test/pool.c b/pjlib/src/pjlib-test/pool.c index d48a3589..978faea4 100644 --- a/pjlib/src/pjlib-test/pool.c +++ b/pjlib/src/pjlib-test/pool.c @@ -17,8 +17,10 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include +#include #include #include +#include #include "test.h" /** @@ -139,6 +141,54 @@ on_error: return status; } +/* Test the buffer based pool */ +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]; + pj_pool_t *pool; + void *p; + PJ_USE_EXCEPTION; + + PJ_LOG(3,("test", "...pool_buf test")); + + pool = pj_pool_create_on_buf("no name", buf, sizeof(buf)); + if (!pool) + return -70; + + /* Drain the pool */ + PJ_TRY { + if ((p=pj_pool_alloc(pool, STATIC_BUF_SIZE/2)) == NULL) + return -75; + + if ((p=pj_pool_alloc(pool, STATIC_BUF_SIZE/2)) == NULL) + return -76; + } + PJ_CATCH_ANY { + return -77; + } + PJ_END; + + /* On the next alloc, exception should be thrown */ + PJ_TRY { + p = pj_pool_alloc(pool, STATIC_BUF_SIZE); + if (p != NULL) { + /* This is unexpected, the alloc should fail */ + return -78; + } + } + PJ_CATCH_ANY { + /* This is the expected result */ + } + PJ_END; + + /* Done */ + return 0; +} + + int pool_test(void) { enum { LOOP = 2 }; @@ -160,6 +210,11 @@ int pool_test(void) if (rc != -40) return rc; } + rc = pool_buf_test(); + if (rc != 0) + return rc; + + return 0; } -- cgit v1.2.3