From d2f04e2133e7cf2efa75a035131776e6d46b3f8e Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Thu, 15 Dec 2016 20:16:10 -0600
Subject: [PATCH] make ks_pool_free appear take double pointer

---
 libs/libks/src/dht/ks_dht.c             |  6 ++--
 libs/libks/src/dht/ks_dht_bucket.c      | 12 ++++----
 libs/libks/src/dht/ks_dht_datagram.c    |  2 +-
 libs/libks/src/dht/ks_dht_endpoint.c    |  2 +-
 libs/libks/src/dht/ks_dht_message.c     |  2 +-
 libs/libks/src/dht/ks_dht_search.c      |  6 ++--
 libs/libks/src/dht/ks_dht_storageitem.c |  2 +-
 libs/libks/src/dht/ks_dht_transaction.c |  2 +-
 libs/libks/src/include/ks_pool.h        |  4 +--
 libs/libks/src/ks_dht.c                 | 40 ++++++++++++-------------
 libs/libks/src/ks_hash.c                | 24 +++++++--------
 libs/libks/src/ks_mutex.c               |  6 ++--
 libs/libks/src/ks_pool.c                | 13 ++++++--
 libs/libks/src/ks_q.c                   |  6 ++--
 libs/libks/src/ks_thread.c              |  2 +-
 libs/libks/src/ks_thread_pool.c         |  2 +-
 libs/libks/src/kws.c                    | 10 +++----
 libs/libks/test/testpools.c             |  6 ++--
 libs/libks/test/testq.c                 |  6 ++--
 libs/libks/test/testthreadmutex.c       |  4 +--
 20 files changed, 83 insertions(+), 74 deletions(-)

diff --git a/libs/libks/src/dht/ks_dht.c b/libs/libks/src/dht/ks_dht.c
index a236f2ff8a..0d53283b02 100644
--- a/libs/libks/src/dht/ks_dht.c
+++ b/libs/libks/src/dht/ks_dht.c
@@ -286,7 +286,7 @@ KS_DECLARE(void) ks_dht_destroy(ks_dht_t **dht)
 	 * Cleanup the array of endpoint pointers if it is allocated.
 	 */
 	if (d->endpoints) {
-		ks_pool_free(d->pool, d->endpoints);
+		ks_pool_free(d->pool, &d->endpoints);
 		d->endpoints = NULL;
 	}
 
@@ -294,7 +294,7 @@ KS_DECLARE(void) ks_dht_destroy(ks_dht_t **dht)
 	 * Cleanup the array of endpoint polling data if it is allocated.
 	 */
 	if (d->endpoints_poll) {
-		ks_pool_free(d->pool, d->endpoints_poll);
+		ks_pool_free(d->pool, &d->endpoints_poll);
 		d->endpoints_poll = NULL;
 	}
 
@@ -338,7 +338,7 @@ KS_DECLARE(void) ks_dht_destroy(ks_dht_t **dht)
 	/**
 	 * Free the dht instance from the pool, after this the dht instance memory is invalid.
 	 */
-	ks_pool_free(d->pool, d);
+	ks_pool_free(d->pool, &d);
 
 	/**
 	 * At this point dht instance is invalidated so NULL the pointer.
diff --git a/libs/libks/src/dht/ks_dht_bucket.c b/libs/libks/src/dht/ks_dht_bucket.c
index e4815d0a50..c64ca228fb 100644
--- a/libs/libks/src/dht/ks_dht_bucket.c
+++ b/libs/libks/src/dht/ks_dht_bucket.c
@@ -214,7 +214,7 @@ KS_DECLARE(void) ks_dhtrt_deinitroute(ks_dhtrt_routetable_t **table)
 
 	ks_pool_t *pool = (*table)->pool;
 
-	ks_pool_free(pool, *table);
+	ks_pool_free(pool, &(*table));
 
 	return;
 }
@@ -259,7 +259,7 @@ KS_DECLARE(ks_status_t)	 ks_dhtrt_create_node( ks_dhtrt_routetable_t *table,
 
     if (( ks_addr_set(&tnode->addr, ip, port, tnode->family) != KS_STATUS_SUCCESS) ||
         ( ks_rwl_create(&tnode->reflock, table->pool) !=  KS_STATUS_SUCCESS))       {
-        ks_pool_free(table->pool, tnode);
+        ks_pool_free(table->pool, &tnode);
 		ks_rwl_read_unlock(internal->lock);
         return KS_STATUS_FAIL;
     }
@@ -308,7 +308,7 @@ KS_DECLARE(ks_status_t) ks_dhtrt_delete_node(ks_dhtrt_routetable_t *table, ks_dh
 
     if (ks_rwl_try_write_lock(node->reflock) ==  KS_STATUS_SUCCESS) {      /* grab exclusive lock on node */
 		ks_rwl_destroy(&(node->reflock));
