From 6ea0e3e2f813e14ed4bb8f4a74024ecbcb05b32b Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 5 Aug 2025 18:26:37 +0200 Subject: [PATCH] MINOR: pools: add macros to register aligned pools This adds an alignment argument to create_pool_from_loc() and completes the existing low-level macros with new ones that expose the alignment and the new macros permit to specify it. For now they're not used. --- include/haproxy/pool.h | 38 +++++++++++++++++++++++++++++--------- src/pool.c | 6 ++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/include/haproxy/pool.h b/include/haproxy/pool.h index a089f035a..bc13098cf 100644 --- a/include/haproxy/pool.h +++ b/include/haproxy/pool.h @@ -33,36 +33,52 @@ /* 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) \ +#define __REGISTER_POOL(_line, _ptr, _name, _size, _align) \ static struct pool_registration __pool_reg_##_line = { \ .name = _name, \ .file = __FILE__, \ .line = __LINE__, \ .size = _size, \ .flags = MEM_F_STATREG, \ - .align = 0, \ + .align = _align, \ }; \ 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) +#define _REGISTER_POOL(line, ptr, name, size, align) \ + __REGISTER_POOL(line, ptr, name, size, align) /* This registers a call to create_pool_callback(ptr) with these args */ #define REGISTER_POOL(ptr, name, size) \ - _REGISTER_POOL(__LINE__, ptr, name, size) + _REGISTER_POOL(__LINE__, ptr, name, size, 0) /* 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(__LINE__, &ptr, name, size) + _REGISTER_POOL(__LINE__, &ptr, name, size, 0) /* 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(__LINE__, &ptr, name, size) + _REGISTER_POOL(__LINE__, &ptr, name, size, 0) + +/*** below are the aligned pool macros, taking one extra arg for alignment ***/ + +/* This registers a call to create_pool_callback(ptr) with these args */ +#define REGISTER_ALIGNED_POOL(ptr, name, size, align) \ + _REGISTER_POOL(__LINE__, ptr, name, size, align) + +/* This macro declares an aligned pool head and registers its creation */ +#define DECLARE_ALIGNED_POOL(ptr, name, size, align) \ + struct pool_head *(ptr) __read_mostly = NULL; \ + _REGISTER_POOL(__LINE__, &ptr, name, size, align) + +/* This macro declares a static aligned pool head and registers its creation */ +#define DECLARE_STATIC_ALIGNED_POOL(ptr, name, size, align) \ + static struct pool_head *(ptr) __read_mostly; \ + _REGISTER_POOL(__LINE__, &ptr, name, size, align) /* 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 @@ -143,7 +159,8 @@ unsigned long long pool_total_allocated(void); unsigned long long pool_total_used(void); void pool_flush(struct pool_head *pool); void pool_gc(struct pool_head *pool_ctx); -struct pool_head *create_pool_with_loc(const char *name, unsigned int size, unsigned int flags, const char *file, unsigned int line); +struct pool_head *create_pool_with_loc(const char *name, unsigned int size, unsigned int align, + unsigned int flags, const char *file, unsigned int line); struct pool_head *create_pool_from_reg(const char *name, struct pool_registration *reg); void create_pool_callback(struct pool_head **ptr, char *name, struct pool_registration *reg); void *pool_destroy(struct pool_head *pool); @@ -153,7 +170,10 @@ void __pool_free(struct pool_head *pool, void *ptr); void pool_inspect_item(const char *msg, struct pool_head *pool, const void *item, const void *caller, ssize_t ofs); #define create_pool(name, size, flags) \ - create_pool_with_loc(name, size, flags, __FILE__, __LINE__) + create_pool_with_loc(name, size, 0, flags, __FILE__, __LINE__) + +#define create_aligned_pool(name, size, align, flags) \ + create_pool_with_loc(name, size, align, flags, __FILE__, __LINE__) /****************** Thread-local cache management ******************/ diff --git a/src/pool.c b/src/pool.c index cafc51094..5fa5e5530 100644 --- a/src/pool.c +++ b/src/pool.c @@ -293,8 +293,10 @@ static int mem_should_fail(const struct pool_head *pool) * The name must be a stable pointer during all the program's life time. * The file and line are passed to store the registration location in the * registration struct. Use create_pool() instead which does it for free. + * The alignment will be stored as-is in the registration. */ -struct pool_head *create_pool_with_loc(const char *name, unsigned int size, unsigned int flags, +struct pool_head *create_pool_with_loc(const char *name, unsigned int size, + unsigned int align, unsigned int flags, const char *file, unsigned int line) { struct pool_registration *reg; @@ -309,7 +311,7 @@ struct pool_head *create_pool_with_loc(const char *name, unsigned int size, unsi reg->line = line; reg->size = size; reg->flags = flags; - reg->align = 0; + reg->align = align; pool = create_pool_from_reg(name, reg); if (!pool)