Lucas De Marchi | f87081b | 2011-11-23 12:21:29 -0200 | [diff] [blame] | 1 | /* |
| 2 | * libkmod - interface to kernel module operations |
| 3 | * |
Lucas De Marchi | a66a6a9 | 2012-01-09 00:40:50 -0200 | [diff] [blame^] | 4 | * Copyright (C) 2011-2012 ProFUSION embedded systems |
Lucas De Marchi | f87081b | 2011-11-23 12:21:29 -0200 | [diff] [blame] | 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 |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
Lucas De Marchi | f87081b | 2011-11-23 12:21:29 -0200 | [diff] [blame] | 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 | |
Lucas De Marchi | 88e9c12 | 2011-11-23 12:23:46 -0200 | [diff] [blame] | 21 | #ifndef _LIBKMOD_MACRO_H_ |
| 22 | #define _LIBKMOD_MACRO_H_ |
Lucas De Marchi | 6924e47 | 2011-11-22 05:38:28 -0200 | [diff] [blame] | 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 | |
Lucas De Marchi | aa1c352 | 2011-11-29 17:59:58 -0200 | [diff] [blame] | 52 | /** |
| 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 | |
Lucas De Marchi | a507d80 | 2011-11-30 14:35:39 -0200 | [diff] [blame] | 75 | /* Temporaries for importing index handling */ |
| 76 | #define NOFAIL(x) (x) |
| 77 | #define fatal(x...) do { } while (0) |
| 78 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 79 | /* Attributes */ |
| 80 | |
Lucas De Marchi | f87081b | 2011-11-23 12:21:29 -0200 | [diff] [blame] | 81 | #define __must_check __attribute__((warn_unused_result)) |
| 82 | #define __printf_format(a,b) __attribute__((format (printf, a, b))) |
| 83 | #if !defined(__always_inline) |
| 84 | #define __always_inline __inline__ __attribute__((always_inline)) |
| 85 | #endif |
| 86 | |
Lucas De Marchi | 6924e47 | 2011-11-22 05:38:28 -0200 | [diff] [blame] | 87 | #endif |