#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
/* 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