2016-01-07 13:07:13 +01:00
|
|
|
#pragma once
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
// Ensure structs are correct size & offsets
|
2018-07-19 09:44:58 +02:00
|
|
|
#if defined(static_assert) || defined(__cplusplus)
|
2018-06-20 11:31:53 +02:00
|
|
|
# define WUT_CHECK_SIZE(Type, Size) \
|
2017-02-21 21:06:18 +01:00
|
|
|
static_assert(sizeof(Type) == Size, \
|
|
|
|
#Type " must be " #Size " bytes")
|
2016-01-07 13:07:13 +01:00
|
|
|
|
2018-06-20 11:31:53 +02:00
|
|
|
# define WUT_CHECK_OFFSET(Type, Offset, Field) \
|
2017-02-21 21:06:18 +01:00
|
|
|
static_assert(offsetof(Type, Field) == Offset, \
|
|
|
|
#Type "::" #Field " must be at offset " #Offset)
|
|
|
|
#else
|
2018-06-20 11:31:53 +02:00
|
|
|
# define WUT_CHECK_SIZE(Type, Size)
|
|
|
|
# define WUT_CHECK_OFFSET(Type, Offset, Field)
|
2017-02-21 21:06:18 +01:00
|
|
|
#endif
|
2016-01-07 13:07:13 +01:00
|
|
|
|
|
|
|
// Workaround weird macro concat ## behaviour
|
2018-06-20 11:31:53 +02:00
|
|
|
#define WUT_PP_CAT(a, b) WUT_PP_CAT_I(a, b)
|
|
|
|
#define WUT_PP_CAT_I(a, b) WUT_PP_CAT_II(~, a ## b)
|
|
|
|
#define WUT_PP_CAT_II(p, res) res
|
2016-01-07 13:07:13 +01:00
|
|
|
|
|
|
|
// Allow us to easily add UNKNOWN / PADDING bytes into our structs,
|
|
|
|
// generates unique variable names using __COUNTER__
|
2018-06-20 11:31:53 +02:00
|
|
|
#define WUT_UNKNOWN_BYTES(Size) char WUT_PP_CAT(__unk, __COUNTER__) [Size]
|
|
|
|
#define WUT_PADDING_BYTES(Size) WUT_UNKNOWN_BYTES(Size)
|
2016-01-07 13:07:13 +01:00
|
|
|
|
2018-06-20 12:10:37 +02:00
|
|
|
// Unknown struct size
|
2018-06-20 11:31:53 +02:00
|
|
|
#define WUT_UNKNOWN_SIZE(x)
|
2018-06-20 12:10:37 +02:00
|
|
|
|
|
|
|
// Marks a struct as packed
|
|
|
|
#define WUT_PACKED __attribute__((__packed__))
|
2018-09-14 09:56:48 +02:00
|
|
|
|
|
|
|
// Sets alignment for a struct
|
|
|
|
#if defined(__alignas_is_defined) || defined(__cplusplus)
|
|
|
|
#define WUT_ALIGNAS(x) alignas(x)
|
|
|
|
#elif defined(__GNUC__) || defined(__clang__)
|
|
|
|
#define WUT_ALIGNAS(x) __attribute__((__aligned__(x)))
|
|
|
|
#endif
|