From: Maxime Henrion Date: Tue, 9 Dec 2025 15:51:33 +0000 (-0500) Subject: CLEANUP: improvements to the alignment macros X-Git-Tag: v3.4-dev1~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=74719dc4577e8e723200e7a1c39da9ae522c0654;p=thirdparty%2Fhaproxy.git CLEANUP: improvements to the alignment macros - It is now possible to use the THREAD_ALIGN and THREAD_ALIGNED macros without a parameter. In this case, we automatically align on the cache line size. - The cache line size is set to 64 by default to match the current code, but it can be overridden on the command line. - This required moving the DEFVAL/DEFNULL/DEFZERO macros to compiler.h instead of tools-t.h, to avoid namespace pollution if we included tools-t.h from compiler.h. --- diff --git a/include/haproxy/compiler.h b/include/haproxy/compiler.h index b5b3b083e..f37d473e2 100644 --- a/include/haproxy/compiler.h +++ b/include/haproxy/compiler.h @@ -31,6 +31,23 @@ #include #endif +/* DEFVAL() returns either the second argument as-is, or if absent. This + * is for use in macros arguments. + */ +#define DEFVAL(_def,...) _FIRST_ARG(NULL, ##__VA_ARGS__, (_def)) + +/* DEFNULL() returns either the argument as-is, or NULL if absent. This is for + * use in macros arguments. + */ +#define DEFNULL(...) DEFVAL(NULL, ##__VA_ARGS__) + +/* DEFZERO() returns either the argument as-is, or 0 if absent. This is for + * use in macros arguments. + */ +#define DEFZERO(...) DEFVAL(0, ##__VA_ARGS__) + +#define _FIRST_ARG(a, b, ...) b + /* * Gcc before 3.0 needs [0] to declare a variable-size array */ @@ -415,6 +432,13 @@ * for multi_threading, see THREAD_PAD() below. * \*****************************************************************************/ +/* Cache line size for alignment purposes. This value is incorrect for some + * Apple CPUs which have 128 bytes cache lines. + */ +#ifndef CACHELINE_SIZE +#define CACHELINE_SIZE 64 +#endif + /* sets alignment for current field or variable */ #ifndef ALIGNED #define ALIGNED(x) __attribute__((aligned(x))) @@ -438,12 +462,12 @@ #endif #endif -/* sets alignment for current field or variable only when threads are enabled. - * Typically used to respect cache line alignment to avoid false sharing. +/* Sets alignment for current field or variable only when threads are enabled. + * When no parameters are provided, we align to the cache line size. */ #ifndef THREAD_ALIGNED #ifdef USE_THREAD -#define THREAD_ALIGNED(x) __attribute__((aligned(x))) +#define THREAD_ALIGNED(...) ALIGNED(DEFVAL(CACHELINE_SIZE, ##__VA_ARGS__)) #else #define THREAD_ALIGNED(x) #endif @@ -476,13 +500,12 @@ #endif #endif -/* add an optional alignment for next fields in a structure, only when threads - * are enabled. Typically used to respect cache line alignment to avoid false - * sharing. +/* Add an optional alignment for next fields in a structure, only when threads + * are enabled. When no parameters are provided, we align to the cache line size. */ #ifndef THREAD_ALIGN #ifdef USE_THREAD -#define THREAD_ALIGN(x) union { } ALIGNED(x) +#define THREAD_ALIGN(...) union { } ALIGNED(DEFVAL(CACHELINE_SIZE, ##__VA_ARGS__)) #else #define THREAD_ALIGN(x) #endif diff --git a/include/haproxy/tools-t.h b/include/haproxy/tools-t.h index ef3e136a4..77911dd85 100644 --- a/include/haproxy/tools-t.h +++ b/include/haproxy/tools-t.h @@ -47,23 +47,6 @@ /* return the largest possible integer of type , with all bits set */ #define MAX_RANGE(ret) (~(typeof(ret))0) -/* DEFVAL() returns either the second argument as-is, or if absent. This - * is for use in macros arguments. - */ -#define DEFVAL(_def,...) _FIRST_ARG(NULL, ##__VA_ARGS__, (_def)) - -/* DEFNULL() returns either the argument as-is, or NULL if absent. This is for - * use in macros arguments. - */ -#define DEFNULL(...) DEFVAL(NULL, ##__VA_ARGS__) - -/* DEFZERO() returns either the argument as-is, or 0 if absent. This is for - * use in macros arguments. - */ -#define DEFZERO(...) DEFVAL(0, ##__VA_ARGS__) - -#define _FIRST_ARG(a, b, ...) b - /* options flags for parse_line() */ #define PARSE_OPT_SHARP 0x00000001 // '#' ends the line #define PARSE_OPT_BKSLASH 0x00000002 // '\' escapes chars