-		ks_pool_free(table->pool, node);
+		ks_pool_free(table->pool, &node);
     }
     else {
         ks_dhtrt_queue_node_fordelete(table, node);
@@ -718,7 +718,7 @@ uint8_t ks_dhtrt_findclosest_locked_nodes(ks_dhtrt_routetable_t *table, ks_dhtrt
 	while (tofree) {
 		ks_dhtrt_sortedxors_t *x = tofree->next;
 
-		ks_pool_free(table->pool, tofree);
+		ks_pool_free(table->pool, &tofree);
 		tofree = x;
 	}
 
@@ -859,10 +859,10 @@ void ks_dhtrt_process_deleted(ks_dhtrt_routetable_t *table)
 
 		if (ks_rwl_try_write_lock(node->reflock) == KS_STATUS_SUCCESS) {        
 	        ks_rwl_destroy(&(node->reflock));
-	        ks_pool_free(table->pool, node);
+	        ks_pool_free(table->pool, &node);
             temp = deleted;
             deleted = deleted->next;
-            ks_pool_free(table->pool, temp);
+            ks_pool_free(table->pool, &temp);
             --internal->deleted_count;
 			if (prev != NULL) {
 				prev->next = deleted;
diff --git a/libs/libks/src/dht/ks_dht_datagram.c b/libs/libks/src/dht/ks_dht_datagram.c
index 83927ddb9b..a13c090d18 100644
--- a/libs/libks/src/dht/ks_dht_datagram.c
+++ b/libs/libks/src/dht/ks_dht_datagram.c
@@ -46,7 +46,7 @@ KS_DECLARE(void) ks_dht_datagram_destroy(ks_dht_datagram_t **datagram)
 
 	dg = *datagram;
 
-	ks_pool_free(dg->pool, dg);
+	ks_pool_free(dg->pool, &dg);
 
 	*datagram = NULL;
 }
diff --git a/libs/libks/src/dht/ks_dht_endpoint.c b/libs/libks/src/dht/ks_dht_endpoint.c
index ad9a44ec5e..4907d15f34 100644
--- a/libs/libks/src/dht/ks_dht_endpoint.c
+++ b/libs/libks/src/dht/ks_dht_endpoint.c
@@ -49,7 +49,7 @@ KS_DECLARE(void) ks_dht_endpoint_destroy(ks_dht_endpoint_t **endpoint)
 	ep = *endpoint;
 
 	if (ep->sock != KS_SOCK_INVALID) ks_socket_close(&ep->sock);
-	ks_pool_free(ep->pool, ep);
+	ks_pool_free(ep->pool, &ep);
 
 	*endpoint = NULL;
 }
diff --git a/libs/libks/src/dht/ks_dht_message.c b/libs/libks/src/dht/ks_dht_message.c
index 1b2284decb..3430c2853d 100644
--- a/libs/libks/src/dht/ks_dht_message.c
+++ b/libs/libks/src/dht/ks_dht_message.c
@@ -45,7 +45,7 @@ KS_DECLARE(void) ks_dht_message_destroy(ks_dht_message_t **message)
 		ben_free(m->data);
 		m->data = NULL;
 	}
-	ks_pool_free(m->pool, *message);
+	ks_pool_free(m->pool, &(*message));
 
 	*message = NULL;
 }
