X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Futil.h;h=a899065a3af3ad9cc37a789dbd414a5baeeb8b8e;hb=e8f9a7bbf1b4dd4cfd4f16a3a9c28ccf406b7d26;hp=911ad3218a6855b75dd5a0a9df8f889d6f98cf88;hpb=33e191a01b2c5aad1e97c109e66dec4b4d280ef6;p=sliver-openvswitch.git diff --git a/lib/util.h b/lib/util.h index 911ad3218..a899065a3 100644 --- a/lib/util.h +++ b/lib/util.h @@ -87,8 +87,23 @@ void ovs_assert_failure(const char *, const char *, const char *) NO_RETURN; extern const char *program_name; +#define __ARRAY_SIZE_NOCHECK(ARRAY) (sizeof(ARRAY) / sizeof((ARRAY)[0])) +#ifdef __GNUC__ +/* return 0 for array types, 1 otherwise */ +#define __ARRAY_CHECK(ARRAY) \ + !__builtin_types_compatible_p(typeof(ARRAY), typeof(&ARRAY[0])) + +/* compile-time fail if not array */ +#define __ARRAY_FAIL(ARRAY) (sizeof(char[-2*!__ARRAY_CHECK(ARRAY)])) +#define __ARRAY_SIZE(ARRAY) \ + __builtin_choose_expr(__ARRAY_CHECK(ARRAY), \ + __ARRAY_SIZE_NOCHECK(ARRAY), __ARRAY_FAIL(ARRAY)) +#else +#define __ARRAY_SIZE(ARRAY) __ARRAY_SIZE_NOCHECK(ARRAY) +#endif + /* Returns the number of elements in ARRAY. */ -#define ARRAY_SIZE(ARRAY) (sizeof ARRAY / sizeof *ARRAY) +#define ARRAY_SIZE(ARRAY) __ARRAY_SIZE(ARRAY) /* Returns X / Y, rounding up. X must be nonnegative to round correctly. */ #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y)) @@ -193,6 +208,10 @@ is_pow2(uintmax_t x) #define ASSIGN_CONTAINER(OBJECT, POINTER, MEMBER) \ ((OBJECT) = OBJECT_CONTAINING(POINTER, OBJECT, MEMBER), (void) 0) +/* Given ATTR, and TYPE, cast the ATTR to TYPE by first casting ATTR to + * (void *). This is to suppress the alignment warning issued by clang. */ +#define ALIGNED_CAST(TYPE, ATTR) ((TYPE) (void *) (ATTR)) + #ifdef __cplusplus extern "C" { #endif