REORG: include: move common/memory.h to haproxy/pool.h

Now the file is ready to be stored into its final destination. A few
minor reorderings were performed to keep the file properly organized,
making the various sections more visible (cache & lockless).

In addition and to stay consistent, memory.c was renamed to pool.c.
diff --git a/Makefile b/Makefile
index f7155d0..6e4cad6 100644
--- a/Makefile
+++ b/Makefile
@@ -802,7 +802,7 @@
        src/payload.o src/vars.o src/debug.o src/mux_pt.o src/http_act.o       \
        src/h2.o src/queue.o src/fd.o src/proto_uxst.o src/lb_chash.o          \
        src/ring.o src/frontend.o src/raw_sock.o src/xprt_handshake.o          \
-       src/htx.o src/memory.o src/applet.o src/channel.o src/signal.o         \
+       src/htx.o src/pool.o src/applet.o src/channel.o src/signal.o           \
        src/lb_fwrr.o src/ev_select.o src/sink.o src/http_conv.o               \
        src/proto_sockpair.o src/mworker-prog.o src/activity.o src/lb_fwlc.o   \
        src/http.o src/lb_fas.o src/uri_auth.o src/thread.o src/regex.o        \
diff --git a/contrib/prometheus-exporter/service-prometheus.c b/contrib/prometheus-exporter/service-prometheus.c
index 6afa43f..fe3431c 100644
--- a/contrib/prometheus-exporter/service-prometheus.c
+++ b/contrib/prometheus-exporter/service-prometheus.c
@@ -17,7 +17,7 @@
 #include <common/cfgparse.h>
 #include <common/buffer.h>
 #include <common/htx.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/list.h>
 
 #include <types/global.h>
diff --git a/include/common/buffer.h b/include/common/buffer.h
index 132cbff..e9aae25 100644
--- a/include/common/buffer.h
+++ b/include/common/buffer.h
@@ -31,7 +31,7 @@
 #include <common/chunk.h>
 #include <import/ist.h>
 #include <haproxy/istbuf.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 #include <proto/activity.h>
 
diff --git a/include/common/chunk.h b/include/common/chunk.h
index 46dfae7..279b802 100644
--- a/include/common/chunk.h
+++ b/include/common/chunk.h
@@ -28,7 +28,7 @@
 #include <haproxy/api.h>
 #include <haproxy/buf-t.h>
 #include <import/ist.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 
 extern struct pool_head *pool_head_trash;
diff --git a/include/common/hpack-tbl.h b/include/common/hpack-tbl.h
index 1d2a8bd..07e0a9f 100644
--- a/include/common/hpack-tbl.h
+++ b/include/common/hpack-tbl.h
@@ -31,7 +31,7 @@
 #include <haproxy/api.h>
 #include <common/http-hdr.h>
 #include <import/ist.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 /* Dynamic Headers Table, usable for tables up to 4GB long and values of 64kB-1.
  * The model can be improved by using offsets relative to the table entry's end
