diff options
author | Martin Storsjö <martin@martin.st> | 2021-03-24 15:21:37 +0300 |
---|---|---|
committer | Martin Storsjö <martin@martin.st> | 2021-04-14 17:57:30 +0300 |
commit | 5407eaf2e69f7d17b0a14b8db390604d0f557025 (patch) | |
tree | 45861895eb7ac1925adfd74b55e60879eb23b6b1 /include | |
parent | ca29d17901db5d4ebdcac7b0832685c775e3fbef (diff) |
attributes: Add a CHECK_OFFSET macro for verifying struct offsets
A static_assert is used if available, otherwise a custom
construct.
Diffstat (limited to 'include')
-rw-r--r-- | include/common/attributes.h | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/include/common/attributes.h b/include/common/attributes.h index 5d6a3ff..15892b9 100644 --- a/include/common/attributes.h +++ b/include/common/attributes.h @@ -31,6 +31,7 @@ #include "config.h" #include <stddef.h> +#include <assert.h> #ifdef __GNUC__ #define ATTR_ALIAS __attribute__((may_alias)) @@ -103,11 +104,11 @@ #endif #if defined(NDEBUG) && (defined(__GNUC__) || defined(__clang__)) +#undef assert #define assert(x) do { if (!(x)) __builtin_unreachable(); } while (0) #elif defined(NDEBUG) && defined(_MSC_VER) +#undef assert #define assert __assume -#else -#include <assert.h> #endif #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) @@ -163,4 +164,12 @@ static inline int clzll(const unsigned long long mask) { #define __has_feature(x) 0 #endif +#ifndef static_assert +#define CHECK_OFFSET(type, field, name) \ + struct check_##type##_##field { int x[(name == offsetof(type, field)) ? 1 : -1]; } +#else +#define CHECK_OFFSET(type, field, name) \ + static_assert(name == offsetof(type, field), #field) +#endif + #endif /* DAV1D_COMMON_ATTRIBUTES_H */ |