]> git.ipfire.org Git - thirdparty/kmod.git/blame - shared/macro.h
Remove FSF mailing address
[thirdparty/kmod.git] / shared / macro.h
CommitLineData
f87081b4 1/*
576dd439 2 * kmod - interface to kernel module operations
f87081b4 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
dea2dfee 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
f87081b4 18 */
e8fd8fec 19#pragma once
6924e47a
LDM
20
21#include <stddef.h>
22
dc8ed09f 23#if defined(HAVE_STATIC_ASSERT)
8efede20
LDM
24#define assert_cc(expr) \
25 _Static_assert((expr), #expr)
dc8ed09f
TP
26#else
27#define assert_cc(expr) \
28 do { (void) sizeof(char [1 - 2*!(expr)]); } while(0)
29#endif
6924e47a 30
6924e47a
LDM
31#define check_types_match(expr1, expr2) \
32 ((typeof(expr1) *)0 != (typeof(expr2) *)0)
6924e47a
LDM
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
aa1c3521
LDM
39
40/* Two gcc extensions.
41 * &a[0] degrades to a pointer: a different type from an array */
8efede20
LDM
42#define _array_size_chk(arr) ({ \
43 assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \
44 0; \
45 })
aa1c3521
LDM
46
47#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
48
a507d803
LDM
49/* Temporaries for importing index handling */
50#define NOFAIL(x) (x)
51#define fatal(x...) do { } while (0)
52
8f788d58
LDM
53/* Attributes */
54
a70c1e77
LDM
55#define _must_check_ __attribute__((warn_unused_result))
56#define _printf_format_(a,b) __attribute__((format (printf, a, b)))
9e2eadb1 57#define _unused_ __attribute__((unused))
a70c1e77 58#define _always_inline_ __inline__ __attribute__((always_inline))
d7aa6e23 59#define _cleanup_(x) __attribute__((cleanup(x)))
d96ca9c4
LDM
60
61/* Define C11 noreturn without <stdnoreturn.h> and even on older gcc
62 * compiler versions */
63#ifndef noreturn
64#if __STDC_VERSION__ >= 201112L
65#define noreturn _Noreturn
66#else
67#define noreturn __attribute__((noreturn))
68#endif
69#endif
43289820
LDM
70
71#define UNIQ __COUNTER__