diff --git a/libs/libks/src/dht/ks_dht_search.c b/libs/libks/src/dht/ks_dht_search.c
index 1974a3d0cf..1945fcd294 100644
--- a/libs/libks/src/dht/ks_dht_search.c
+++ b/libs/libks/src/dht/ks_dht_search.c
@@ -53,12 +53,12 @@ KS_DECLARE(void) ks_dht_search_destroy(ks_dht_search_t **search)
 		ks_hash_destroy(&s->pending);
 	}
 	if (s->callbacks) {
-		ks_pool_free(s->pool, s->callbacks);
+		ks_pool_free(s->pool, &s->callbacks);
 		s->callbacks = NULL;
 	}
 	if (s->mutex) ks_mutex_destroy(&s->mutex);
 
-	ks_pool_free(s->pool, s);
+	ks_pool_free(s->pool, &s);
 
 	*search = NULL;
 }
@@ -115,7 +115,7 @@ KS_DECLARE(void) ks_dht_search_pending_destroy(ks_dht_search_pending_t **pending
 
 	p = *pending;
 
-	ks_pool_free(p->pool, p);
+	ks_pool_free(p->pool, &p);
 
 	*pending = NULL;
 }
diff --git a/libs/libks/src/dht/ks_dht_storageitem.c b/libs/libks/src/dht/ks_dht_storageitem.c
index d395e49540..c20cdd1e9c 100644
--- a/libs/libks/src/dht/ks_dht_storageitem.c
+++ b/libs/libks/src/dht/ks_dht_storageitem.c
@@ -105,7 +105,7 @@ KS_DECLARE(void) ks_dht_storageitem_destroy(ks_dht_storageitem_t **item)
 		ben_free(si->v);
 		si->v = NULL;
 	}
-	ks_pool_free(si->pool, si);
+	ks_pool_free(si->pool, &si);
 
 	*item = NULL;
 }
diff --git a/libs/libks/src/dht/ks_dht_transaction.c b/libs/libks/src/dht/ks_dht_transaction.c
index 0912fa7589..91db11447d 100644
--- a/libs/libks/src/dht/ks_dht_transaction.c
+++ b/libs/libks/src/dht/ks_dht_transaction.c
@@ -40,7 +40,7 @@ KS_DECLARE(void) ks_dht_transaction_destroy(ks_dht_transaction_t **transaction)
 
 	t = *transaction;
 
-	ks_pool_free(t->pool, t);
+	ks_pool_free(t->pool, &t);
 
 	*transaction = NULL;
 }
diff --git a/libs/libks/src/include/ks_pool.h b/libs/libks/src/include/ks_pool.h
index 083d152ac8..57dc5a725c 100644
--- a/libs/libks/src/include/ks_pool.h
+++ b/libs/libks/src/include/ks_pool.h
@@ -291,7 +291,7 @@ KS_DECLARE(void *) ks_pool_calloc_ex(ks_pool_t *mp_p, const unsigned long ele_n,
  *
  */
 
-KS_DECLARE(ks_status_t) ks_pool_free(ks_pool_t *mp_p, void *addr);
+KS_DECLARE(ks_status_t) ks_pool_free_ex(ks_pool_t *mp_p, void **addrP);
 
 /*
  * void *ks_pool_resize
@@ -453,7 +453,7 @@ KS_DECLARE(const char *) ks_pool_strerror(const ks_status_t error);
 
 KS_DECLARE(ks_status_t) ks_pool_set_cleanup(ks_pool_t *mp_p, void *ptr, void *arg, int type, ks_pool_cleanup_fn_t fn);
 
-#define ks_pool_safe_free(_p, _a) ks_pool_free(_p, _a); (_a) = NULL
+#define ks_pool_free(_p, _x) ks_pool_free_ex(_p, (void **)_x)
 
 /*<<<<<<<<<<   This is end of the auto-generated output from fillproto. */
 
diff --git a/libs/libks/src/ks_dht.c b/libs/libks/src/ks_dht.c
index 31ad823b89..5715a4902c 100644
--- a/libs/libks/src/ks_dht.c
+++ b/libs/libks/src/ks_dht.c
@@ -943,7 +943,7 @@ static int expire_buckets(dht_handle_t *h, struct bucket *b)
             b->nodes = n->next;
             b->count--;
             changed = 1;
-            ks_pool_free(h->pool, n);
+            ks_pool_free(h->pool, &n);
         }
 
         p = b->nodes;
