]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: improvements to the alignment macros
authorMaxime Henrion <mhenrion@haproxy.com>
Tue, 9 Dec 2025 15:51:33 +0000 (10:51 -0500)
committerOlivier Houchard <cognet@ci0.org>
Tue, 9 Dec 2025 16:05:52 +0000 (17:05 +0100)
- 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.

include/haproxy/compiler.h
include/haproxy/tools-t.h

index b5b3b083e61e9ea7d180b8b47b377188f248f293..f37d473e251e3d997756a4e9063d587fd83b2e36 100644 (file)
 #include <stdlib.h>
 #endif
 
+/* DEFVAL() returns either the second argument as-is, or <def> 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
  */
  * 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)))
 #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
 #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
index ef3e136a4fa03793a33586d39bf2cd023e1c7b49..77911dd85d9590993f868d648081cefa0eca74f1 100644 (file)
 /* return the largest possible integer of type <ret>, with all bits set */
 #define MAX_RANGE(ret) (~(typeof(ret))0)
 
-/* DEFVAL() returns either the second argument as-is, or <def> 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