]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: memory: add a callback function to create a pool
authorWilly Tarreau <w@1wt.eu>
Mon, 26 Nov 2018 10:44:35 +0000 (11:44 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 26 Nov 2018 18:50:32 +0000 (19:50 +0100)
The new function create_pool_callback() takes 3 args including the
return pointer, and creates a pool with the specified name and size.
In case of allocation error, it emits an error message and returns.

The new macro REGISTER_POOL() registers a callback using this function
and will be usable to request some pools creation and guarantee that
the allocation will be checked. An even simpler approach is to use
DECLARE_POOL() and DECLARE_STATIC_POOL() which declare and register
the pool.

include/common/memory.h
src/memory.c

index 57d6ba63e69f20f76a293fa48a1d5e8ca61a8343..5bed60294359b5ed843aa900952d4a58cc787894 100644 (file)
@@ -32,6 +32,7 @@
 #include <common/config.h>
 #include <common/mini-clist.h>
 #include <common/hathreads.h>
+#include <common/initcall.h>
 
 #ifndef DEBUG_DONT_SHARE_POOLS
 #define MEM_F_SHARED   0x1
@@ -116,6 +117,21 @@ void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
  * 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))
+
+/* This macro declares a pool head <ptr> and registers its creation */
+#define DECLARE_POOL(ptr, name, size)   \
+       struct pool_head *(ptr) = NULL; \
+       REGISTER_POOL(&ptr, name, size)
+
+/* This macro declares a static pool head <ptr> and registers its creation */
+#define DECLARE_STATIC_POOL(ptr, name, size) \
+       static struct pool_head *(ptr);      \
+       REGISTER_POOL(&ptr, name, size)
 
 /* Dump statistics on pools usage.
  */
index 1554243f08736cd30594f37cf66d64d43df50adc..587702a2ea3a22fbb3c765f4e538d3af5cdc5344 100644 (file)
@@ -9,6 +9,7 @@
  * 2 of the License, or (at your option) any later version.
  *
  */
+#include <errno.h>
 
 #include <types/applet.h>
 #include <types/cli.h>
@@ -521,6 +522,21 @@ static int cli_io_handler_dump_pools(struct appctx *appctx)
        return 1;
 }
 
+/* callback used to create early pool <name> of size <size> and store the
+ * resulting pointer into <ptr>. If the allocation fails, it quits with after
+ * emitting an error message.
+ */
+void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size)
+{
+       *ptr = create_pool(name, size, MEM_F_SHARED);
+       if (!*ptr) {
+               ha_alert("Failed to allocate pool '%s' of size %u : %s. Aborting.\n",
+                        name, size, strerror(errno));
+               exit(1);
+       }
+}
+
+
 /* register cli keywords */
 static struct cli_kw_list cli_kws = {{ },{
        { { "show", "pools",  NULL }, "show pools     : report information about the memory pools usage", NULL, cli_io_handler_dump_pools },