2011-11-23 21:21:29 +07:00
|
|
|
/*
|
|
|
|
* libkmod - interface to kernel module operations
|
|
|
|
*
|
2013-01-16 20:27:21 +07:00
|
|
|
* Copyright (C) 2011-2013 ProFUSION embedded systems
|
2011-11-23 21:21:29 +07:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
2011-12-13 03:24:35 +07:00
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2011-11-23 21:21:29 +07:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2012-07-18 20:19:48 +07:00
|
|
|
#pragma once
|
2011-11-22 14:38:28 +07:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2013-09-06 20:27:04 +07:00
|
|
|
#if defined(HAVE_STATIC_ASSERT)
|
2013-04-16 00:09:05 +07:00
|
|
|
#define assert_cc(expr) \
|
|
|
|
_Static_assert((expr), #expr)
|
2013-09-06 20:27:04 +07:00
|
|
|
#else
|
|
|
|
#define assert_cc(expr) \
|
|
|
|
do { (void) sizeof(char [1 - 2*!(expr)]); } while(0)
|
|
|
|
#endif
|
2011-11-22 14:38:28 +07:00
|
|
|
|
|
|
|
#define check_types_match(expr1, expr2) \
|
|
|
|
((typeof(expr1) *)0 != (typeof(expr2) *)0)
|
|
|
|
|
|
|
|
#define container_of(member_ptr, containing_type, member) \
|
|
|
|
((containing_type *) \
|
|
|
|
((char *)(member_ptr) - offsetof(containing_type, member)) \
|
|
|
|
- check_types_match(*(member_ptr), ((containing_type *)0)->member))
|
|
|
|
|
2011-11-30 02:59:58 +07:00
|
|
|
|
|
|
|
/* Two gcc extensions.
|
|
|
|
* &a[0] degrades to a pointer: a different type from an array */
|
2013-04-16 00:09:05 +07:00
|
|
|
#define _array_size_chk(arr) ({ \
|
|
|
|
assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \
|
|
|
|
0; \
|
|
|
|
})
|
2011-11-30 02:59:58 +07:00
|
|
|
|
|
|
|
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
|
|
|
|
|
2011-11-30 23:35:39 +07:00
|
|
|
/* Temporaries for importing index handling */
|
|
|
|
#define NOFAIL(x) (x)
|
|
|
|
#define fatal(x...) do { } while (0)
|
|
|
|
|
2011-11-25 10:22:56 +07:00
|
|
|
/* Attributes */
|
|
|
|
|
2012-05-24 06:27:23 +07:00
|
|
|
#define _must_check_ __attribute__((warn_unused_result))
|
|
|
|
#define _printf_format_(a,b) __attribute__((format (printf, a, b)))
|
2012-05-24 06:28:53 +07:00
|
|
|
#define _unused_ __attribute__((unused))
|
2012-05-24 06:27:23 +07:00
|
|
|
#define _always_inline_ __inline__ __attribute__((always_inline))
|
2013-11-14 09:19:15 +07:00
|
|
|
#define _cleanup_(x) __attribute__((cleanup(x)))
|
2013-12-18 04:10:16 +07:00
|
|
|
|
|
|
|
/* Define C11 noreturn without <stdnoreturn.h> and even on older gcc
|
|
|
|
* compiler versions */
|
|
|
|
#ifndef noreturn
|
|
|
|
#if __STDC_VERSION__ >= 201112L
|
|
|
|
#define noreturn _Noreturn
|
|
|
|
#else
|
|
|
|
#define noreturn __attribute__((noreturn))
|
|
|
|
#endif
|
|
|
|
#endif
|