diff mbox

[U-Boot,v3,09/54] Add a way of checking the position of a structure member

Message ID 1435095556-15924-10-git-send-email-sjg@chromium.org
State Accepted
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass June 23, 2015, 9:38 p.m. UTC
U-Boot uses structures for hardware access so it is important that these
structures are correct. Add a way of asserting that a structure member is
at a particular offset. This can be created using the datasheet for the
hardware.

This implementation uses Static_assert() since BUILD_BUG_ON() only works
within functions.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v3: None
Changes in v2: None

 include/common.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Simon Glass July 17, 2015, 11:56 p.m. UTC | #1
On 23 June 2015 at 15:38, Simon Glass <sjg@chromium.org> wrote:
> U-Boot uses structures for hardware access so it is important that these
> structures are correct. Add a way of asserting that a structure member is
> at a particular offset. This can be created using the datasheet for the
> hardware.
>
> This implementation uses Static_assert() since BUILD_BUG_ON() only works
> within functions.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> Changes in v3: None
> Changes in v2: None
>
>  include/common.h | 11 +++++++++++
>  1 file changed, 11 insertions(+)

Applied to u-boot-dm.
diff mbox

Patch

diff --git a/include/common.h b/include/common.h
index 8f4b2ec..4566bd1 100644
--- a/include/common.h
+++ b/include/common.h
@@ -1010,6 +1010,17 @@  int cpu_release(int nr, int argc, char * const argv[]);
 #define DEFINE_CACHE_ALIGN_BUFFER(type, name, size)			\
 	DEFINE_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN)
 
+/*
+ * check_member() - Check the offset of a structure member
+ *
+ * @structure:	Name of structure (e.g. global_data)
+ * @member:	Name of member (e.g. baudrate)
+ * @offset:	Expected offset in bytes
+ */
+#define check_member(structure, member, offset) _Static_assert( \
+	offsetof(struct structure, member) == offset, \
+	"`struct " #structure "` offset for `" #member "` is not " #offset)
+
 /* Pull in stuff for the build system */
 #ifdef DO_DEPS_ONLY
 # include <environment.h>