From 3cf609b42e573adf8e7183070176a450a7b4959e Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Thu, 30 Mar 2006 15:56:01 +0000 Subject: Added ability to have custom pool backend (needed for pool debugging facility) git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@364 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/src/pj/config.c | 4 +- pjlib/src/pj/pool.c | 5 + pjlib/src/pj/pool_caching.c | 31 +++++-- pjlib/src/pj/pool_dbg.c | 191 ++++++++++++++++++++++++++++++++++++++ pjlib/src/pj/pool_policy_malloc.c | 4 + 5 files changed, 224 insertions(+), 11 deletions(-) create mode 100644 pjlib/src/pj/pool_dbg.c (limited to 'pjlib/src') diff --git a/pjlib/src/pj/config.c b/pjlib/src/pj/config.c index 7c2d2d96..f4bff393 100644 --- a/pjlib/src/pj/config.c +++ b/pjlib/src/pj/config.c @@ -21,7 +21,7 @@ #include static const char *id = "config.c"; -const char *PJ_VERSION = "0.5.4.1"; +const char *PJ_VERSION = "0.5.4.5"; PJ_DEF(void) pj_dump_config(void) { @@ -43,10 +43,12 @@ PJ_DEF(void) pj_dump_config(void) PJ_LOG(3, (id, " PJ_LOG_MAX_SIZE : %d", PJ_LOG_MAX_SIZE)); PJ_LOG(3, (id, " PJ_LOG_USE_STACK_BUFFER : %d", PJ_LOG_USE_STACK_BUFFER)); PJ_LOG(3, (id, " PJ_POOL_DEBUG : %d", PJ_POOL_DEBUG)); + PJ_LOG(3, (id, " PJ_HAS_POOL_ALT_API : %d", PJ_HAS_POOL_ALT_API)); PJ_LOG(3, (id, " PJ_HAS_TCP : %d", PJ_HAS_TCP)); PJ_LOG(3, (id, " PJ_MAX_HOSTNAME : %d", PJ_MAX_HOSTNAME)); PJ_LOG(3, (id, " ioqueue type : %s", pj_ioqueue_name())); PJ_LOG(3, (id, " PJ_IOQUEUE_MAX_HANDLES : %d", PJ_IOQUEUE_MAX_HANDLES)); + PJ_LOG(3, (id, " PJ_IOQUEUE_HAS_SAFE_UNREG : %d", PJ_IOQUEUE_HAS_SAFE_UNREG)); PJ_LOG(3, (id, " PJ_HAS_THREADS : %d", PJ_HAS_THREADS)); PJ_LOG(3, (id, " PJ_LOG_USE_STACK_BUFFER : %d", PJ_LOG_USE_STACK_BUFFER)); PJ_LOG(3, (id, " PJ_HAS_SEMAPHORE : %d", PJ_HAS_SEMAPHORE)); diff --git a/pjlib/src/pj/pool.c b/pjlib/src/pj/pool.c index 4cda9f4a..e5412d4b 100644 --- a/pjlib/src/pj/pool.c +++ b/pjlib/src/pj/pool.c @@ -22,6 +22,9 @@ #include #include +#if !PJ_HAS_POOL_ALT_API + + /* Include inline definitions when inlining is disabled. */ #if !PJ_FUNCTIONS_ARE_INLINED # include @@ -261,3 +264,5 @@ PJ_DEF(void) pj_pool_destroy_int(pj_pool_t *pool) } +#endif /* PJ_HAS_POOL_ALT_API */ + diff --git a/pjlib/src/pj/pool_caching.c b/pjlib/src/pj/pool_caching.c index 324ad05d..4f9904e8 100644 --- a/pjlib/src/pj/pool_caching.c +++ b/pjlib/src/pj/pool_caching.c @@ -22,6 +22,8 @@ #include #include +#if !PJ_HAS_POOL_ALT_API + static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, const char *name, pj_size_t initial_size, @@ -163,6 +165,7 @@ static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool) { pj_caching_pool *cp = (pj_caching_pool*)pf; + unsigned pool_capacity; int i; PJ_CHECK_STACK(); @@ -175,12 +178,14 @@ static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool) /* Decrement used count. */ --cp->used_count; + pool_capacity = pj_pool_get_capacity(pool); + /* Destroy the pool if the size is greater than our size or if the total * capacity in our recycle list (plus the size of the pool) exceeds * maximum capacity. . */ - if (pool->capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] || - cp->capacity + pool->capacity > cp->max_capacity) + if (pool_capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] || + cp->capacity + pool_capacity > cp->max_capacity) { pj_pool_destroy_int(pool); pj_mutex_unlock(cp->mutex); @@ -189,14 +194,14 @@ static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool) /* Reset pool. */ PJ_LOG(6, (pool->obj_name, "recycle(): cap=%d, used=%d(%d%%)", - pool->capacity, pj_pool_get_used_size(pool), - pj_pool_get_used_size(pool)*100/pool->capacity)); + pool_capacity, pj_pool_get_used_size(pool), + pj_pool_get_used_size(pool)*100/pool_capacity)); pj_pool_reset(pool); /* * Otherwise put the pool in our recycle list. */ - for (i=0; i < PJ_CACHING_POOL_ARRAY_SIZE && pool_sizes[i] != pool->capacity; ++i) + for (i=0; i < PJ_CACHING_POOL_ARRAY_SIZE && pool_sizes[i] != pool_capacity; ++i) ; pj_assert( i != PJ_CACHING_POOL_ARRAY_SIZE ); @@ -208,7 +213,7 @@ static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool) } pj_list_insert_after(&cp->free_list[i], pool); - cp->capacity += pool->capacity; + cp->capacity += pool_capacity; pj_mutex_unlock(cp->mutex); } @@ -228,11 +233,14 @@ static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail ) pj_uint32_t total_used = 0, total_capacity = 0; PJ_LOG(3,("cachpool", " Dumping all active pools:")); while (pool != (void*)&cp->used_list) { - PJ_LOG(3,("cachpool", " %12s: %8d of %8d (%d%%) used", pool->obj_name, - pj_pool_get_used_size(pool), pool->capacity, - pj_pool_get_used_size(pool)*100/pool->capacity)); + unsigned pool_capacity = pj_pool_get_capacity(pool); + PJ_LOG(3,("cachpool", " %12s: %8d of %8d (%d%%) used", + pj_pool_getobjname(pool), + pj_pool_get_used_size(pool), + pool_capacity, + pj_pool_get_used_size(pool)*100/pool_capacity)); total_used += pj_pool_get_used_size(pool); - total_capacity += pool->capacity; + total_capacity += pool_capacity; pool = pool->next; } PJ_LOG(3,("cachpool", " Total %9d of %9d (%d %%) used!", @@ -246,3 +254,6 @@ static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail ) PJ_UNUSED_ARG(detail); #endif } + +#endif /* PJ_HAS_POOL_ALT_API */ + diff --git a/pjlib/src/pj/pool_dbg.c b/pjlib/src/pj/pool_dbg.c new file mode 100644 index 00000000..550fcdc5 --- /dev/null +++ b/pjlib/src/pj/pool_dbg.c @@ -0,0 +1,191 @@ +/* $Id$ */ +/* + * Copyright (C)2003-2006 Benny Prijono + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +#include + +#if PJ_POOL_DEBUG + +#if PJ_HAS_MALLOC_H +# include +#endif + + +#if PJ_HAS_STDLIB_H +# include +#endif + + +#if defined(PJ_WIN32) && PJ_WIN32!=0 && defined(PJ_DEBUG) && PJ_DEBUG!=0 \ + && !PJ_NATIVE_STRING_IS_UNICODE +# include +# define TRACE_(msg) OutputDebugString(msg) +#endif + +/* Uncomment this to enable TRACE_ */ +//#undef TRACE_ + + +struct pj_pool_mem +{ + struct pj_pool_mem *next; + + /* data follows immediately */ +}; + + +struct pj_pool_t +{ + struct pj_pool_mem *first_mem; + pj_size_t used_size; + pj_pool_callback *cb; +}; + + +int PJ_NO_MEMORY_EXCEPTION; + + +/* Create pool */ +PJ_DEF(pj_pool_t*) pj_pool_create_imp( const char *file, int line, + void *factory, + const char *name, + pj_size_t initial_size, + pj_size_t increment_size, + pj_pool_callback *callback) +{ + pj_pool_t *pool; + + PJ_UNUSED_ARG(file); + PJ_UNUSED_ARG(line); + PJ_UNUSED_ARG(factory); + PJ_UNUSED_ARG(name); + PJ_UNUSED_ARG(initial_size); + PJ_UNUSED_ARG(increment_size); + + pool = malloc(sizeof(struct pj_pool_t)); + if (!pool) + return NULL; + + pool->first_mem = NULL; + pool->used_size = 0; + pool->cb = callback; + + return pool; +} + + +/* Release pool */ +PJ_DEF(void) pj_pool_release_imp(pj_pool_t *pool) +{ + pj_pool_reset(pool); + free(pool); +} + +/* Get pool name */ +PJ_DEF(const char*) pj_pool_getobjname_imp(pj_pool_t *pool) +{ + PJ_UNUSED_ARG(pool); + return "pooldbg"; +} + +/* Reset pool */ +PJ_DEF(void) pj_pool_reset_imp(pj_pool_t *pool) +{ + struct pj_pool_mem *mem; + + mem = pool->first_mem; + while (mem) { + struct pj_pool_mem *next = mem->next; + free(mem); + mem = next; + } + + pool->first_mem = NULL; +} + +/* Get capacity */ +PJ_DEF(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool) +{ + PJ_UNUSED_ARG(pool); + + /* Unlimited capacity */ + return 0x7FFFFFFFUL; +} + +/* Get total used size */ +PJ_DEF(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool) +{ + return pool->used_size; +} + +/* Allocate memory from the pool */ +PJ_DEF(void*) pj_pool_alloc_imp( const char *file, int line, + pj_pool_t *pool, pj_size_t sz) +{ + struct pj_pool_mem *mem; + + PJ_UNUSED_ARG(file); + PJ_UNUSED_ARG(line); + + mem = malloc(sz + sizeof(struct pj_pool_mem)); + if (!mem) { + if (pool->cb) + (*pool->cb)(pool, sz); + return NULL; + } + + mem->next = pool->first_mem; + pool->first_mem = mem; + +#ifdef TRACE_ + { + char msg[120]; + pj_ansi_sprintf(msg, "Mem %X (%d+%d bytes) allocated by %s:%d\r\n", + mem, sz, sizeof(struct pj_pool_mem), + file, line); + TRACE_(msg); + } +#endif + + return ((char*)mem) + sizeof(struct pj_pool_mem); +} + +/* Allocate memory from the pool and zero the memory */ +PJ_DEF(void*) pj_pool_calloc_imp( const char *file, int line, + pj_pool_t *pool, unsigned cnt, + unsigned elemsz) +{ + void *mem; + + mem = pj_pool_alloc_imp(file, line, pool, cnt*elemsz); + if (!mem) + return NULL; + + pj_memset(mem, 0, cnt*elemsz); + return mem; +} + +/* Allocate memory from the pool and zero the memory */ +PJ_DEF(void*) pj_pool_zalloc_imp( const char *file, int line, + pj_pool_t *pool, pj_size_t sz) +{ + return pj_pool_calloc_imp(file, line, pool, 1, sz); +} + + +#endif /* PJ_POOL_DEBUG */ diff --git a/pjlib/src/pj/pool_policy_malloc.c b/pjlib/src/pj/pool_policy_malloc.c index 05aca5bb..7387bec4 100644 --- a/pjlib/src/pj/pool_policy_malloc.c +++ b/pjlib/src/pj/pool_policy_malloc.c @@ -21,6 +21,8 @@ #include #include +#if !PJ_HAS_POOL_ALT_API + /* * This file contains pool default policy definition and implementation. */ @@ -60,3 +62,5 @@ pj_pool_factory_policy pj_pool_factory_default_policy = &default_pool_callback, 0 }; + +#endif /* PJ_HAS_POOL_ALT_API */ -- cgit v1.2.3