* gcc_compat.h [GNUC_PREREQ(3, 0)] (BUILD_BUG_ON_ZERO): New macro.
(SAME_TYPE, MUST_BE_ARRAY): Likewise.
* defs.h (ARRAY_SIZE): Add MUST_BE_ARRAY for build-time type check.
(offsetof(type, member) + sizeof(((type *)NULL)->member))
#endif
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + MUST_BE_ARRAY(a))
/* macros */
#ifndef MAX
# define ATTRIBUTE_PACKED /* empty */
#endif
+#if GNUC_PREREQ(3, 0)
+# define SAME_TYPE(x, y) __builtin_types_compatible_p(typeof(x), typeof(y))
+# define BUILD_BUG_ON_ZERO(expr) (sizeof(int[-1 + 2 * !!(expr)]) * 0)
+/* &(a)[0] is a pointer and not an array, shouldn't be treated as the same */
+# define MUST_BE_ARRAY(a) BUILD_BUG_ON_ZERO(!SAME_TYPE((a), &(a)[0]))
+#else
+# define SAME_TYPE(x, y) 0
+# define MUST_BE_ARRAY(a) 0
+#endif
+
#if GNUC_PREREQ(3, 0)
# define ATTRIBUTE_MALLOC __attribute__((__malloc__))
#else