blob: 4fc54059519f535a209199af7e6b6f34797e38d6 [file] [log] [blame]
Lucas De Marchif87081b2011-11-23 12:21:29 -02001/*
Lucas De Marchi576dd432014-10-02 22:03:19 -03002 * kmod - interface to kernel module operations
Lucas De Marchif87081b2011-11-23 12:21:29 -02003 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Lucas De Marchif87081b2011-11-23 12:21:29 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchif87081b2011-11-23 12:21:29 -020010 *
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
Lucas De Marchidea2dfe2014-12-25 23:32:03 -020017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Lucas De Marchif87081b2011-11-23 12:21:29 -020018 */
Lucas De Marchie8fd8fe2012-07-18 10:19:48 -030019#pragma once
Lucas De Marchi6924e472011-11-22 05:38:28 -020020
21#include <stddef.h>
22
Thomas Petazzonidc8ed092013-09-06 15:27:04 +020023#if defined(HAVE_STATIC_ASSERT)
Lucas De Marchi8efede22013-04-15 14:09:05 -030024#define assert_cc(expr) \
25 _Static_assert((expr), #expr)
Thomas Petazzonidc8ed092013-09-06 15:27:04 +020026#else
27#define assert_cc(expr) \
28 do { (void) sizeof(char [1 - 2*!(expr)]); } while(0)
29#endif
Lucas De Marchi6924e472011-11-22 05:38:28 -020030
Lucas De Marchi6924e472011-11-22 05:38:28 -020031#define check_types_match(expr1, expr2) \
32 ((typeof(expr1) *)0 != (typeof(expr2) *)0)
Lucas De Marchi6924e472011-11-22 05:38:28 -020033
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
Lucas De Marchiaa1c3522011-11-29 17:59:58 -020039
40/* Two gcc extensions.
41 * &a[0] degrades to a pointer: a different type from an array */
Lucas De Marchi8efede22013-04-15 14:09:05 -030042#define _array_size_chk(arr) ({ \
43 assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \
44 0; \
45 })
Lucas De Marchiaa1c3522011-11-29 17:59:58 -020046
47#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
Lucas De Marchibf2fbab2015-01-14 14:31:34 -020048#define XSTRINGIFY(x) #x
49#define STRINGIFY(x) XSTRINGIFY(x)
Lucas De Marchiaa1c3522011-11-29 17:59:58 -020050
Lucas De Marchia507d802011-11-30 14:35:39 -020051/* Temporaries for importing index handling */
52#define NOFAIL(x) (x)
53#define fatal(x...) do { } while (0)
54
Lucas De Marchi8f788d52011-11-25 01:22:56 -020055/* Attributes */
56
Lucas De Marchia70c1e72012-05-23 20:27:23 -030057#define _must_check_ __attribute__((warn_unused_result))
58#define _printf_format_(a,b) __attribute__((format (printf, a, b)))
Lucas De Marchi9e2eadb2012-05-23 20:28:53 -030059#define _unused_ __attribute__((unused))
Lucas De Marchia70c1e72012-05-23 20:27:23 -030060#define _always_inline_ __inline__ __attribute__((always_inline))
Lucas De Marchid7aa6e22013-11-14 00:19:15 -020061#define _cleanup_(x) __attribute__((cleanup(x)))
Lucas De Marchid96ca9c2013-12-17 19:10:16 -020062
63/* Define C11 noreturn without <stdnoreturn.h> and even on older gcc
64 * compiler versions */
65#ifndef noreturn
Lucas De Marchi16a62c72015-02-26 13:02:04 -030066#if defined(HAVE_NORETURN)
Lucas De Marchid96ca9c2013-12-17 19:10:16 -020067#define noreturn _Noreturn
68#else
69#define noreturn __attribute__((noreturn))
70#endif
71#endif
Lucas De Marchi43289822014-10-09 14:29:04 -030072
73#define UNIQ __COUNTER__