@@ -953,7 +953,7 @@ static int expire_buckets(dht_handle_t *h, struct bucket *b)
                 p->next = n->next;
                 b->count--;
                 changed = 1;
-                ks_pool_free(h->pool, n);
+                ks_pool_free(h->pool, &n);
             }
             p = p->next;
         }
@@ -1073,7 +1073,7 @@ static void expire_searches(dht_handle_t *h)
             } else {
                 h->searches = next;
 			}
-            ks_pool_free(h->pool, sr);
+            ks_pool_free(h->pool, &sr);
             h->numsearches--;
         } else {
             previous = sr;
@@ -1425,11 +1425,11 @@ static int expire_storage(dht_handle_t *h)
             free(st->peers);
             if (previous) {
                 previous->next = st->next;
-				ks_pool_free(h->pool, st);
+				ks_pool_free(h->pool, &st);
                 st = previous->next;
 			} else {
                 h->storage = st->next;
-				ks_pool_free(h->pool, st);
+				ks_pool_free(h->pool, &st);
                 st = h->storage;
 			}
 
@@ -1670,7 +1670,7 @@ static void ks_dht_store_entry_destroy(struct ks_dht_store_entry_s **old_entry)
 		entry->body = NULL;
 	}
 	
-	ks_pool_free(pool, entry);	
+	ks_pool_free(pool, &entry);	
 	return;
 }
 
@@ -1848,7 +1848,7 @@ static void ks_dht_store_destroy(struct ks_dht_store_s **old_store)
 
 	ks_hash_destroy(&store->hash);
 
-	ks_pool_free(pool, store);
+	ks_pool_free(pool, &store);
 	
 	return;
 }
@@ -1954,7 +1954,7 @@ static void clear_all_ip(dht_handle_t *h)
 		ipt = (ks_ip_t *) val;
 
 		ks_socket_close(&ipt->sock);
-		ks_pool_free(h->pool, ipt);
+		ks_pool_free(h->pool, &ipt);
 
 		if (ipt->addr.family == AF_INET) {
 			h->ip4s--;
@@ -2008,14 +2008,14 @@ static ks_ip_t *add_ip(dht_handle_t *h, const char *ip, int port, int family)
 
 	if ((ipt->sock = socket(family, SOCK_DGRAM, IPPROTO_UDP)) == KS_SOCK_INVALID) {
 		ks_log(KS_LOG_ERROR, "Socket Error\n");
-		ks_pool_free(h->pool, ipt);
+		ks_pool_free(h->pool, &ipt);
 		return NULL;
 	}
 
 	if (ks_addr_bind(ipt->sock, &ipt->addr) != KS_STATUS_SUCCESS) {
 		ks_log(KS_LOG_ERROR, "Error Adding bind ip: %s port: %d sock: %d (%s)\n", ip, port, ipt->sock, strerror(errno));
 		ks_socket_close(&ipt->sock);
-		ks_pool_free(h->pool, ipt);
+		ks_pool_free(h->pool, &ipt);
 		return NULL;
 	}
 
@@ -2168,9 +2168,9 @@ KS_DECLARE(ks_status_t) ks_dht_init(dht_handle_t **handle, ks_dht_af_flag_t af_f
     return KS_STATUS_SUCCESS;
 
  fail:
-    ks_pool_free(h->pool, h->buckets);
+    ks_pool_free(h->pool, &h->buckets);
     h->buckets = NULL;
-    ks_pool_free(h->pool, h->buckets6);
+    ks_pool_free(h->pool, &h->buckets6);
     h->buckets6 = NULL;
     return KS_STATUS_FAIL;
 }
@@ -2193,9 +2193,9 @@ KS_DECLARE(int) dht_uninit(dht_handle_t **handle)
         while (b->nodes) {
             struct node *n = b->nodes;
             b->nodes = n->next;
-            ks_pool_free(h->pool, n);
+            ks_pool_free(h->pool, &n);
         }
-        ks_pool_free(h->pool, b);
+        ks_pool_free(h->pool, &b);
     }
 
     while (h->buckets6) {
@@ -2204,28 +2204,28 @@ KS_DECLARE(int) dht_uninit(dht_handle_t **handle)
         while (b->nodes) {
             struct node *n = b->nodes;
             b->nodes = n->next;
-            ks_pool_free(h->pool, n);
+            ks_pool_free(h->pool, &n);
         }
-        ks_pool_free(h->pool, b);
+        ks_pool_free(h->pool, &b);
     }
 
     while (h->storage) {
         struct storage *st = h->storage;
         h->storage = h->storage->next;
-        ks_pool_free(h->pool, st->peers);
-        ks_pool_free(h->pool, st);
+        ks_pool_free(h->pool, &st->peers);
+        ks_pool_free(h->pool, &st);
     }
 
     while (h->searches) {
         struct search *sr = h->searches;
         h->searches = h->searches->next;
-        ks_pool_free(h->pool, sr);
+        ks_pool_free(h->pool, &sr);
     }
 
 	ks_dht_store_destroy(&h->store);
 	pool = h->pool;
 	h->pool = NULL;
