[MAJOR] ported appsession to use mempools v2

Also during this process, a bug was found in appsession_refresh().
It would not automatically requeue the task in the queue, so the
old sessions would not vanish.
diff --git a/include/common/appsession.h b/include/common/appsession.h
index 8fa681c..4687298 100644
--- a/include/common/appsession.h
+++ b/include/common/appsession.h
@@ -10,6 +10,7 @@
 #include <common/config.h>
 #include <common/hashpjw.h>
 #include <common/list.h>
+#include <common/memory.h>
 
 #include <types/task.h>
 
@@ -20,14 +21,11 @@
 	unsigned long int request_count;
 } appsess;
 
-#define sizeof_appsess  sizeof(struct appsessions)
-extern void **pool_appsess;
+extern struct pool_head *pool2_appsess;
 
 struct app_pool {
-	void **sessid;
-	void **serverid;
-	int ses_waste, ses_use, ses_msize;
-	int ser_waste, ser_use, ser_msize;
+	struct pool_head *sessid;
+	struct pool_head *serverid;
 };
 
 extern struct app_pool apools;
diff --git a/src/appsession.c b/src/appsession.c
index bf26510..50554a5 100644
--- a/src/appsession.c
+++ b/src/appsession.c
@@ -2,6 +2,7 @@
  * AppSession functions.
  *
  * Copyright 2004-2006 Alexander Lazic, Klaus Wagner
+ * Copyright 2006-2007 Willy Tarreau
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -17,6 +18,7 @@
 #include <common/chtbl.h>
 #include <common/config.h>
 #include <common/list.h>
+#include <common/memory.h>
 #include <common/time.h>
 
 #include <types/buffers.h>
@@ -27,7 +29,7 @@
 #include <proto/task.h>
 
 
-void **pool_appsess = NULL;
+struct pool_head *pool2_appsess;
 struct app_pool apools;
 int have_appsession;
 
@@ -72,30 +74,40 @@
 	struct proxy        *p = proxy;
     
 	if (!initialized) {
+		pool2_appsess = create_pool("appsess", sizeof(appsess), MEM_F_SHARED);
+		if (pool2_appsess == NULL)
+			return -1;
+
 		if (!appsession_task_init()) {
+			int ser_msize, ses_msize;
+
 			apools.sessid = NULL;
 			apools.serverid = NULL;
-			apools.ser_waste = 0;
-			apools.ser_use = 0;
-			apools.ser_msize = sizeof(void *);
-			apools.ses_waste = 0;
-			apools.ses_use = 0;
-			apools.ses_msize = sizeof(void *);
+
+			ser_msize = sizeof(void *);
+			ses_msize = sizeof(void *);
 			while (p) {
 				s = p->srv;
-				if (apools.ses_msize < p->appsession_len)
-					apools.ses_msize = p->appsession_len;
+				if (ses_msize < p->appsession_len)
+					ses_msize = p->appsession_len;
 				while (s) {
 					idlen = strlen(s->id);
-					if (apools.ser_msize < idlen)
-						apools.ser_msize = idlen;
+					if (ser_msize < idlen)
+						ser_msize = idlen;
 					s = s->next;
 				}
 				p = p->next;
 			}
 			/* we use strings, so reserve space for '\0' */
-			apools.ser_msize ++;
-			apools.ses_msize ++;
+			ser_msize ++;
+			ses_msize ++;
+
+			apools.sessid = create_pool("sessid", ses_msize, MEM_F_SHARED);
+			if (!apools.sessid)
+				return -1;
+			apools.serverid = create_pool("serverid", ser_msize, MEM_F_SHARED);
+			if (!apools.serverid)
+				return -1;
 		}
 		else {
 			fprintf(stderr, "appsession_task_init failed\n");
@@ -118,8 +130,8 @@
 		t->state = TASK_IDLE;
 		t->context = NULL;
 		tv_ms_add(&t->expire, &now, TBLCHKINT);
-		task_queue(t);
 		t->process = appsession_refresh;
+		task_queue(t);
 		initialized ++;
 	}
 	return 0;
@@ -174,6 +186,7 @@
 		p = p->next;
 	}
 	tv_ms_add(&t->expire, &now, TBLCHKINT); /* check expiration every 5 seconds */
