Use structure size and version for event description extensibility
[libside.git] / include / side / macros.h
index 892a21a2ca25e5aec206fb530873ae973cc83340..10fcb66ffff07255314ed05a95f53cb2b2ad3cd4 100644 (file)
@@ -8,6 +8,8 @@
 
 #include <stddef.h>
 #include <limits.h>
+#include <stdint.h>
+#include <side/endian.h>
 
 /* Helper macros */
 
 
 #define SIDE_PARAM(...)        __VA_ARGS__
 
+#define side_offsetofend(type, member) \
+       (offsetof(type, member) + sizeof(((type *)0)->member))
+
+/*
+ * SIDE_PARAM_SELECT_ARG1
+ *
+ * Select second argument. Use inside macros to implement optional last
+ * macro argument, such as:
+ *
+ * #define macro(_a, _b, _c, _optional...) \
+ *     SIDE_PARAM_SELECT_ARG1(_, ##_optional, do_default_macro())
+ *
+ * This macro is far from pretty, but attempts to create a cleaner layer
+ * on top fails for various reasons:
+ *
+ * - The libside API needs to use the default argument selection as an
+ *   argument to itself (recursively), e.g. for fields and for types, so
+ *   using the argument selection within an extra layer of macro fails
+ *   because the extra layer cannot expand recursively.
+ * - Attempts to make the extra layer of macro support recursion through
+ *   another layer of macros which expands all arguments failed because
+ *   the optional argument may contain commas, and is therefore expanded
+ *   into multiple arguments before argument selection, which fails to
+ *   select the optional argument content after its first comma.
+ */
+#define SIDE_PARAM_SELECT_ARG1(_arg0, _arg1, ...) _arg1
+
+/*
+ * Compile time assertion.
+ * - predicate: boolean expression to evaluate,
+ * - msg: string to print to the user on failure when `static_assert()` is
+ *   supported,
+ * - c_identifier_msg: message to be included in the typedef to emulate a
+ *   static assertion. This parameter must be a valid C identifier as it will
+ *   be used as a typedef name.
+ */
+#ifdef __cplusplus
+#define side_static_assert(predicate, msg, c_identifier_msg)  \
+       static_assert(predicate, msg)
+#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
+#define side_static_assert(predicate, msg, c_identifier_msg)  \
+       _Static_assert(predicate, msg)
+#else
+/*
+ * Evaluates the predicate and emit a compilation error on failure.
+ *
+ * If the predicate evaluates to true, this macro emits a function
+ * prototype with an argument type which is an array of size 0.
+ *
+ * If the predicate evaluates to false, this macro emits a function
+ * prototype with an argument type which is an array of negative size
+ * which is invalid in C and forces a compiler error. The
+ * c_identifier_msg parameter is used as the argument identifier so it
+ * is printed to the user when the error is reported.
+ */
+#define side_static_assert(predicate, msg, c_identifier_msg)  \
+       void side_static_assert_proto(char c_identifier_msg[2*!!(predicate)-1])
+#endif
+
 /*
  * side_container_of - Get the address of an object containing a field.
  *
                (type *)((char *)__ptr - offsetof(type, member));       \
        })
 
-#define side_struct_field_sizeof_bit(_struct, _field) \
-       (sizeof(((_struct * )NULL)->_field) * CHAR_BIT)
+#define side_struct_field_sizeof(_struct, _field) \
+       sizeof(((_struct * )NULL)->_field)
+
+#define SIDE_PACKED    __attribute__((packed))
 
-#if defined(__SIZEOF_LONG__)
-#define SIDE_BITS_PER_LONG     (__SIZEOF_LONG__ * 8)
-#elif defined(_LP64)
-#define SIDE_BITS_PER_LONG     64
+#define side_padding(bytes)    char padding[bytes]
+
+#define side_check_size(_type, _len)                           \
+       side_static_assert(sizeof(_type) == (_len),             \
+               "Unexpected size for type: `" #_type "`",       \
+               unexpected_size_for_type_##_type)
+
+#if (SIDE_BYTE_ORDER == SIDE_LITTLE_ENDIAN)
+#define SIDE_ENDIAN_ORDER(_low, _high)         _low, _high
+#else
+#define SIDE_ENDIAN_ORDER(_low, _high)         _high, _low
+#endif
+
+/*
+ * The side_ptr macros allow defining a pointer type which is suitable
+ * for use by 32-bit, 64-bit and 128-bit kernels without compatibility
+ * code, while preserving information about the pointer type.
+ *
+ * Those pointers are stored as 128-bit integers, and the type of the
+ * actual pointer is kept alongside with the 128-bit pointer value in a
+ * 0-len array within a union.
+ */
+#if (__SIZEOF_POINTER__ <= 8)
+# define side_raw_ptr_t(_type)                                 \
+       union {                                                 \
+               struct {                                        \
+                       uint64_t SIDE_ENDIAN_ORDER(low, high);  \
+               } v;                                            \
+               struct {                                        \
+                       _type t[0];                             \
+               } SIDE_PACKED s;                                \
+               side_padding(16);                               \
+       }
+# define side_ptr_get(_field)                                  \
+       ((__typeof__((_field).s.t[0]))(uintptr_t)(_field).v.low)
+# define side_ptr_set(_field, _ptr)                            \
+       do {                                                    \
+               (_field).v.low = (uint64_t)(uintptr_t)(_ptr);   \
+               (_field).v.high = 0;                            \
+       } while (0)
+
+/* Keep the correct field init order to make old g++ happy. */
+# if (SIDE_BYTE_ORDER == SIDE_LITTLE_ENDIAN)
+#  define SIDE_PTR_INIT(...)                                   \
+       {                                                       \
+               .v = {                                          \
+                       .low = (uintptr_t) (__VA_ARGS__),       \
+                       .high = 0,                              \
+               },                                              \
+       }
+# else
+#  define SIDE_PTR_INIT(...)                                   \
+       {                                                       \
+               .v = {                                          \
+                       .high = 0,                              \
+                       .low = (uintptr_t) (__VA_ARGS__),       \
+               },                                              \
+       }
+# endif
+#elif (__SIZEOF_POINTER__ == 16)
+# define side_raw_ptr_t(_type)                                 \
+       union {                                                 \
+               uintptr_t v;                                    \
+               struct {                                        \
+                       _type t[0];                             \
+               } SIDE_PACKED s;                                \
+               side_padding(16);                               \
+       }
+# define side_ptr_get(_field)                                  \
+       ((__typeof__((_field).s.t[0]))(_field).v)
+# define side_ptr_set(_field, _ptr)                            \
+       do {                                                    \
+               (_field).v = (uintptr_t)(_ptr);                 \
+       } while (0)
+# define SIDE_PTR_INIT(...)    { .v = (uintptr_t) (__VA_ARGS__) }
 #else
-#define SIDE_BITS_PER_LONG     32
+# error "Unsupported pointer size"
 #endif
 
+#define side_ptr_t(_type)      side_raw_ptr_t(_type *)
+#define side_func_ptr_t(_type) side_raw_ptr_t(_type)
+
+side_static_assert(sizeof(side_ptr_t(int)) == 16,
+       "Unexpected size for side_ptr_t",
+       unexpected_size_side_ptr_t);
+
+/*
+ * side_enum_t allows defining fixed-sized enumerations while preserving
+ * typing information.
+ */
+#define side_enum_t(_enum_type, _size_type)                    \
+       union {                                                 \
+               _size_type v;                                   \
+               struct {                                        \
+                       _enum_type t[0];                        \
+               } SIDE_PACKED s;                                \
+       }
+
+#define side_enum_get(_field)                                  \
+       ((__typeof__((_field).s.t[0]))(_field).v)
+
+#define side_enum_set(_field, _v)                              \
+       do {                                                    \
+               (_field).v = (_v);                              \
+       } while (0)
+
+#define SIDE_ENUM_INIT(...)    { .v = (__VA_ARGS__) }
+
 #endif /* _SIDE_MACROS_H */
This page took 0.026644 seconds and 4 git commands to generate.