]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: compiler: add FIXED_SIZE(size, type, name) macro
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 22 Oct 2025 17:23:02 +0000 (19:23 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Wed, 22 Oct 2025 18:52:12 +0000 (20:52 +0200)
FIXED_SIZE() macro can be used to instruct the compiler that the struct
member named <name>, handled as <type>, must be stored using <size> bytes
and that even if the type used is actualler smaller than the expected size

FIXED_SIZE_ARRAY(), similar to FIXED_SIZE() but for arrays: it takes an
extra argument which is the number of members.

They may be used for portability concerns to ensure a structure mapping
remains consistent between platforms.

include/haproxy/compiler.h

index b5b3b083e61e9ea7d180b8b47b377188f248f293..48235dc77b4db2e8f1199a856689437d424fe063 100644 (file)
 #  define ALWAYS_PAD(x)      _ALWAYS_PAD(x, __LINE__)
 #endif
 
+/* force the struct member named <name> handled as <type> to be stored using
+ * <size> bytes, even if the type used is actually smaller than the
+ * expected <size>.
+ *
+ * This can be useful to ensure struct mapping consistency between
+ * platforms for which native types may differ in size.
+ *
+ * /!\ you must ensure <size> cannot be smaller than actual member size
+ * on supported platforms, because it would result in undefined behaviors
+ * on such systems.
+ */
+#ifndef FIXED_SIZE
+#  define _FIXED_SIZE(size, type, name) union { char __fixed_##name[size]; type name; };
+#  define FIXED_SIZE(size, type, name) _FIXED_SIZE(size, type, name)
+#endif
+
+/* same as FIXED_SIZE_ARRAY but for arrays */
+#ifndef FIXED_SIZE_ARRAY
+#  define _FIXED_SIZE_ARRAY(size, number, type, name) union { char __fixed_##name[size * number]; type name[number]; };
+#  define FIXED_SIZE_ARRAY(size, number, type, name) _FIXED_SIZE_ARRAY(size, number, type, name)
+#endif
+
 /* The THREAD_LOCAL type attribute defines thread-local storage and is defined
  * to __thread when threads are enabled or empty when disabled.
  */