+	task_queue(t);
 	*next = t->expire;
 } /* end appsession_refresh */
 
@@ -196,12 +209,12 @@
     temp1 = (appsess *)data;
 
     if (temp1->sessid)
-	pool_free_to(apools.sessid, temp1->sessid);
+	pool_free2(apools.sessid, temp1->sessid);
 
     if (temp1->serverid)
-	pool_free_to(apools.serverid, temp1->serverid);
+	pool_free2(apools.serverid, temp1->serverid);
 
-    pool_free(appsess, temp1);
+    pool_free2(pool2_appsess, temp1);
 } /* end destroy */
 
 void appsession_cleanup( void )
diff --git a/src/haproxy.c b/src/haproxy.c
index c88bffc..7f5641c 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -667,12 +667,12 @@
 	pool_destroy(pool_requri);
 	pool_destroy2(pool2_task);
 	pool_destroy(pool_capture);
-	pool_destroy(pool_appsess);
+	pool_destroy2(pool2_appsess);
 	pool_destroy2(pool2_pendconn);
     
 	if (have_appsession) {
-		pool_destroy(apools.serverid);
-		pool_destroy(apools.sessid);
+		pool_destroy2(apools.serverid);
+		pool_destroy2(apools.sessid);
 	}
 } /* end deinit() */
 
diff --git a/src/proto_http.c b/src/proto_http.c
index d1bfdcf..51d265c 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -4298,7 +4298,7 @@
 
 					asession_temp = &local_asession;
 			  
-					if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
+					if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
 						Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
 						send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
 						return;
@@ -4310,9 +4310,9 @@
 			    
 					/* only do insert, if lookup fails */
 					if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
-						if ((asession_temp = pool_alloc(appsess)) == NULL) {
+						if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
 							/* free previously allocated memory */
-							pool_free_to(apools.sessid, local_asession.sessid);
+							pool_free2(apools.sessid, local_asession.sessid);
 							Alert("Not enough memory process_cli():asession:calloc().\n");
 							send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
 							return;
@@ -4323,7 +4323,7 @@
 						chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
 					} else {
 						/* free previously allocated memory */
-						pool_free_to(apools.sessid, local_asession.sessid);
+						pool_free2(apools.sessid, local_asession.sessid);
 					}
 			    
 					if (asession_temp->serverid == NULL) {
@@ -4771,7 +4771,7 @@
 				size_t server_id_len = strlen(t->srv->id) + 1;
 				asession_temp = &local_asession;
 		      
-				if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
+				if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
 					Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
 					send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
 					return;
@@ -4782,7 +4782,7 @@
 
 				/* only do insert, if lookup fails */
 				if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
-					if ((asession_temp = pool_alloc(appsess)) == NULL) {
+					if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
 						Alert("Not enough Memory process_srv():asession:calloc().\n");
 						send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
 						return;
@@ -4793,11 +4793,11 @@
 				}/* end if (chtbl_lookup()) */
 				else {
 					/* free wasted memory */
-					pool_free_to(apools.sessid, local_asession.sessid);
+					pool_free2(apools.sessid, local_asession.sessid);
 				} /* end else from if (chtbl_lookup()) */
 		      
 				if (asession_temp->serverid == NULL) {
-					if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
+					if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
 						Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
 						send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
 						return;
@@ -4941,7 +4941,7 @@
 	/* First try if we already have an appsession */
 	asession_temp = &local_asession;
 	
-	if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
+	if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
 		Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
 		send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
 		return;
@@ -4954,9 +4954,9 @@
 	
 	/* only do insert, if lookup fails */
 	if (chtbl_lookup(&(t->be->htbl_proxy), (void *)&asession_temp)) {
-		if ((asession_temp = pool_alloc(appsess)) == NULL) {
+		if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
 			/* free previously allocated memory */
-			pool_free_to(apools.sessid, local_asession.sessid);
+			pool_free2(apools.sessid, local_asession.sessid);
 			Alert("Not enough memory process_cli():asession:calloc().\n");
 			send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
 			return;
@@ -4967,7 +4967,7 @@
 	}
 	else {
 		/* free previously allocated memory */
-		pool_free_to(apools.sessid, local_asession.sessid);
+		pool_free2(apools.sessid, local_asession.sessid);
 	}
 	
 	tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);