]> git.ipfire.org Git - thirdparty/kmod.git/blob - shared/macro.h
0f035b1781886245e335d2c59c0ab328dca27edb
[thirdparty/kmod.git] / shared / macro.h
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #pragma once
21
22 #include <stddef.h>
23
24 #if defined(HAVE_STATIC_ASSERT)
25 #define assert_cc(expr) \
26 _Static_assert((expr), #expr)
27 #else
28 #define assert_cc(expr) \
29 do { (void) sizeof(char [1 - 2*!(expr)]); } while(0)
30 #endif
31
32 #define check_types_match(expr1, expr2) \
33 ((typeof(expr1) *)0 != (typeof(expr2) *)0)
34
35 #define container_of(member_ptr, containing_type, member) \
36 ((containing_type *) \
37 ((char *)(member_ptr) - offsetof(containing_type, member)) \
38 - check_types_match(*(member_ptr), ((containing_type *)0)->member))
39
40
41 /* Two gcc extensions.
42 * &a[0] degrades to a pointer: a different type from an array */
43 #define _array_size_chk(arr) ({ \
44 assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \
45 0; \
46 })
47
48 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
49
50 /* Temporaries for importing index handling */
51 #define NOFAIL(x) (x)
52 #define fatal(x...) do { } while (0)
53
54 /* Attributes */
55
56 #define _must_check_ __attribute__((warn_unused_result))
57 #define _printf_format_(a,b) __attribute__((format (printf, a, b)))
58 #define _unused_ __attribute__((unused))
59 #define _always_inline_ __inline__ __attribute__((always_inline))
60 #define _cleanup_(x) __attribute__((cleanup(x)))
61
62 /* Define C11 noreturn without <stdnoreturn.h> and even on older gcc
63 * compiler versions */
64 #ifndef noreturn
65 #if __STDC_VERSION__ >= 201112L
66 #define noreturn _Noreturn
67 #else
68 #define noreturn __attribute__((noreturn))
69 #endif
70 #endif
71
72 #define UNIQ __COUNTER__