Added ability to have custom pool backend (needed for pool debugging facility)

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@364 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjlib/include/pj/config.h b/pjlib/include/pj/config.h
index 896cfd5..cd95dbc 100644
--- a/pjlib/include/pj/config.h
+++ b/pjlib/include/pj/config.h
@@ -208,8 +208,12 @@
 #  define PJ_TERM_HAS_COLOR	    1
 #endif
 
+
 /**
- * Pool debugging.
+ * If pool debugging is used, then each memory allocation from the pool
+ * will call malloc(), and pool will release all memory chunks when it
+ * is destroyed. This works better when memory verification programs
+ * such as Rational Purify is used.
  *
  * Default: 0
  */
@@ -217,6 +221,17 @@
 #  define PJ_POOL_DEBUG		    0
 #endif
 
+
+/**
+ * Do we have alternate pool implementation?
+ *
+ * Default: 0
+ */
+#ifndef PJ_HAS_POOL_ALT_API
+#   define PJ_HAS_POOL_ALT_API	    PJ_POOL_DEBUG
+#endif
+
+
 /**
  * \def PJ_HAS_TCP
  * Support TCP in the library.
@@ -261,6 +276,42 @@
 #   endif
 #endif
 
+
+/**
+ * If PJ_IOQUEUE_HAS_SAFE_UNREG macro is defined, then ioqueue will do more
+ * things to ensure thread safety of handle unregistration operation by
+ * employing reference counter to each handle.
+ *
+ * In addition, the ioqueue will preallocate memory for the handles, 
+ * according to the maximum number of handles that is specified during 
+ * ioqueue creation.
+ *
+ * All applications would normally want this enabled, but you may disable
+ * this if:
+ *  - there is no dynamic unregistration to all ioqueues.
+ *  - there is no threading, or there is no preemptive multitasking.
+ *
+ * Default: 1
+ */
+#ifndef PJ_IOQUEUE_HAS_SAFE_UNREG
+#   define PJ_IOQUEUE_HAS_SAFE_UNREG	1
+#endif
+
+
+/**
+ * When safe unregistration (PJ_IOQUEUE_HAS_SAFE_UNREG) is configured in
+ * ioqueue, the PJ_IOQUEUE_KEY_FREE_DELAY macro specifies how long the
+ * ioqueue key is kept in closing state before it can be reused.
+ *
+ * The value is in miliseconds.
+ *
+ * Default: 500 msec.
+ */
+#ifndef PJ_IOQUEUE_KEY_FREE_DELAY
+#   define PJ_IOQUEUE_KEY_FREE_DELAY	500
+#endif
+
+
 /**
  * Overrides FD_SETSIZE so it is consistent throughout the library.
  * OS specific configuration header (compat/os_*) might have declared
diff --git a/pjlib/include/pj/pool.h b/pjlib/include/pj/pool.h
index 885b275..385392a 100644
--- a/pjlib/include/pj/pool.h
+++ b/pjlib/include/pj/pool.h
@@ -16,6 +16,17 @@
  * 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/list.h>
+
+/* See if we use pool's alternate API.
+ * The alternate API is used e.g. to implement pool debugging.
+ */
+#if PJ_HAS_POOL_ALT_API
+#  include <pj/pool_alt.h>
+#endif
+
+
 #ifndef __PJ_POOL_H__
 #define __PJ_POOL_H__
 
@@ -24,8 +35,6 @@
  * @brief Memory Pool.
  */
 
-#include <pj/list.h>
-
 PJ_BEGIN_DECL
 
 /**
@@ -474,6 +483,17 @@
 
 
 /**
+ * Dump pool factory state.
+ * @param pf	    The pool factory.
+ * @param detail    Detail state required.
+ */
+PJ_INLINE(void) pj_pool_factory_dump( pj_pool_factory *pf,
+				      pj_bool_t detail )
+{
+    (*pf->dump_status)(pf, detail);
+}
+
+/**
  *  @}	// PJ_POOL_FACTORY
  */
 
