1 /* 2 * kmod - interface to kernel module operations 3 * 4 * Copyright (C) 2011-2013 ProFUSION embedded systems 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 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 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, see <http://www.gnu.org/licenses/>. 18 */ 19 #pragma once 20 21 #include <stddef.h> 22 23 #if defined(HAVE_STATIC_ASSERT) 24 #define assert_cc(expr) \ 25 _Static_assert((expr), #expr) 26 #else 27 #define assert_cc(expr) \ 28 do { (void) sizeof(char [1 - 2*!(expr)]); } while(0) 29 #endif 30 31 #define check_types_match(expr1, expr2) \ 32 ((typeof(expr1) *)0 != (typeof(expr2) *)0) 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 39 40 /* Two gcc extensions. 41 * &a[0] degrades to a pointer: a different type from an array */ 42 #define _array_size_chk(arr) ({ \ 43 assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \ 44 0; \ 45 }) 46 47 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr)) 48 49 #define XSTRINGIFY(x) #x 50 #define STRINGIFY(x) XSTRINGIFY(x) 51 52 #define XCONCATENATE(x, y) x ## y 53 #define CONCATENATE(x, y) XCONCATENATE(x, y) 54 #define UNIQ(x) CONCATENATE(x, __COUNTER__) 55 56 /* Temporaries for importing index handling */ 57 #define NOFAIL(x) (x) 58 #define fatal(x...) do { } while (0) 59 60 /* Attributes */ 61 62 #define _must_check_ __attribute__((warn_unused_result)) 63 #define _printf_format_(a,b) __attribute__((format (printf, a, b))) 64 #define _unused_ __attribute__((unused)) 65 #define _always_inline_ __inline__ __attribute__((always_inline)) 66 #define _cleanup_(x) __attribute__((cleanup(x))) 67 68 /* Define C11 noreturn without <stdnoreturn.h> and even on older gcc 69 * compiler versions */ 70 #ifndef noreturn 71 #if defined(HAVE_NORETURN) 72 #define noreturn _Noreturn 73 #else 74 #define noreturn __attribute__((noreturn)) 75 #endif 76 #endif 77