diff options
author | Benny Prijono <bennylp@teluu.com> | 2006-03-30 15:56:01 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2006-03-30 15:56:01 +0000 |
commit | 3cf609b42e573adf8e7183070176a450a7b4959e (patch) | |
tree | 3a34159d7b023de27b99675520ab8f82e4245027 /pjlib/src/pj/pool_dbg.c | |
parent | e985f59aabf893535e0d12c5867e44d69ccb83fe (diff) |
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
Diffstat (limited to 'pjlib/src/pj/pool_dbg.c')
-rw-r--r-- | pjlib/src/pj/pool_dbg.c | 191 |
1 files changed, 191 insertions, 0 deletions
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 <benny@prijono.org> + * + * 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 <pj/pool.h> +#include <pj/string.h> + +#if PJ_POOL_DEBUG + +#if PJ_HAS_MALLOC_H +# include <malloc.h> +#endif + + +#if PJ_HAS_STDLIB_H +# include <stdlib.h> +#endif + + +#if defined(PJ_WIN32) && PJ_WIN32!=0 && defined(PJ_DEBUG) && PJ_DEBUG!=0 \ + && !PJ_NATIVE_STRING_IS_UNICODE +# include <windows.h> +# 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 */ |