diff --git a/include/common/memory.h b/include/haproxy/pool.h
similarity index 86%
rename from include/common/memory.h
rename to include/haproxy/pool.h
index d612263..a62d06e 100644
--- a/include/common/memory.h
+++ b/include/haproxy/pool.h
@@ -1,8 +1,8 @@
 /*
- * include/common/memory.h
+ * include/haproxy/pool.h
  * Memory management definitions..
  *
- * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu
+ * Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -19,8 +19,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#ifndef _COMMON_MEMORY_H
-#define _COMMON_MEMORY_H
+#ifndef _HAPROXY_POOL_H
+#define _HAPROXY_POOL_H
 
 #include <string.h>
 
@@ -31,36 +31,6 @@
 #include <haproxy/pool-t.h>
 #include <haproxy/thread.h>
 
-#ifdef CONFIG_HAP_LOCAL_POOLS
-extern struct pool_head pool_base_start[MAX_BASE_POOLS];
-extern unsigned int pool_base_count;
-extern struct pool_cache_head pool_cache[][MAX_BASE_POOLS];
-extern struct list pool_lru_head[MAX_THREADS];
-extern THREAD_LOCAL size_t pool_cache_bytes;   /* total cache size */
-extern THREAD_LOCAL size_t pool_cache_count;   /* #cache objects   */
-#endif
-
-/* poison each newly allocated area with this byte if >= 0 */
-extern int mem_poison_byte;
-
-/* Allocates new entries for pool <pool> until there are at least <avail> + 1
- * available, then returns the last one for immediate use, so that at least
- * <avail> are left available in the pool upon return. NULL is returned if the
- * last entry could not be allocated. It's important to note that at least one
- * allocation is always performed even if there are enough entries in the pool.
- * A call to the garbage collector is performed at most once in case malloc()
- * returns an error, before returning NULL.
- */
-void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
-void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
-
-/* Try to find an existing shared pool with the same characteristics and
- * returns it, otherwise creates this one. NULL is returned if no memory
- * is available for a new creation.
- */
-struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
-void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size);
-
 /* This registers a call to create_pool_callback(ptr, name, size) */
 #define REGISTER_POOL(ptr, name, size)  \
 	INITCALL3(STG_POOL, create_pool_callback, (ptr), (name), (size))
@@ -75,48 +45,23 @@
 	static struct pool_head *(ptr);      \
 	REGISTER_POOL(&ptr, name, size)
 
-/* Dump statistics on pools usage.
- */
+/* poison each newly allocated area with this byte if >= 0 */
+extern int mem_poison_byte;
+
+void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
+void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
 void dump_pools_to_trash();
 void dump_pools(void);
 int pool_total_failures();
 unsigned long pool_total_allocated();
 unsigned long pool_total_used();
-
-/*
- * This function frees whatever can be freed in pool <pool>.
- */
 void pool_flush(struct pool_head *pool);
-
-/*
- * This function frees whatever can be freed in all pools, but respecting
- * the minimum thresholds imposed by owners.
- *
- * <pool_ctx> is used when pool_gc is called to release resources to allocate
- * an element in __pool_refill_alloc. It is important because <pool_ctx> is
- * already locked, so we need to skip the lock here.
- */
 void pool_gc(struct pool_head *pool_ctx);
-
-/*
- * This function destroys a pull by freeing it completely.
- * This should be called only under extreme circumstances.
- */
+struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
+void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size);
 void *pool_destroy(struct pool_head *pool);
 void pool_destroy_all();
 
-/* returns the pool index for pool <pool>, or -1 if this pool has no index */
-static inline ssize_t pool_get_index(const struct pool_head *pool)
-{
-#ifdef CONFIG_HAP_LOCAL_POOLS
-	size_t idx;
-
-	idx = pool - pool_base_start;
-	if (idx < MAX_BASE_POOLS)
-		return idx;
-#endif
-	return -1;
-}
 
 /* returns true if the pool is considered to have too many free objects */
 static inline int pool_is_crowded(const struct pool_head *pool)
@@ -125,9 +70,31 @@
 	       (int)(pool->allocated - pool->used) >= pool->minavail;
 }
 
+
 #ifdef CONFIG_HAP_LOCAL_POOLS
+
+/****************** Thread-local cache management ******************/
+
+extern struct pool_head pool_base_start[MAX_BASE_POOLS];
+extern unsigned int pool_base_count;
+extern struct pool_cache_head pool_cache[][MAX_BASE_POOLS];
+extern struct list pool_lru_head[MAX_THREADS];
+extern THREAD_LOCAL size_t pool_cache_bytes;   /* total cache size */
+extern THREAD_LOCAL size_t pool_cache_count;   /* #cache objects   */
+
 void pool_evict_from_cache();
 
