[MEDIUM] stick-tables: add a reference counter to each entry

We'll soon have to maintain links from sessions to entries, so let's
add a refcount in entries to avoid purging them if it's not null.
diff --git a/src/stick_table.c b/src/stick_table.c
index 5723dff..09cb96b 100644
--- a/src/stick_table.c
+++ b/src/stick_table.c
@@ -64,6 +64,7 @@
 static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
 {
 	memset((void *)ts - t->data_size, 0, t->data_size);
+	ts->ref_cnt = 0;
 	ts->key.node.leaf_p = NULL;
 	ts->exp.node.leaf_p = NULL;
 	return ts;
@@ -78,6 +79,7 @@
 	struct stksess *ts;
 	struct eb32_node *eb;
 	int batched = 0;
+	int looped = 0;
 
 	eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
 
@@ -86,8 +88,12 @@
 		if (unlikely(!eb)) {
 			/* we might have reached the end of the tree, typically because
 			 * <now_ms> is in the first half and we're first scanning the last
-			 * half. Let's loop back to the beginning of the tree now.
+			 * half. Let's loop back to the beginning of the tree now if we
+			 * have not yet visited it.
 			 */
+			if (looped)
+				break;
+			looped = 1;
 			eb = eb32_first(&t->exps);
 			if (likely(!eb))
 				break;
@@ -97,6 +103,10 @@
 		ts = eb32_entry(eb, struct stksess, exp);
 		eb = eb32_next(eb);
 
+		/* don't delete an entry which is currently referenced */
+		if (ts->ref_cnt)
+			continue;
+
 		eb32_delete(&ts->exp);
 
 		if (ts->expire != ts->exp.key) {
@@ -223,6 +233,7 @@
 {
 	struct stksess *ts;
 	struct eb32_node *eb;
+	int looped = 0;
 
 	eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
 
@@ -230,8 +241,12 @@
 		if (unlikely(!eb)) {
 			/* we might have reached the end of the tree, typically because
 			 * <now_ms> is in the first half and we're first scanning the last
-			 * half. Let's loop back to the beginning of the tree now.
+			 * half. Let's loop back to the beginning of the tree now if we
+			 * have not yet visited it.
 			 */
+			if (looped)
+				break;
+			looped = 1;
 			eb = eb32_first(&t->exps);
 			if (likely(!eb))
 				break;
@@ -247,6 +262,10 @@
 		ts = eb32_entry(eb, struct stksess, exp);
 		eb = eb32_next(eb);
 
+		/* don't delete an entry which is currently referenced */
+		if (ts->ref_cnt)
+			continue;
+
 		eb32_delete(&ts->exp);
 
 		if (!tick_is_expired(ts->expire, now_ms)) {