wut/include/wut_structsize.h

41 lines
1.3 KiB
C
Raw Normal View History

2016-01-07 13:07:13 +01:00
#pragma once
#include <assert.h>
#include <stddef.h>
// Ensure structs are correct size & offsets
#if defined(static_assert) || defined(__cplusplus)
2018-06-20 11:31:53 +02:00
# define WUT_CHECK_SIZE(Type, Size) \
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) \
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)
#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
// Unknown struct size
2018-06-20 11:31:53 +02:00
#define WUT_UNKNOWN_SIZE(x)
// Marks a struct as packed
#define WUT_PACKED __attribute__((__packed__))
// 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