+/* returns the pool index for pool <pool>, or -1 if this pool has no index */
+static inline ssize_t pool_get_index(const struct pool_head *pool)
+{
+	size_t idx;
+
+	idx = pool - pool_base_start;
+	if (idx < MAX_BASE_POOLS)
+		return idx;
+	return -1;
+}
+
 /* Tries to retrieve an object from the local pool cache corresponding to pool
  * <pool>. Returns NULL if none is available.
  */
@@ -175,9 +142,19 @@
 	if (unlikely(pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE))
 		pool_evict_from_cache(pool, ptr, idx);
 }
+
+#else // CONFIG_HAP_LOCAL_POOLS
+
+/* always return index -1 when thread-local pools are disabled */
+#define pool_get_index(pool) ((ssize_t)-1)
+
 #endif // CONFIG_HAP_LOCAL_POOLS
 
+
 #ifdef CONFIG_HAP_LOCKLESS_POOLS
+
+/****************** Lockless pools implementation ******************/
+
 /*
  * Returns a pointer to type <type> taken from the pool <pool_type> if
  * available, otherwise returns NULL. No malloc() is attempted, and poisonning
@@ -232,6 +209,9 @@
 }
 
 #else /* CONFIG_HAP_LOCKLESS_POOLS */
+
+/****************** Locked pools implementation ******************/
+
 /*
  * Returns a pointer to type <type> taken from the pool <pool_type> if
  * available, otherwise returns NULL. No malloc() is attempted, and poisonning
@@ -284,6 +264,8 @@
 #endif /* CONFIG_HAP_LOCKLESS_POOLS */
 
 
+/****************** Common high-level code ******************/
+
 static inline void *pool_get_first(struct pool_head *pool)
 {
 	void *p;
@@ -384,8 +366,7 @@
 	}
 }
 
-
-#endif /* _COMMON_MEMORY_H */
+#endif /* _HAPROXY_POOL_H */
 
 /*
  * Local variables:
diff --git a/include/proto/applet.h b/include/proto/applet.h
index aaa4903..80744ba 100644
--- a/include/proto/applet.h
+++ b/include/proto/applet.h
@@ -25,7 +25,7 @@
 #include <stdlib.h>
 
 #include <haproxy/api.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/list.h>
 #include <types/applet.h>
 #include <proto/task.h>
diff --git a/include/proto/connection.h b/include/proto/connection.h
index 7ffe1fd..de976d7 100644
--- a/include/proto/connection.h
+++ b/include/proto/connection.h
@@ -24,7 +24,7 @@
 
 #include <haproxy/api.h>
 #include <import/ist.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <types/connection.h>
 #include <types/listener.h>
 #include <proto/fd.h>
diff --git a/include/proto/log.h b/include/proto/log.h
index ef792c8..48802dc 100644
--- a/include/proto/log.h
+++ b/include/proto/log.h
@@ -27,7 +27,7 @@
 #include <syslog.h>
 
 #include <haproxy/api.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/thread-t.h>
 
 #include <types/log.h>
diff --git a/include/proto/obj_type.h b/include/proto/obj_type.h
index f4816b1..402d084 100644
--- a/include/proto/obj_type.h
+++ b/include/proto/obj_type.h
@@ -23,7 +23,7 @@
 #define _PROTO_OBJ_TYPE_H
 
 #include <haproxy/api.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <types/applet.h>
 #include <types/connection.h>
 #include <types/listener.h>
diff --git a/include/proto/queue.h b/include/proto/queue.h
index 9bec9b4..d4da40c 100644
--- a/include/proto/queue.h
+++ b/include/proto/queue.h
@@ -23,7 +23,7 @@
 #define _PROTO_QUEUE_H
 
 #include <haproxy/api.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 #include <types/proxy.h>
 #include <types/queue.h>
diff --git a/include/proto/session.h b/include/proto/session.h
index 522e43f..866217e 100644
--- a/include/proto/session.h
+++ b/include/proto/session.h
@@ -24,7 +24,7 @@
 
 #include <haproxy/api.h>
 #include <common/buffer.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 #include <types/global.h>
 #include <types/session.h>
diff --git a/include/proto/stream.h b/include/proto/stream.h
index b821cba..80c56ef 100644
--- a/include/proto/stream.h
+++ b/include/proto/stream.h
@@ -23,7 +23,7 @@
 #define _PROTO_STREAM_H
 
 #include <haproxy/api.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <types/action.h>
 #include <types/stream.h>
 #include <proto/fd.h>
diff --git a/include/proto/task.h b/include/proto/task.h
index d5cded3..5a3d021 100644
--- a/include/proto/task.h
+++ b/include/proto/task.h
@@ -26,7 +26,7 @@
 #include <sys/time.h>
 
 #include <haproxy/api.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/intops.h>
 #include <haproxy/list.h>
 #include <common/ticks.h>
diff --git a/src/action.c b/src/action.c
index cb897b6..1125eca 100644
--- a/src/action.c
+++ b/src/action.c
@@ -11,7 +11,7 @@
  */
 
 #include <haproxy/api.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/list.h>
 #include <common/standard.h>
 
