diff --git a/include/common/memory.h b/include/common/memory.h index 57d6ba63e..5bed60294 100644 --- a/include/common/memory.h +++ b/include/common/memory.h @@ -32,6 +32,7 @@ #include #include #include +#include #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 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 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. */ diff --git a/src/memory.c b/src/memory.c index 1554243f0..587702a2e 100644 --- a/src/memory.c +++ b/src/memory.c @@ -9,6 +9,7 @@ * 2 of the License, or (at your option) any later version. * */ +#include #include #include @@ -521,6 +522,21 @@ static int cli_io_handler_dump_pools(struct appctx *appctx) return 1; } +/* callback used to create early pool of size and store the + * resulting pointer into . 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 },