]>
Commit | Line | Data |
---|---|---|
f87081b4 LDM |
1 | /* |
2 | * libkmod - interface to kernel module operations | |
3 | * | |
e6b0e49b | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
f87081b4 LDM |
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 | |
cb451f35 LDM |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or (at your option) any later version. | |
f87081b4 LDM |
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 | */ | |
e8fd8fec | 20 | #pragma once |
6924e47a LDM |
21 | |
22 | #include <stddef.h> | |
23 | ||
dc8ed09f | 24 | #if defined(HAVE_STATIC_ASSERT) |
8efede20 LDM |
25 | #define assert_cc(expr) \ |
26 | _Static_assert((expr), #expr) | |
dc8ed09f TP |
27 | #else |
28 | #define assert_cc(expr) \ | |
29 | do { (void) sizeof(char [1 - 2*!(expr)]); } while(0) | |
30 | #endif | |
6924e47a | 31 | |
6924e47a LDM |
32 | #define check_types_match(expr1, expr2) \ |
33 | ((typeof(expr1) *)0 != (typeof(expr2) *)0) | |
6924e47a LDM |
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 | ||
aa1c3521 LDM |
40 | |
41 | /* Two gcc extensions. | |
42 | * &a[0] degrades to a pointer: a different type from an array */ | |
8efede20 LDM |
43 | #define _array_size_chk(arr) ({ \ |
44 | assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \ | |
45 | 0; \ | |
46 | }) | |
aa1c3521 LDM |
47 | |
48 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr)) | |
49 | ||
a507d803 LDM |
50 | /* Temporaries for importing index handling */ |
51 | #define NOFAIL(x) (x) | |
52 | #define fatal(x...) do { } while (0) | |
53 | ||
8f788d58 LDM |
54 | /* Attributes */ |
55 | ||
a70c1e77 LDM |
56 | #define _must_check_ __attribute__((warn_unused_result)) |
57 | #define _printf_format_(a,b) __attribute__((format (printf, a, b))) | |
9e2eadb1 | 58 | #define _unused_ __attribute__((unused)) |
a70c1e77 | 59 | #define _always_inline_ __inline__ __attribute__((always_inline)) |
d7aa6e23 | 60 | #define _cleanup_(x) __attribute__((cleanup(x))) |
d96ca9c4 LDM |
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 |