diff --git a/src/buffer.c b/src/buffer.c
index c8d7a69..ec07e76 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -16,7 +16,7 @@
 
 #include <haproxy/api.h>
 #include <common/buffer.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 #include <types/global.h>
 
diff --git a/src/cfgparse.c b/src/cfgparse.c
index a1917fa..373a321 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -37,7 +37,7 @@
 #include <common/cfgparse.h>
 #include <common/chunk.h>
 #include <haproxy/errors.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <common/standard.h>
 #include <haproxy/time.h>
 #include <common/uri_auth.h>
diff --git a/src/compression.c b/src/compression.c
index 154d629..45471c3 100644
--- a/src/compression.c
+++ b/src/compression.c
@@ -29,7 +29,7 @@
 #include <haproxy/api.h>
 #include <common/cfgparse.h>
 #include <haproxy/thread.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 #include <types/global.h>
 #include <types/compression.h>
diff --git a/src/flt_spoe.c b/src/flt_spoe.c
index df67a29..52771ef 100644
--- a/src/flt_spoe.c
+++ b/src/flt_spoe.c
@@ -15,7 +15,7 @@
 #include <haproxy/api.h>
 #include <common/cfgparse.h>
 #include <haproxy/thread.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/time.h>
 
 #include <types/arg.h>
diff --git a/src/haproxy.c b/src/haproxy.c
index a2ceeec..917b14f 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -85,7 +85,7 @@
 #include <common/cfgparse.h>
 #include <common/chunk.h>
 #include <haproxy/errors.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/list.h>
 #include <common/namespace.h>
 #include <common/net_helper.h>
diff --git a/src/http_acl.c b/src/http_acl.c
index fe71658..c55c720 100644
--- a/src/http_acl.c
+++ b/src/http_acl.c
@@ -19,7 +19,7 @@
 #include <haproxy/api.h>
 #include <common/chunk.h>
 #include <common/http.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <common/standard.h>
 #include <haproxy/version.h>
 
diff --git a/src/http_act.c b/src/http_act.c
index da57d8f..8e394cc 100644
--- a/src/http_act.c
+++ b/src/http_act.c
@@ -20,7 +20,7 @@
 #include <common/cfgparse.h>
 #include <common/chunk.h>
 #include <common/http.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <common/standard.h>
 #include <common/uri_auth.h>
 #include <haproxy/version.h>
diff --git a/src/http_conv.c b/src/http_conv.c
index 1099521..b82883a 100644
--- a/src/http_conv.c
+++ b/src/http_conv.c
@@ -19,7 +19,7 @@
 #include <haproxy/api.h>
 #include <common/chunk.h>
 #include <common/http.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <common/standard.h>
 #include <haproxy/version.h>
 
