blob: d09d6645eb13aef3af621e6c1bab8611e52c7c9f [file] [log] [blame]
Lucas De Marchif87081b2011-11-23 12:21:29 -02001/*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011 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
8 * License as published by the Free Software Foundation version 2.1.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
Lucas De Marchi88e9c122011-11-23 12:23:46 -020020#ifndef _LIBKMOD_MACRO_H_
21#define _LIBKMOD_MACRO_H_
Lucas De Marchi6924e472011-11-22 05:38:28 -020022
23#include <stddef.h>
24
25#define BUILD_ASSERT(cond) \
26 do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
27
28#define EXPR_BUILD_ASSERT(cond) \
29 (sizeof(char [1 - 2*!(cond)]) - 1)
30
31#if HAVE_TYPEOF
32#define check_type(expr, type) \
33 ((typeof(expr) *)0 != (type *)0)
34
35#define check_types_match(expr1, expr2) \
36 ((typeof(expr1) *)0 != (typeof(expr2) *)0)
37#else
38/* Without typeof, we can only test the sizes. */
39#define check_type(expr, type) \
40 EXPR_BUILD_ASSERT(sizeof(expr) == sizeof(type))
41
42#define check_types_match(expr1, expr2) \
43 EXPR_BUILD_ASSERT(sizeof(expr1) == sizeof(expr2))
44#endif /* HAVE_TYPEOF */
45
46#define container_of(member_ptr, containing_type, member) \
47 ((containing_type *) \
48 ((char *)(member_ptr) - offsetof(containing_type, member)) \
49 - check_types_match(*(member_ptr), ((containing_type *)0)->member))
50
Lucas De Marchiaa1c3522011-11-29 17:59:58 -020051/**
52 * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.
53 * @cond: the compile-time condition which must be true.
54 *
55 * Your compile will fail if the condition isn't true, or can't be evaluated
56 * by the compiler. This can be used in an expression: its value is "0".
57 *
58 * Example:
59 * #define foo_to_char(foo) \
60 * ((char *)(foo) \
61 * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0))
62 */
63#define BUILD_ASSERT_OR_ZERO(cond) \
64 (sizeof(char [1 - 2*!(cond)]) - 1)
65
66/* Two gcc extensions.
67 * &a[0] degrades to a pointer: a different type from an array */
68#define _array_size_chk(arr) \
69 BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \
70 typeof(&(arr)[0])))
71
72#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
73
Lucas De Marchi8f788d52011-11-25 01:22:56 -020074/* Attributes */
75
Lucas De Marchif87081b2011-11-23 12:21:29 -020076#define __must_check __attribute__((warn_unused_result))
77#define __printf_format(a,b) __attribute__((format (printf, a, b)))
78#if !defined(__always_inline)
79#define __always_inline __inline__ __attribute__((always_inline))
80#endif
81
Lucas De Marchi6924e472011-11-22 05:38:28 -020082#endif