/* Ugly structure offset handling macros */
+#define SAME_TYPE(a, b) ({ int _ = ((a) != (b)); !_; })
+#define TYPE_CAST(from, to, what) ( SAME_TYPE(((from) NULL), (what)), ((to) (what)))
+
+#ifdef offsetof
+#define OFFSETOF offsetof
+#else
#define OFFSETOF(s, i) ((size_t) &((s *)0)->i)
-#define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
+#endif
+
+#define SKIP_BACK(s, i, p) ({ \
+ typeof(p) _orig = p; \
+ s *_ptr = ((s *)((char *)_orig - OFFSETOF(s, i))); \
+ SAME_TYPE(&_ptr->i, _orig); \
+ _ptr; })
+#define SKIP_BACK_DECLARE(s, n, i, p) s *n = SKIP_BACK(s, i, p)
#define BIRD_ALIGN(s, a) (((s)+a-1)&~(a-1))
- #define CPU_STRUCT_ALIGN (MAX_(_Alignof(void*), _Alignof(u64)))
+#define BIRD_SET_ALIGNED_POINTER(ptr, val) do { \
+ size_t _alignment = _Alignof(typeof(*ptr)); \
+ ptr = (typeof(ptr)) BIRD_ALIGN((uintptr_t)(val), _alignment); \
+} while (0)
+ #define CPU_STRUCT_ALIGN (alignof(max_align_t))
+#define BIRD_CPU_ALIGN(s) BIRD_ALIGN((s), CPU_STRUCT_ALIGN)
+
+/* Structure item alignment macros */
+
+#define PADDING_NAME(id) _padding_##id
+#define PADDING_(id, sz) u8 PADDING_NAME(id)[sz]
+
+#if CPU_POINTER_ALIGNMENT == 4
+#define PADDING(id, n32, n64) PADDING_(id, n32)
+#elif CPU_POINTER_ALIGNMENT == 8
+#define PADDING(id, n32, n64) PADDING_(id, n64)
+#else
+#error "Strange CPU pointer alignment: " CPU_POINTER_ALIGNMENT
+#endif
/* Utility macros */