ia64/xen-unstable

changeset 19629:5369133d772c

Introduce __must_be_array

Cloning the similar construct from Linux, allowing to detect improper
uses of ARRAY_SIZE() at build time.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed May 20 15:29:25 2009 +0100 (2009-05-20)
parents 23f9857f642f
children 1695a86b3d7c
files xen/include/xen/compiler.h xen/include/xen/config.h xen/include/xen/lib.h
line diff
     1.1 --- a/xen/include/xen/compiler.h	Wed May 20 15:27:30 2009 +0100
     1.2 +++ b/xen/include/xen/compiler.h	Wed May 20 15:29:25 2009 +0100
     1.3 @@ -35,6 +35,10 @@
     1.4  #define offsetof(a,b) ((unsigned long)&(((a *)0)->b))
     1.5  #endif
     1.6  
     1.7 +/* &a[0] degrades to a pointer: a different type from an array */
     1.8 +#define __must_be_array(a) \
     1.9 +  BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
    1.10 +
    1.11  #ifdef GCC_HAS_VISIBILITY_ATTRIBUTE
    1.12  /* Results in more efficient PIC code (no indirections through GOT or PLT). */
    1.13  #pragma GCC visibility push(hidden)
     2.1 --- a/xen/include/xen/config.h	Wed May 20 15:27:30 2009 +0100
     2.2 +++ b/xen/include/xen/config.h	Wed May 20 15:29:25 2009 +0100
     2.3 @@ -11,7 +11,7 @@
     2.4  
     2.5  #define EXPORT_SYMBOL(var)
     2.6  #define EXPORT_SYMBOL_GPL(var)
     2.7 -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
     2.8 +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
     2.9  
    2.10  /*
    2.11   * The following log levels are as follows:
     3.1 --- a/xen/include/xen/lib.h	Wed May 20 15:27:30 2009 +0100
     3.2 +++ b/xen/include/xen/lib.h	Wed May 20 15:29:25 2009 +0100
     3.3 @@ -18,6 +18,12 @@ void __warn(char *file, int line);
     3.4  /* Force a compilation error if condition is true */
     3.5  #define BUILD_BUG_ON(condition) ((void)sizeof(struct { int:-!!(condition); }))
     3.6  
     3.7 +/* Force a compilation error if condition is true, but also produce a
     3.8 +   result (of value 0 and type size_t), so the expression can be used
     3.9 +   e.g. in a structure initializer (or where-ever else comma expressions
    3.10 +   aren't permitted). */
    3.11 +#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
    3.12 +
    3.13  #ifndef assert_failed
    3.14  #define assert_failed(p)                                        \
    3.15  do {                                                            \