Lucas De Marchi | f87081b | 2011-11-23 12:21:29 -0200 | [diff] [blame] | 1 | /* |
Lucas De Marchi | 576dd43 | 2014-10-02 22:03:19 -0300 | [diff] [blame] | 2 | * kmod - interface to kernel module operations |
Lucas De Marchi | f87081b | 2011-11-23 12:21:29 -0200 | [diff] [blame] | 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 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 |
Lucas De Marchi | dea2dfe | 2014-12-25 23:32:03 -0200 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Lucas De Marchi | f87081b | 2011-11-23 12:21:29 -0200 | [diff] [blame] | 18 | */ |
Lucas De Marchi | e8fd8fe | 2012-07-18 10:19:48 -0300 | [diff] [blame] | 19 | #pragma once |
Lucas De Marchi | 6924e47 | 2011-11-22 05:38:28 -0200 | [diff] [blame] | 20 | |
| 21 | #include <stddef.h> |
| 22 | |
Thomas Petazzoni | dc8ed09 | 2013-09-06 15:27:04 +0200 | [diff] [blame] | 23 | #if defined(HAVE_STATIC_ASSERT) |
Lucas De Marchi | 8efede2 | 2013-04-15 14:09:05 -0300 | [diff] [blame] | 24 | #define assert_cc(expr) \ |
| 25 | _Static_assert((expr), #expr) |
Thomas Petazzoni | dc8ed09 | 2013-09-06 15:27:04 +0200 | [diff] [blame] | 26 | #else |
| 27 | #define assert_cc(expr) \ |
| 28 | do { (void) sizeof(char [1 - 2*!(expr)]); } while(0) |
| 29 | #endif |
Lucas De Marchi | 6924e47 | 2011-11-22 05:38:28 -0200 | [diff] [blame] | 30 | |
Lucas De Marchi | 6924e47 | 2011-11-22 05:38:28 -0200 | [diff] [blame] | 31 | #define check_types_match(expr1, expr2) \ |
| 32 | ((typeof(expr1) *)0 != (typeof(expr2) *)0) |
Lucas De Marchi | 6924e47 | 2011-11-22 05:38:28 -0200 | [diff] [blame] | 33 | |
| 34 | #define container_of(member_ptr, containing_type, member) \ |
| 35 | ((containing_type *) \ |
| 36 | ((char *)(member_ptr) - offsetof(containing_type, member)) \ |
| 37 | - check_types_match(*(member_ptr), ((containing_type *)0)->member)) |
| 38 | |
Lucas De Marchi | aa1c352 | 2011-11-29 17:59:58 -0200 | [diff] [blame] | 39 | |
| 40 | /* Two gcc extensions. |
| 41 | * &a[0] degrades to a pointer: a different type from an array */ |
Lucas De Marchi | 8efede2 | 2013-04-15 14:09:05 -0300 | [diff] [blame] | 42 | #define _array_size_chk(arr) ({ \ |
| 43 | assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \ |
| 44 | 0; \ |
| 45 | }) |
Lucas De Marchi | aa1c352 | 2011-11-29 17:59:58 -0200 | [diff] [blame] | 46 | |
| 47 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr)) |
Lucas De Marchi | bf2fbab | 2015-01-14 14:31:34 -0200 | [diff] [blame] | 48 | #define XSTRINGIFY(x) #x |
| 49 | #define STRINGIFY(x) XSTRINGIFY(x) |
Lucas De Marchi | aa1c352 | 2011-11-29 17:59:58 -0200 | [diff] [blame] | 50 | |
Lucas De Marchi | a507d80 | 2011-11-30 14:35:39 -0200 | [diff] [blame] | 51 | /* Temporaries for importing index handling */ |
| 52 | #define NOFAIL(x) (x) |
| 53 | #define fatal(x...) do { } while (0) |
| 54 | |
Lucas De Marchi | 8f788d5 | 2011-11-25 01:22:56 -0200 | [diff] [blame] | 55 | /* Attributes */ |
| 56 | |
Lucas De Marchi | a70c1e7 | 2012-05-23 20:27:23 -0300 | [diff] [blame] | 57 | #define _must_check_ __attribute__((warn_unused_result)) |
| 58 | #define _printf_format_(a,b) __attribute__((format (printf, a, b))) |
Lucas De Marchi | 9e2eadb | 2012-05-23 20:28:53 -0300 | [diff] [blame] | 59 | #define _unused_ __attribute__((unused)) |
Lucas De Marchi | a70c1e7 | 2012-05-23 20:27:23 -0300 | [diff] [blame] | 60 | #define _always_inline_ __inline__ __attribute__((always_inline)) |
Lucas De Marchi | d7aa6e2 | 2013-11-14 00:19:15 -0200 | [diff] [blame] | 61 | #define _cleanup_(x) __attribute__((cleanup(x))) |
Lucas De Marchi | d96ca9c | 2013-12-17 19:10:16 -0200 | [diff] [blame] | 62 | |
| 63 | /* Define C11 noreturn without <stdnoreturn.h> and even on older gcc |
| 64 | * compiler versions */ |
| 65 | #ifndef noreturn |
Lucas De Marchi | 16a62c7 | 2015-02-26 13:02:04 -0300 | [diff] [blame^] | 66 | #if defined(HAVE_NORETURN) |
Lucas De Marchi | d96ca9c | 2013-12-17 19:10:16 -0200 | [diff] [blame] | 67 | #define noreturn _Noreturn |
| 68 | #else |
| 69 | #define noreturn __attribute__((noreturn)) |
| 70 | #endif |
| 71 | #endif |
Lucas De Marchi | 4328982 | 2014-10-09 14:29:04 -0300 | [diff] [blame] | 72 | |
| 73 | #define UNIQ __COUNTER__ |