blob: 81ccc997ce0a7852052afac7d6ebad9225d56dd4 [file] [log] [blame]
Lucas De Marchi6924e472011-11-22 05:38:28 -02001#ifndef _LIBKMOD_UTIL_H_
2#define _LIBKMOD_UTIL_H_
3
4#include <stddef.h>
5
6#define BUILD_ASSERT(cond) \
7 do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
8
9#define EXPR_BUILD_ASSERT(cond) \
10 (sizeof(char [1 - 2*!(cond)]) - 1)
11
12#if HAVE_TYPEOF
13#define check_type(expr, type) \
14 ((typeof(expr) *)0 != (type *)0)
15
16#define check_types_match(expr1, expr2) \
17 ((typeof(expr1) *)0 != (typeof(expr2) *)0)
18#else
19/* Without typeof, we can only test the sizes. */
20#define check_type(expr, type) \
21 EXPR_BUILD_ASSERT(sizeof(expr) == sizeof(type))
22
23#define check_types_match(expr1, expr2) \
24 EXPR_BUILD_ASSERT(sizeof(expr1) == sizeof(expr2))
25#endif /* HAVE_TYPEOF */
26
27#define container_of(member_ptr, containing_type, member) \
28 ((containing_type *) \
29 ((char *)(member_ptr) - offsetof(containing_type, member)) \
30 - check_types_match(*(member_ptr), ((containing_type *)0)->member))
31
32#endif