-	ks_pool_free(pool, h);
+	ks_pool_free(pool, &h);
 	ks_pool_close(&pool);
 
     return 1;
diff --git a/libs/libks/src/ks_hash.c b/libs/libks/src/ks_hash.c
index c65e2b4d28..961c154cda 100644
--- a/libs/libks/src/ks_hash.c
+++ b/libs/libks/src/ks_hash.c
@@ -293,7 +293,7 @@ ks_hash_expand(ks_hash_t *h)
 					newtable[index] = e;
 				}
 			}
-			ks_pool_safe_free(h->pool, h->table);
+			ks_pool_free(h->pool, &h->table);
 			h->table = newtable;
 		}
     /* Plan B: realloc instead */
@@ -357,10 +357,10 @@ static void * _ks_hash_remove(ks_hash_t *h, void *k, unsigned int hashvalue, uns
 			h->entrycount--;
 			v = e->v;
 			if (e->flags & KS_HASH_FLAG_FREE_KEY) {
-				ks_pool_free(h->pool, e->k);
+				ks_pool_free(h->pool, &e->k);
 			}
 			if (e->flags & KS_HASH_FLAG_FREE_VALUE) {
-				ks_pool_safe_free(h->pool, e->v); 
+				ks_pool_free(h->pool, &e->v); 
 				v = NULL;
 			} else if (e->destructor) {
 				e->destructor(e->v);
@@ -369,7 +369,7 @@ static void * _ks_hash_remove(ks_hash_t *h, void *k, unsigned int hashvalue, uns
 				h->destructor(e->v);
 				v = e->v = NULL;
 			}
-			ks_pool_safe_free(h->pool, e);
+			ks_pool_free(h->pool, &e);
 			return v;
 		}
 		pE = &(e->next);
@@ -538,11 +538,11 @@ ks_hash_destroy(ks_hash_t **h)
 			f = e; e = e->next; 
 
 			if (f->flags & KS_HASH_FLAG_FREE_KEY) {
-				ks_pool_free((*h)->pool, f->k); 
+				ks_pool_free((*h)->pool, &f->k); 
 			}
 			
 			if (f->flags & KS_HASH_FLAG_FREE_VALUE) {
-				ks_pool_safe_free((*h)->pool, f->v); 
+				ks_pool_free((*h)->pool, &f->v); 
 			} else if (f->destructor) {
 				f->destructor(f->v);
 				f->v = NULL;
@@ -550,18 +550,18 @@ ks_hash_destroy(ks_hash_t **h)
 				(*h)->destructor(f->v);
 				f->v = NULL;
 			}
-			ks_pool_safe_free((*h)->pool, f); 
+			ks_pool_free((*h)->pool, &f); 
 		}
 	}
 
 	pool = (*h)->pool;
