WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH] introduce __must_be_array

To: <xen-devel@xxxxxxxxxxxxxxxxxxx>
Subject: [Xen-devel] [PATCH] introduce __must_be_array
From: "Jan Beulich" <JBeulich@xxxxxxxxxx>
Date: Wed, 20 May 2009 14:04:27 +0100
Delivery-date: Wed, 20 May 2009 06:04:52 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
Cloning the similar construct from Linux, allowing to detect improper
uses of ARRAY_SIZE() at build time.

Signed-off-by: Jan Beulich <jbeulich@xxxxxxxxxx>

--- 2009-03-27.orig/xen/include/xen/compiler.h  2008-06-10 18:00:41.000000000 
+0200
+++ 2009-03-27/xen/include/xen/compiler.h       2009-04-07 13:38:22.000000000 
+0200
@@ -35,6 +35,10 @@
 #define offsetof(a,b) ((unsigned long)&(((a *)0)->b))
 #endif
 
+/* &a[0] degrades to a pointer: a different type from an array */
+#define __must_be_array(a) \
+  BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
+
 #ifdef GCC_HAS_VISIBILITY_ATTRIBUTE
 /* Results in more efficient PIC code (no indirections through GOT or PLT). */
 #pragma GCC visibility push(hidden)
--- 2009-03-27.orig/xen/include/xen/config.h    2007-10-12 16:12:16.000000000 
+0200
+++ 2009-03-27/xen/include/xen/config.h 2009-04-07 13:41:00.000000000 +0200
@@ -11,7 +11,7 @@
 
 #define EXPORT_SYMBOL(var)
 #define EXPORT_SYMBOL_GPL(var)
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
 
 /*
  * The following log levels are as follows:
--- 2009-03-27.orig/xen/include/xen/lib.h       2009-03-30 09:11:46.000000000 
+0200
+++ 2009-03-27/xen/include/xen/lib.h    2009-04-07 13:43:03.000000000 +0200
@@ -18,6 +18,12 @@ void __warn(char *file, int line);
 /* Force a compilation error if condition is true */
 #define BUILD_BUG_ON(condition) ((void)sizeof(struct { int:-!!(condition); }))
 
+/* Force a compilation error if condition is true, but also produce a
+   result (of value 0 and type size_t), so the expression can be used
+   e.g. in a structure initializer (or where-ever else comma expressions
+   aren't permitted). */
+#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
+
 #ifndef assert_failed
 #define assert_failed(p)                                        \
 do {                                                            \




_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel

<Prev in Thread] Current Thread [Next in Thread>