On Wed, Oct 02, 2013 at 02:40:09AM -0300, Flavio Leitner wrote: > GCC provides two useful builtin functions that can help > to improve array size checking during compilation. > > This patch contains no functional changes, but it makes > it easier to detect mistakes. > > Signed-off-by: Flavio Leitner <f...@redhat.com>
This seems simpler: --8<--------------------------cut here-------------------------->8-- From: Flavio Leitner <f...@redhat.com> Date: Wed, 2 Oct 2013 02:40:09 -0300 Subject: [PATCH] util: use gcc builtins to better check array sizes GCC provides two useful builtin functions that can help to improve array size checking during compilation. This patch contains no functional changes, but it makes it easier to detect mistakes. Signed-off-by: Flavio Leitner <f...@redhat.com> Signed-off-by: Ben Pfaff <b...@nicira.com> --- lib/util.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/util.h b/lib/util.h index 0db41be..8978bdb 100644 --- a/lib/util.h +++ b/lib/util.h @@ -87,8 +87,18 @@ void ovs_assert_failure(const char *, const char *, const char *) NO_RETURN; extern const char *program_name; +#ifdef __GNUC__ +/* Fails a build assertion if ARRAY is not an array type. */ +#define ARRAY_CHECK(ARRAY) \ + BUILD_ASSERT(!__builtin_types_compatible_p(typeof(ARRAY), \ + typeof(&ARRAY[0]))) +#else +#define ARRAY_CHECK(ARRAY) ((void) 0) +#endif + /* Returns the number of elements in ARRAY. */ -#define ARRAY_SIZE(ARRAY) (sizeof ARRAY / sizeof *ARRAY) +#define ARRAY_SIZE(ARRAY) \ + (ARRAY_CHECK(ARRAY), sizeof(ARRAY) / sizeof((ARRAY)[0])) /* Returns X / Y, rounding up. X must be nonnegative to round correctly. */ #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y)) -- 1.7.10.4 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev