summaryrefslogtreecommitdiff
path: root/src/basic/macro.h
diff options
context:
space:
mode:
authorMichal Schmidt <mschmidt@redhat.com>2015-09-14 20:32:17 +0200
committerMichal Schmidt <mschmidt@redhat.com>2015-09-16 15:32:25 +0200
commit7f39a2104532e38d77a66c15fdb9e33e30ee7004 (patch)
tree52d0f0e7f9aff6bed4000d8dbc855361c137577f /src/basic/macro.h
parentac2896bab61bd8cd0b8a8b92f8347e2c11a088b5 (diff)
basic: make sure argument of ELEMENTSOF is an array
Using ELEMENTSOF on a pointer will result in a compilation error.
Diffstat (limited to 'src/basic/macro.h')
-rw-r--r--src/basic/macro.h7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/basic/macro.h b/src/basic/macro.h
index cbc3ca97b8..fd6224e568 100644
--- a/src/basic/macro.h
+++ b/src/basic/macro.h
@@ -123,8 +123,11 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
return 1UL << (sizeof(u) * 8 - __builtin_clzl(u - 1UL));
}
-#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
-
+#define ELEMENTSOF(x) \
+ __extension__ (__builtin_choose_expr( \
+ !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
+ sizeof(x)/sizeof((x)[0]), \
+ (void)0))
/*
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.