diff --git a/src/http_fetch.c b/src/http_fetch.c
index 96d44ab..c253c8d 100644
--- a/src/http_fetch.c
+++ b/src/http_fetch.c
@@ -22,7 +22,7 @@
 #include <common/h1.h>
 #include <common/http.h>
 #include <common/htx.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <common/standard.h>
 #include <haproxy/version.h>
 
diff --git a/src/http_rules.c b/src/http_rules.c
index 6a5a5f3..01a820e 100644
--- a/src/http_rules.c
+++ b/src/http_rules.c
@@ -20,7 +20,7 @@
 #include <common/cfgparse.h>
 #include <common/chunk.h>
 #include <common/http.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <common/standard.h>
 #include <haproxy/version.h>
 
diff --git a/src/pipe.c b/src/pipe.c
index 8c82821..6fe7b05 100644
--- a/src/pipe.c
+++ b/src/pipe.c
@@ -15,7 +15,7 @@
 
 #include <haproxy/api.h>
 #include <haproxy/thread.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 #include <types/global.h>
 #include <types/pipe.h>
diff --git a/src/memory.c b/src/pool.c
similarity index 99%
rename from src/memory.c
rename to src/pool.c
index afab8f8..3a4b34c 100644
--- a/src/memory.c
+++ b/src/pool.c
@@ -19,7 +19,7 @@
 
 #include <common/cfgparse.h>
 #include <haproxy/thread.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/list.h>
 #include <common/standard.h>
 
diff --git a/src/proxy.c b/src/proxy.c
index 5e29ec9..a8ca53a 100644
--- a/src/proxy.c
+++ b/src/proxy.c
@@ -20,7 +20,7 @@
 #include <haproxy/api.h>
 #include <common/cfgparse.h>
 #include <haproxy/errors.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/time.h>
 
 #include <import/eb32tree.h>
diff --git a/src/queue.c b/src/queue.c
index e354e0a..43a21b1 100644
--- a/src/queue.c
+++ b/src/queue.c
@@ -70,7 +70,7 @@
  */
 
 #include <haproxy/api.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/time.h>
 #include <haproxy/thread.h>
 #include <import/eb32tree.h>
diff --git a/src/session.c b/src/session.c
index 3f26cd4..8044169 100644
--- a/src/session.c
+++ b/src/session.c
@@ -13,7 +13,7 @@
 #include <haproxy/api.h>
 #include <common/buffer.h>
 #include <common/http.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 #include <types/global.h>
 #include <types/session.h>
diff --git a/src/stats.c b/src/stats.c
index a089318..0869d54 100644
--- a/src/stats.c
+++ b/src/stats.c
@@ -29,7 +29,7 @@
 #include <haproxy/debug.h>
 #include <common/http.h>
 #include <common/htx.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/list.h>
 #include <common/standard.h>
 #include <common/ticks.h>
diff --git a/src/stick_table.c b/src/stick_table.c
index 0fb0927..bd7f97f 100644
--- a/src/stick_table.c
+++ b/src/stick_table.c
@@ -16,7 +16,7 @@
 
 #include <haproxy/api.h>
 #include <common/cfgparse.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/list.h>
 #include <common/net_helper.h>
 #include <common/standard.h>
diff --git a/src/stream.c b/src/stream.c
index 92c1b50..955bc44 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -19,7 +19,7 @@
 #include <common/buffer.h>
 #include <haproxy/thread.h>
 #include <common/htx.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 
 #include <types/applet.h>
 #include <types/capture.h>
diff --git a/src/task.c b/src/task.c
index 96c09a3..5663aba 100644
--- a/src/task.c
+++ b/src/task.c
@@ -13,7 +13,7 @@
 #include <string.h>
 
 #include <haproxy/api.h>
-#include <common/memory.h>
+#include <haproxy/pool.h>
 #include <haproxy/list.h>
 #include <common/standard.h>
 #include <haproxy/time.h>