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-changelog

[Xen-changelog] [xen-unstable] Introduce __must_be_array

To: xen-changelog@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-changelog] [xen-unstable] Introduce __must_be_array
From: Xen patchbot-unstable <patchbot-unstable@xxxxxxxxxxxxxxxxxxx>
Date: Tue, 26 May 2009 05:50:17 -0700
Delivery-date: Tue, 26 May 2009 05:50:31 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-changelog-request@lists.xensource.com?subject=help>
List-id: BK change log <xen-changelog.lists.xensource.com>
List-post: <mailto:xen-changelog@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-changelog>, <mailto:xen-changelog-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-changelog>, <mailto:xen-changelog-request@lists.xensource.com?subject=unsubscribe>
Reply-to: xen-devel@xxxxxxxxxxxxxxxxxxx
Sender: xen-changelog-bounces@xxxxxxxxxxxxxxxxxxx
# HG changeset patch
# User Keir Fraser <keir.fraser@xxxxxxxxxx>
# Date 1242829765 -3600
# Node ID 5369133d772c91512f0ae6e5a9381839d38de462
# Parent  23f9857f642f0b511553a2b7507a972465cc8930
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@xxxxxxxxxx>
---
 xen/include/xen/compiler.h |    4 ++++
 xen/include/xen/config.h   |    2 +-
 xen/include/xen/lib.h      |    6 ++++++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff -r 23f9857f642f -r 5369133d772c xen/include/xen/compiler.h
--- a/xen/include/xen/compiler.h        Wed May 20 15:27:30 2009 +0100
+++ b/xen/include/xen/compiler.h        Wed May 20 15:29:25 2009 +0100
@@ -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)
diff -r 23f9857f642f -r 5369133d772c xen/include/xen/config.h
--- a/xen/include/xen/config.h  Wed May 20 15:27:30 2009 +0100
+++ b/xen/include/xen/config.h  Wed May 20 15:29:25 2009 +0100
@@ -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:
diff -r 23f9857f642f -r 5369133d772c xen/include/xen/lib.h
--- a/xen/include/xen/lib.h     Wed May 20 15:27:30 2009 +0100
+++ b/xen/include/xen/lib.h     Wed May 20 15:29:25 2009 +0100
@@ -17,6 +17,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)                                        \

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

<Prev in Thread] Current Thread [Next in Thread>
  • [Xen-changelog] [xen-unstable] Introduce __must_be_array, Xen patchbot-unstable <=