diff --git a/pjlib/include/pj/pool_alt.h b/pjlib/include/pj/pool_alt.h
new file mode 100644
index 0000000..a0929dc
--- /dev/null
+++ b/pjlib/include/pj/pool_alt.h
@@ -0,0 +1,125 @@
+/* $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 
+ */
+#ifndef __PJ_POOL_ALT_H__
+#define __PJ_POOL_ALT_H__
+
+#define __PJ_POOL_H__
+
+
+typedef struct pj_pool_t pj_pool_t;
+
+
+/**
+ * The type for function to receive callback from the pool when it is unable
+ * to allocate memory. The elegant way to handle this condition is to throw
+ * exception, and this is what is expected by most of this library 
+ * components.
+ */
+typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size);
+
+/**
+ * This constant denotes the exception number that will be thrown by default
+ * memory factory policy when memory allocation fails.
+ */
+extern int PJ_NO_MEMORY_EXCEPTION;
+
+
+
+/*
+ * Declare all pool API as macro that calls the implementation
+ * function.
+ */
+#define pj_pool_create(fc,nm,init,inc,cb)   \
+	pj_pool_create_imp(__FILE__, __LINE__, fc, nm, init, inc, cb)
+
+#define pj_pool_release(pool)		    pj_pool_release_imp(pool)
+#define pj_pool_getobjname(pool)	    pj_pool_getobjname_imp(pool)
+#define pj_pool_reset(pool)		    pj_pool_reset_imp(pool)
+#define pj_pool_get_capacity(pool)	    pj_pool_get_capacity_imp(pool)
+#define pj_pool_get_used_size(pool)	    pj_pool_get_used_size_imp(pool)
+#define pj_pool_alloc(pool,sz)		    \
+	pj_pool_alloc_imp(__FILE__, __LINE__, pool, sz)
+
+#define pj_pool_calloc(pool,cnt,elem)	    \
+	pj_pool_calloc_imp(__FILE__, __LINE__, pool, cnt, elem)
+
+#define pj_pool_zalloc(pool,sz)		    \
+	pj_pool_zalloc_imp(__FILE__, __LINE__, pool, sz)
+
+
+
+/*
+ * Declare prototypes for pool implementation API.
+ */
+
+/* Create pool */
+PJ_DECL(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);
+
+/* Release pool */
+PJ_DECL(void) pj_pool_release_imp(pj_pool_t *pool);
+
+/* Get pool name */
+PJ_DECL(const char*) pj_pool_getobjname_imp(pj_pool_t *pool);
+
+/* Reset pool */
+PJ_DECL(void) pj_pool_reset_imp(pj_pool_t *pool);
+
+/* Get capacity */
+PJ_DECL(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool);
+
+/* Get total used size */
+PJ_DECL(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool);
+
+/* Allocate memory from the pool */
+PJ_DECL(void*) pj_pool_alloc_imp(const char *file, int line, 
+				 pj_pool_t *pool, pj_size_t sz);
+
+/* Allocate memory from the pool and zero the memory */
+PJ_DECL(void*) pj_pool_calloc_imp(const char *file, int line, 
+				  pj_pool_t *pool, unsigned cnt, 
+				  unsigned elemsz);
+
+/* Allocate memory from the pool and zero the memory */
+PJ_DECL(void*) pj_pool_zalloc_imp(const char *file, int line, 
+				  pj_pool_t *pool, pj_size_t sz);
+
+
+typedef struct pj_pool_factory
+{
+    int dummy;
+} pj_pool_factory;
+
+typedef struct pj_caching_pool 
+{
+    pj_pool_factory factory;
+} pj_caching_pool;
+
+
+#define pj_caching_pool_init( cp, pol, mac)
+#define pj_caching_pool_destroy(cp)
+#define pj_pool_factory_dump(pf, detail)
+
+
+#endif	/* __PJ_POOL_ALT_H__ */
+
diff --git a/pjlib/src/pj/config.c b/pjlib/src/pj/config.c
index 7c2d2d9..f4bff39 100644
--- a/pjlib/src/pj/config.c
+++ b/pjlib/src/pj/config.c
@@ -21,7 +21,7 @@
 #include <pj/ioqueue.h>
 
 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_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 4cda9f4..e5412d4 100644
--- a/pjlib/src/pj/pool.c
+++ b/pjlib/src/pj/pool.c
@@ -22,6 +22,9 @@
 #include <pj/assert.h>
 #include <pj/os.h>
 
+#if !PJ_HAS_POOL_ALT_API
+
+
 /* Include inline definitions when inlining is disabled. */
 #if !PJ_FUNCTIONS_ARE_INLINED
 #  include <pj/pool_i.h>
@@ -261,3 +264,5 @@
 }
 
 
+#endif	/* PJ_HAS_POOL_ALT_API */
+
diff --git a/pjlib/src/pj/pool_caching.c b/pjlib/src/pj/pool_caching.c
index 324ad05..4f9904e 100644
--- a/pjlib/src/pj/pool_caching.c
+++ b/pjlib/src/pj/pool_caching.c
@@ -22,6 +22,8 @@
 #include <pj/assert.h>
 #include <pj/os.h>
 
+#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 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 @@
     /* 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 @@
 
     /* 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 @@
     }
 
     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 @@
 	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 @@
     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 0000000..550fcdc
--- /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 */
diff --git a/pjlib/src/pj/pool_policy_malloc.c b/pjlib/src/pj/pool_policy_malloc.c
index 05aca5b..7387bec 100644
--- a/pjlib/src/pj/pool_policy_malloc.c
+++ b/pjlib/src/pj/pool_policy_malloc.c
@@ -21,6 +21,8 @@
 #include <pj/os.h>
 #include <pj/compat/malloc.h>
 
+#if !PJ_HAS_POOL_ALT_API
+
 /*
  * This file contains pool default policy definition and implementation.
  */
@@ -60,3 +62,5 @@
     &default_pool_callback,
     0
 };
+
+#endif	/* PJ_HAS_POOL_ALT_API */