Index: pjproject/trunk/pjlib/include/pj/config.h
===================================================================
--- pjproject/trunk/pjlib/include/pj/config.h	(revision 363)
+++ pjproject/trunk/pjlib/include/pj/config.h	(revision 364)
@@ -209,6 +209,10 @@
 #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,4 +221,15 @@
 #  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
+
 
 /**
@@ -261,4 +276,40 @@
 #   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
+
 
 /**
Index: pjproject/trunk/pjlib/include/pj/pool.h
===================================================================
--- pjproject/trunk/pjlib/include/pj/pool.h	(revision 363)
+++ pjproject/trunk/pjlib/include/pj/pool.h	(revision 364)
@@ -17,4 +17,15 @@
  * 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,6 +35,4 @@
  * @brief Memory Pool.
  */
-
-#include <pj/list.h>
 
 PJ_BEGIN_DECL
@@ -475,4 +484,15 @@
 
 /**
+ * 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
  */
Index: pjproject/trunk/pjlib/include/pj/pool_alt.h
===================================================================
--- pjproject/trunk/pjlib/include/pj/pool_alt.h	(revision 364)
+++ pjproject/trunk/pjlib/include/pj/pool_alt.h	(revision 364)
@@ -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__ */
+
Index: pjproject/trunk/pjlib/src/pj/config.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/config.c	(revision 363)
+++ pjproject/trunk/pjlib/src/pj/config.c	(revision 364)
@@ -22,5 +22,5 @@
 
 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)
@@ -44,8 +44,10 @@
     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));
Index: pjproject/trunk/pjlib/src/pj/pool.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/pool.c	(revision 363)
+++ pjproject/trunk/pjlib/src/pj/pool.c	(revision 364)
@@ -23,4 +23,7 @@
 #include <pj/os.h>
 
+#if !PJ_HAS_POOL_ALT_API
+
+
 /* Include inline definitions when inlining is disabled. */
 #if !PJ_FUNCTIONS_ARE_INLINED
@@ -262,2 +265,4 @@
 
 
+#endif	/* PJ_HAS_POOL_ALT_API */
+
Index: pjproject/trunk/pjlib/src/pj/pool_caching.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/pool_caching.c	(revision 363)
+++ pjproject/trunk/pjlib/src/pj/pool_caching.c	(revision 364)
@@ -23,4 +23,6 @@
 #include <pj/os.h>
 
+#if !PJ_HAS_POOL_ALT_API
+
 static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, 
 				    const char *name,
@@ -164,4 +166,5 @@
 {
     pj_caching_pool *cp = (pj_caching_pool*)pf;
+    unsigned pool_capacity;
     int i;
 
@@ -175,4 +178,6 @@
     /* 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
@@ -180,6 +185,6 @@
      * 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);
@@ -190,6 +195,6 @@
     /* 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);
 
@@ -197,5 +202,5 @@
      * 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)
 	;
 
@@ -209,5 +214,5 @@
 
     pj_list_insert_after(&cp->free_list[i], pool);
-    cp->capacity += pool->capacity;
+    cp->capacity += pool_capacity;
 
     pj_mutex_unlock(cp->mutex);
@@ -229,9 +234,12 @@
         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;
 	}
@@ -247,2 +255,5 @@
 #endif
 }
+
+#endif	/* PJ_HAS_POOL_ALT_API */
+
Index: pjproject/trunk/pjlib/src/pj/pool_dbg.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/pool_dbg.c	(revision 364)
+++ pjproject/trunk/pjlib/src/pj/pool_dbg.c	(revision 364)
@@ -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 */
Index: pjproject/trunk/pjlib/src/pj/pool_policy_malloc.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/pool_policy_malloc.c	(revision 363)
+++ pjproject/trunk/pjlib/src/pj/pool_policy_malloc.c	(revision 364)
@@ -21,4 +21,6 @@
 #include <pj/os.h>
 #include <pj/compat/malloc.h>
+
+#if !PJ_HAS_POOL_ALT_API
 
 /*
@@ -61,2 +63,4 @@
     0
 };
+
+#endif	/* PJ_HAS_POOL_ALT_API */
