blob: 19014d1d343e3727bd4d98cb5ee3e5ef5761445e [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
5 * Copyright (C) 2011 Lucas De Marchi <lucas.de.marchi@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation version 2.1.
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 */
20
Lucas De Marchi6924e472011-11-22 05:38:28 -020021#ifndef _LIBKMOD_UTIL_H_
22#define _LIBKMOD_UTIL_H_
23
24#include <stddef.h>
25
26#define BUILD_ASSERT(cond) \
27 do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
28
29#define EXPR_BUILD_ASSERT(cond) \
30 (sizeof(char [1 - 2*!(cond)]) - 1)
31
32#if HAVE_TYPEOF
33#define check_type(expr, type) \
34 ((typeof(expr) *)0 != (type *)0)
35
36#define check_types_match(expr1, expr2) \
37 ((typeof(expr1) *)0 != (typeof(expr2) *)0)
38#else
39/* Without typeof, we can only test the sizes. */
40#define check_type(expr, type) \
41 EXPR_BUILD_ASSERT(sizeof(expr) == sizeof(type))
42
43#define check_types_match(expr1, expr2) \
44 EXPR_BUILD_ASSERT(sizeof(expr1) == sizeof(expr2))
45#endif /* HAVE_TYPEOF */
46
47#define container_of(member_ptr, containing_type, member) \
48 ((containing_type *) \
49 ((char *)(member_ptr) - offsetof(containing_type, member)) \
50 - check_types_match(*(member_ptr), ((containing_type *)0)->member))
51
Lucas De Marchif87081b2011-11-23 12:21:29 -020052#define __must_check __attribute__((warn_unused_result))
53#define __printf_format(a,b) __attribute__((format (printf, a, b)))
54#if !defined(__always_inline)
55#define __always_inline __inline__ __attribute__((always_inline))
56#endif
57
Lucas De Marchi6924e472011-11-22 05:38:28 -020058#endif