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__ */
+