-    ks_pool_safe_free(pool, (*h)->table);
+    ks_pool_free(pool, &(*h)->table);
 	ks_hash_write_unlock(*h);
-	if ((*h)->rwl) ks_pool_free(pool, (*h)->rwl);
+	if ((*h)->rwl) ks_pool_free(pool, &(*h)->rwl);
 	if ((*h)->mutex) {
-		ks_pool_free(pool, (*h)->mutex);
+		ks_pool_free(pool, &(*h)->mutex);
 	}
-	ks_pool_free(pool, *h);
+	ks_pool_free(pool, &(*h));
 	pool = NULL;
 	*h = NULL;
 
@@ -580,7 +580,7 @@ KS_DECLARE(void) ks_hash_last(ks_hash_iterator_t **iP)
 		ks_rwl_read_unlock(i->h->rwl);
 	}
 
-	ks_pool_free(i->h->pool, i);
+	ks_pool_free(i->h->pool, &i);
 	
 	*iP = NULL;
 }
diff --git a/libs/libks/src/ks_mutex.c b/libs/libks/src/ks_mutex.c
index 572a609d68..166c742487 100644
--- a/libs/libks/src/ks_mutex.c
+++ b/libs/libks/src/ks_mutex.c
@@ -93,7 +93,7 @@ KS_DECLARE(ks_status_t) ks_mutex_destroy(ks_mutex_t **mutexP)
 #endif
 		free(mutex);
 	} else {
-		ks_pool_free(mutex->pool, (void *)mutex);
+		ks_pool_free(mutex->pool, &mutex);
 	}
 
 	return KS_STATUS_SUCCESS;
@@ -422,7 +422,7 @@ KS_DECLARE(ks_status_t) ks_cond_destroy(ks_cond_t **cond)
 
 	*cond = NULL;
 
-	return ks_pool_free(condp->pool, condp);
+	return ks_pool_free(condp->pool, &condp);
 }
 
 
@@ -655,7 +655,7 @@ KS_DECLARE(ks_status_t) ks_rwl_destroy(ks_rwl_t **rwlock)
 
 	*rwlock = NULL;
 
-	return ks_pool_free(rwlockp->pool, rwlockp);
+	return ks_pool_free(rwlockp->pool, &rwlockp);
 }
 
 
