summaryrefslogtreecommitdiff
path: root/include/linux/build_bug.h
diff options
context:
space:
mode:
authorMaxim Levitsky <mlevitsk@redhat.com>2022-10-25 15:47:27 +0300
committerPaolo Bonzini <pbonzini@redhat.com>2022-11-09 20:31:16 +0300
commit07a368b3f55a79d33cad113d506b279e04fd2a00 (patch)
treea5cc4f6daeedce9bdb81f08525650557fcb30874 /include/linux/build_bug.h
parent00009406f0dbc53b95b9062c0cc297d6893ff394 (diff)
downloadlinux-07a368b3f55a79d33cad113d506b279e04fd2a00.tar.xz
bug: introduce ASSERT_STRUCT_OFFSET
ASSERT_STRUCT_OFFSET allows to assert during the build of the kernel that a field in a struct have an expected offset. KVM used to have such macro, but there is almost nothing KVM specific in it so move it to build_bug.h, so that it can be used in other places in KVM. Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> Message-Id: <20221025124741.228045-10-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'include/linux/build_bug.h')
-rw-r--r--include/linux/build_bug.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/include/linux/build_bug.h b/include/linux/build_bug.h
index e3a0be2c90ad..3aa3640f8c18 100644
--- a/include/linux/build_bug.h
+++ b/include/linux/build_bug.h
@@ -77,4 +77,13 @@
#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
#define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
+
+/*
+ * Compile time check that field has an expected offset
+ */
+#define ASSERT_STRUCT_OFFSET(type, field, expected_offset) \
+ BUILD_BUG_ON_MSG(offsetof(type, field) != (expected_offset), \
+ "Offset of " #field " in " #type " has changed.")
+
+
#endif /* _LINUX_BUILD_BUG_H */