]>
Commit | Line | Data |
---|---|---|
f87081b4 LDM |
1 | /* |
2 | * libkmod - interface to kernel module operations | |
3 | * | |
a66a6a99 | 4 | * Copyright (C) 2011-2012 ProFUSION embedded systems |
f87081b4 LDM |
5 | * |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
cb451f35 LDM |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or (at your option) any later version. | |
f87081b4 LDM |
10 | * |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
88e9c12e LDM |
21 | #ifndef _LIBKMOD_MACRO_H_ |
22 | #define _LIBKMOD_MACRO_H_ | |
6924e47a LDM |
23 | |
24 | #include <stddef.h> | |
25 | ||
26 | #define BUILD_ASSERT(cond) \ | |
27 | do { (void) sizeof(char [1 - 2*!(cond)]); } while(0) | |
28 | ||
29 | #define EXPR_BUILD_ASSERT(cond) \ | |
30 | (sizeof(char [1 - 2*!(cond)]) - 1) | |
31 | ||
32 | #if HAVE_TYPEOF | |
33 | #define check_type(expr, type) \ | |
34 | ((typeof(expr) *)0 != (type *)0) | |
35 | ||
36 | #define check_types_match(expr1, expr2) \ | |
37 | ((typeof(expr1) *)0 != (typeof(expr2) *)0) | |
38 | #else | |
39 | /* Without typeof, we can only test the sizes. */ | |
40 | #define check_type(expr, type) \ | |
41 | EXPR_BUILD_ASSERT(sizeof(expr) == sizeof(type)) | |
42 | ||
43 | #define check_types_match(expr1, expr2) \ | |
44 | EXPR_BUILD_ASSERT(sizeof(expr1) == sizeof(expr2)) | |
45 | #endif /* HAVE_TYPEOF */ | |
46 | ||
47 | #define container_of(member_ptr, containing_type, member) \ | |
48 | ((containing_type *) \ | |
49 | ((char *)(member_ptr) - offsetof(containing_type, member)) \ | |
50 | - check_types_match(*(member_ptr), ((containing_type *)0)->member)) | |
51 | ||
f4cc6ea5 | 52 | /* |
aa1c3521 LDM |
53 | * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression. |
54 | * @cond: the compile-time condition which must be true. | |
55 | * | |
56 | * Your compile will fail if the condition isn't true, or can't be evaluated | |
57 | * by the compiler. This can be used in an expression: its value is "0". | |
58 | * | |
59 | * Example: | |
60 | * #define foo_to_char(foo) \ | |
61 | * ((char *)(foo) \ | |
62 | * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0)) | |
63 | */ | |
64 | #define BUILD_ASSERT_OR_ZERO(cond) \ | |
65 | (sizeof(char [1 - 2*!(cond)]) - 1) | |
66 | ||
67 | /* Two gcc extensions. | |
68 | * &a[0] degrades to a pointer: a different type from an array */ | |
69 | #define _array_size_chk(arr) \ | |
70 | BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \ | |
71 | typeof(&(arr)[0]))) | |
72 | ||
73 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr)) | |
74 | ||
a507d803 LDM |
75 | /* Temporaries for importing index handling */ |
76 | #define NOFAIL(x) (x) | |
77 | #define fatal(x...) do { } while (0) | |
78 | ||
8f788d58 LDM |
79 | /* Attributes */ |
80 | ||
a70c1e77 LDM |
81 | #define _must_check_ __attribute__((warn_unused_result)) |
82 | #define _printf_format_(a,b) __attribute__((format (printf, a, b))) | |
9e2eadb1 | 83 | #define _unused_ __attribute__((unused)) |
a70c1e77 | 84 | #define _always_inline_ __inline__ __attribute__((always_inline)) |
f87081b4 | 85 | |
6924e47a | 86 | #endif |