diff options
author | Bill Richardson <wfrichar@chromium.org> | 2013-04-26 20:02:07 -0700 |
---|---|---|
committer | ChromeBot <chrome-bot@google.com> | 2013-04-29 11:05:08 -0700 |
commit | d9bddaa0722d2b95c9b72c9c3a24c33053381311 (patch) | |
tree | 9db87c5dbf220fed30d9caca8a10a0aa2a61039c /include | |
parent | 7cebaa0a017d734ae9dda19a39fd867463f785c6 (diff) | |
download | chrome-ec-d9bddaa0722d2b95c9b72c9c3a24c33053381311.tar.gz |
Improved the BUILD_ASSERT macro to work outside of functions.
This will let us check the size of static array initializers.
Also moved this macro definition and ARRAY_SIZE into a new "tricks.h"
header, so that userspace utils can use it too.
BUG=none
BRANCH=none
TEST=manual
Built everything, tested on Link. Tried various assertions.
Change-Id: I612891108ea37dbca3572e0f25ab54a7bc0ed860
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/49417
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'include')
-rw-r--r-- | include/compile_time_macros.h | 26 | ||||
-rw-r--r-- | include/util.h | 13 |
2 files changed, 27 insertions, 12 deletions
diff --git a/include/compile_time_macros.h b/include/compile_time_macros.h new file mode 100644 index 0000000000..9db583a7f2 --- /dev/null +++ b/include/compile_time_macros.h @@ -0,0 +1,26 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Handy clever tricks */ + +#ifndef __CROS_EC_TRICKS_H +#define __CROS_EC_TRICKS_H + +/* Test an important condition at compile time, not run time */ +#define _BA1_(cond, line) \ + extern int __build_assertion_ ## line[1 - 2*!(cond)] \ + __attribute__ ((unused)) +#define _BA0_(c, x) _BA1_(c, x) +#define BUILD_ASSERT(cond) _BA0_(cond, __LINE__) + +/* Number of elements in an array */ +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +/* Just in case - http://gcc.gnu.org/onlinedocs/gcc/Offsetof.html */ +#ifndef offsetof +#define offsetof(type, member) __builtin_offsetof(type, member) +#endif + +#endif /* __CROS_EC_TRICKS_H */ diff --git a/include/util.h b/include/util.h index c953e468f8..9db42c2f34 100644 --- a/include/util.h +++ b/include/util.h @@ -9,16 +9,11 @@ #define __CROS_EC_UTIL_H #include "common.h" +#include "compile_time_macros.h" #include "config.h" #include "panic.h" /** - * Trigger a compilation failure if the condition - * is not verified at build time. - */ -#define BUILD_ASSERT(cond) ((void)sizeof(char[1 - 2*!(cond)])) - -/** * Trigger a debug exception if the condition * is not verified at runtime. */ @@ -41,12 +36,6 @@ /* Standard macros / definitions */ -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -#endif -#ifndef OFFSET_OF -#define OFFSET_OF(struc, field) ((uint32_t)&(((const struc * const)0)->field)) -#endif #ifndef MAX #define MAX(a, b) ((a) > (b) ? (a) : (b)) #endif |