• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define XSTRINGIFY(x) #x
49 #define STRINGIFY(x) XSTRINGIFY(x)
50 
51 /* Temporaries for importing index handling */
52 #define NOFAIL(x) (x)
53 #define fatal(x...) do { } while (0)
54 
55 /* Attributes */
56 
57 #define _must_check_ __attribute__((warn_unused_result))
58 #define _printf_format_(a,b) __attribute__((format (printf, a, b)))
59 #define _unused_ __attribute__((unused))
60 #define _always_inline_ __inline__ __attribute__((always_inline))
61 #define _cleanup_(x) __attribute__((cleanup(x)))
62 
63 /* Define C11 noreturn without <stdnoreturn.h> and even on older gcc
64  * compiler versions */
65 #ifndef noreturn
66 #if defined(HAVE_NORETURN)
67 #define noreturn _Noreturn
68 #else
69 #define noreturn __attribute__((noreturn))
70 #endif
71 #endif
72 
73 #define UNIQ __COUNTER__
74