From ff62aacb208151ddedb9a0d146b8d721fd1eafb8 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 5 Aug 2025 18:59:15 +0200 Subject: [PATCH] MEDIUM: pools: change the static pool creation to pass a registration Now we're creating statically allocated registrations instead of passing all the parameters and allocating them on the fly. Not only this is simpler to extend (we're limited in number of INITCALL args), but it also leaves all of these in the data segment where they are easier to find when debugging. --- include/haproxy/pool.h | 28 +++++++++++++++++++++++----- src/pool.c | 6 +++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/include/haproxy/pool.h b/include/haproxy/pool.h index 3c9651e40..2c9e2e75b 100644 --- a/include/haproxy/pool.h +++ b/include/haproxy/pool.h @@ -30,19 +30,37 @@ #include #include -/* This registers a call to create_pool_callback(ptr, name, size) */ +/* This creates a pool_reg registers a call to create_pool_callback(ptr) with it. + * Do not use this one, use REGISTER_POOL() instead. + */ +#define __REGISTER_POOL(_line, _ptr, _name, _size) \ + static struct pool_registration __pool_reg_##_line = { \ + .name = _name, \ + .size = _size, \ + .flags = MEM_F_STATREG, \ + .align = 0, \ + }; \ + INITCALL3(STG_POOL, create_pool_callback, (_ptr), (_name), &__pool_reg_##_line); + +/* intermediary level for line number resolution, do not use this one, use + * REGISTER_POOL() instead. + */ +#define _REGISTER_POOL(line, ptr, name, size) \ + __REGISTER_POOL(line, ptr, name, size) + +/* This registers a call to create_pool_callback(ptr) with these args */ #define REGISTER_POOL(ptr, name, size) \ - INITCALL3(STG_POOL, create_pool_callback, (ptr), (name), (size)) + _REGISTER_POOL(__LINE__, ptr, name, size) /* This macro declares a pool head and registers its creation */ #define DECLARE_POOL(ptr, name, size) \ struct pool_head *(ptr) __read_mostly = NULL; \ - REGISTER_POOL(&ptr, name, size) + _REGISTER_POOL(__LINE__, &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) __read_mostly; \ - REGISTER_POOL(&ptr, name, size) + _REGISTER_POOL(__LINE__, &ptr, name, size) /* By default, free objects are linked by a pointer stored at the beginning of * the memory area. When DEBUG_MEMORY_POOLS is set, the allocated area is @@ -125,7 +143,7 @@ void pool_flush(struct pool_head *pool); void pool_gc(struct pool_head *pool_ctx); struct pool_head *create_pool(const char *name, unsigned int size, unsigned int flags); struct pool_head *create_pool_from_reg(const char *name, struct pool_registration *reg); -void create_pool_callback(struct pool_head **ptr, char *name, unsigned int size); +void create_pool_callback(struct pool_head **ptr, char *name, struct pool_registration *reg); void *pool_destroy(struct pool_head *pool); void pool_destroy_all(void); void *__pool_alloc(struct pool_head *pool, unsigned int flags); diff --git a/src/pool.c b/src/pool.c index 507a52032..67ce7060f 100644 --- a/src/pool.c +++ b/src/pool.c @@ -1536,12 +1536,12 @@ static int cli_io_handler_dump_pools(struct appctx *appctx) * 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) +void create_pool_callback(struct pool_head **ptr, char *name, struct pool_registration *reg) { - *ptr = create_pool(name, size, MEM_F_SHARED); + *ptr = create_pool_from_reg(name, reg); if (!*ptr) { ha_alert("Failed to allocate pool '%s' of size %u : %s. Aborting.\n", - name, size, strerror(errno)); + name, reg->size, strerror(errno)); exit(1); } }