diff --git a/libs/libks/src/ks_pool.c b/libs/libks/src/ks_pool.c
index 92e2d827fe..a2a9503234 100644
--- a/libs/libks/src/ks_pool.c
+++ b/libs/libks/src/ks_pool.c
@@ -1616,13 +1616,18 @@ KS_DECLARE(void *) ks_pool_calloc(ks_pool_t *mp_p, const unsigned long ele_n, co
  * mp_p <-> Pointer to the memory pool.
  *
  *
- * addr <-> Address to free.
+ * addr <-> pointer to pointer of Address to free.
  *
  */
-KS_DECLARE(ks_status_t) ks_pool_free(ks_pool_t *mp_p, void *addr)
+KS_DECLARE(ks_status_t) ks_pool_free_ex(ks_pool_t *mp_p, void **addrP)
 {
 	ks_status_t r;
+	void *addr;
 
+	ks_assert(addrP);
+
+	addr = *addrP;
+	
 	ks_assert(mp_p);
 	ks_assert(addr);
 
@@ -1649,6 +1654,10 @@ KS_DECLARE(ks_status_t) ks_pool_free(ks_pool_t *mp_p, void *addr)
 
 	ks_mutex_unlock(mp_p->mutex);
 
+	if (r == KS_STATUS_SUCCESS) {
+		*addrP = NULL;
+	}
+
 	return r;
 
 }
diff --git a/libs/libks/src/ks_q.c b/libs/libks/src/ks_q.c
index f8ab627213..05e962a56a 100644
--- a/libs/libks/src/ks_q.c
+++ b/libs/libks/src/ks_q.c
@@ -73,14 +73,14 @@ static void ks_q_cleanup(ks_pool_t *mpool, void *ptr, void *arg, int type, ks_po
 		while(np) {
 			fp = np;
 			np = np->next;
-			ks_pool_free(q->pool, fp);
+			ks_pool_free(q->pool, &fp);
 		}
 		
 		np = q->empty;
 		while(np) {
 			fp = np;
 			np = np->next;
-			ks_pool_free(q->pool, fp);
+			ks_pool_free(q->pool, &fp);
 		}
 		break;
 	case KS_MPCL_DESTROY:
@@ -167,7 +167,7 @@ KS_DECLARE(ks_status_t) ks_q_destroy(ks_q_t **qP)
 		ks_q_term(q);
 
 		pool = q->pool;
-		ks_pool_free(pool, q);
+		ks_pool_free(pool, &q);
 		pool = NULL;
 
 		return KS_STATUS_SUCCESS;
diff --git a/libs/libks/src/ks_thread.c b/libs/libks/src/ks_thread.c
index 63030b81f4..890d22a02e 100644
--- a/libs/libks/src/ks_thread.c
+++ b/libs/libks/src/ks_thread.c
@@ -261,7 +261,7 @@ KS_DECLARE(ks_status_t) ks_thread_create_ex(ks_thread_t **rthread, ks_thread_fun
 	if (thread) {
 		thread->running = 0;
 		if (pool) {
-			ks_pool_safe_free(pool, thread);
+			ks_pool_free(pool, &thread);
 		}
 	}
   done:
diff --git a/libs/libks/src/ks_thread_pool.c b/libs/libks/src/ks_thread_pool.c
index aa0ff29ef3..8368ebdd24 100644
--- a/libs/libks/src/ks_thread_pool.c
+++ b/libs/libks/src/ks_thread_pool.c
@@ -170,7 +170,7 @@ static void *worker_thread(ks_thread_t *thread, void *data)
 		idle_sec = 0;
 		job->func(thread, job->data);
 		
-		ks_pool_free(tp->pool, job);
+		ks_pool_free(tp->pool, &job);
 
 		ks_mutex_lock(tp->mutex);
 		tp->busy_thread_count--;
diff --git a/libs/libks/src/kws.c b/libs/libks/src/kws.c
index d364b50f0e..4b34bb345c 100644
--- a/libs/libks/src/kws.c
+++ b/libs/libks/src/kws.c
@@ -752,7 +752,7 @@ KS_DECLARE(void) kws_destroy(kws_t **kwsP)
 	kws->down = 2;
 
 	if (kws->write_buffer) {
-		ks_pool_free(kws->pool, kws->write_buffer);
+		ks_pool_free(kws->pool, &kws->write_buffer);
 		kws->write_buffer = NULL;
 		kws->write_buffer_len = 0;
 	}
@@ -767,12 +767,12 @@ KS_DECLARE(void) kws_destroy(kws_t **kwsP)
 		kws->ssl = NULL;
 	}
 
-	if (kws->buffer) ks_pool_free(kws->pool, kws->buffer);
-	if (kws->bbuffer) ks_pool_free(kws->pool, kws->bbuffer);
+	if (kws->buffer) ks_pool_free(kws->pool, &kws->buffer);
+	if (kws->bbuffer) ks_pool_free(kws->pool, &kws->bbuffer);
 
 	kws->buffer = kws->bbuffer = NULL;
 
-	ks_pool_free(kws->pool, kws);
+	ks_pool_free(kws->pool, &kws);
 	kws = NULL;
 }
 
@@ -786,7 +786,7 @@ KS_DECLARE(ks_ssize_t) kws_close(kws_t *kws, int16_t reason)
 	kws->down = 1;
 	
 	if (kws->uri) {
-		ks_pool_free(kws->pool, kws->uri);
+		ks_pool_free(kws->pool, &kws->uri);
 		kws->uri = NULL;
 	}
 
diff --git a/libs/libks/test/testpools.c b/libs/libks/test/testpools.c
index b3be4ebfbf..1cb77f1271 100644
--- a/libs/libks/test/testpools.c
+++ b/libs/libks/test/testpools.c
@@ -84,7 +84,7 @@ int main(int argc, char **argv)
 
 	printf("FREE:\n");
 
-	status = ks_pool_safe_free(pool, str);
+	status = ks_pool_free(pool, &str);
 	if (status != KS_STATUS_SUCCESS) {
 		fprintf(stderr, "FREE ERR: [%s]\n", ks_pool_strerror(err));
 		exit(255);
@@ -122,7 +122,7 @@ int main(int argc, char **argv)
 
 	printf("FREE OBJ:\n");
 
-	status = ks_pool_safe_free(pool, foo);
+	status = ks_pool_free(pool, &foo);
 	ok(status == KS_STATUS_SUCCESS);
 	if (status != KS_STATUS_SUCCESS) {
 		fprintf(stderr, "FREE OBJ ERR: [%s]\n", ks_pool_strerror(status));
@@ -181,7 +181,7 @@ int main(int argc, char **argv)
 
 	printf("FREE 2:\n");
 
-	status = ks_pool_free(pool, str);
+	status = ks_pool_free(pool, &str);
 	ok(status == KS_STATUS_SUCCESS);
 	if (status != KS_STATUS_SUCCESS) {
 		fprintf(stderr, "FREE2 ERR: [%s]\n", ks_pool_strerror(status));
diff --git a/libs/libks/test/testq.c b/libs/libks/test/testq.c
index 9f5936e027..8519e1e957 100644
--- a/libs/libks/test/testq.c
+++ b/libs/libks/test/testq.c
@@ -10,7 +10,7 @@ static void *test1_thread(ks_thread_t *thread, void *data)
 	while(ks_q_pop(q, &pop) == KS_STATUS_SUCCESS) {
 		//int *i = (int *)pop;
 		//printf("POP %d\n", *i);
-		ks_pool_free(thread->pool, pop);
+		ks_pool_free(thread->pool, &pop);
 	}
 	
 	return NULL;
@@ -19,7 +19,7 @@ static void *test1_thread(ks_thread_t *thread, void *data)
 static void do_flush(ks_q_t *q, void *ptr, void *flush_data)
 {
 	ks_pool_t *pool = (ks_pool_t *)flush_data;
-	ks_pool_free(pool, ptr);
+	ks_pool_free(pool, &ptr);
 
 }
 
@@ -104,7 +104,7 @@ static void *test2_thread(ks_thread_t *thread, void *data)
 			//int *i = (int *)pop;
 			//printf("%p POP %d\n", (void *)pthread_self(), *i);
 			popped++;
-			ks_pool_free(thread->pool, pop);
+			ks_pool_free(thread->pool, &pop);
 		} else if (status == KS_STATUS_INACTIVE) {
 			break;
 		} else if (t2->try && ks_q_size(t2->q)) {
diff --git a/libs/libks/test/testthreadmutex.c b/libs/libks/test/testthreadmutex.c
index 38e697f16b..ebb593b662 100644
--- a/libs/libks/test/testthreadmutex.c
+++ b/libs/libks/test/testthreadmutex.c
@@ -185,7 +185,7 @@ static void *thread_test_function_atatched(ks_thread_t *thread, void *data)
 
 	for (i = 0; i < LOOP_COUNT; i++) {
 		if (last_mem) {
-			ks_pool_safe_free(thread->pool, last_mem);
+			ks_pool_free(thread->pool, &last_mem);
 		}
 		mem = ks_pool_alloc(thread->pool, 1024);
 		last_mem = mem;
@@ -246,7 +246,7 @@ static void check_thread_priority(void)
 	ok( ks_thread_priority(thread_p) == KS_PRI_IMPORTANT );
 	end_todo;
 
-	ks_pool_free(pool, thread_p);
+	ks_pool_free(pool, &thread_p);
 }
 
 static void join_threads(void)