Commit 5407eaf2 authored by Martin Storsjö's avatar Martin Storsjö
Browse files

attributes: Add a CHECK_OFFSET macro for verifying struct offsets

A static_assert is used if available, otherwise a custom
construct.
parent ca29d179
......@@ -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 */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment