1
/*
2
 * Copyright 2008-2011 Various Authors
3
 * Copyright 2005 Timo Hirvonen
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as
7
 * published by the Free Software Foundation; either version 2 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#ifndef COMPILER_H
20
#define COMPILER_H
21
22
#include <stddef.h>
23
24
/*
25
 * GCC 2.96 or compatible required
26
 */
27
#if defined(__GNUC__)
28
29
#if __GNUC__ > 3
30
#undef offsetof
31
#define offsetof(type, member) __builtin_offsetof(type, member)
32
#endif
33
34
/* Optimization: Condition @x is likely */
35
#define likely(x)	__builtin_expect(!!(x), 1)
36
37
/* Optimization: Condition @x is unlikely */
38
#define unlikely(x)	__builtin_expect(!!(x), 0)
39
40
#else
41
42
#define likely(x)	(x)
43
#define unlikely(x)	(x)
44
45
#endif
46
47
/* Optimization: Function never returns */
48
#define __NORETURN	__attribute__((__noreturn__))
49
50
/* Argument at index @fmt_idx is printf compatible format string and
51
 * argument at index @first_idx is the first format argument */
52
#define __FORMAT(fmt_idx, first_idx) __attribute__((format(printf, (fmt_idx), (first_idx))))
53
54
#if defined(__GNUC__) && (__GNUC__ >= 3)
55
56
/* Optimization: Pointer returned can't alias other pointers */
57
#define __MALLOC	__attribute__((__malloc__))
58
59
#else
60
61
#define __MALLOC
62
63
#endif
64
65
66
/**
67
 * container_of - cast a member of a structure out to the containing structure
68
 *
69
 * @ptr:	the pointer to the member.
70
 * @type:	the type of the container struct this is embedded in.
71
 * @member:	the name of the member within the struct.
72
 *
73
 */
74
#define container_of_portable(ptr, type, member) \
75
	((type *)(void *)( (char *)(ptr) - offsetof(type,member) ))
76
#undef container_of
77
#if defined(__GNUC__)
78
#define container_of(ptr, type, member) __extension__ ({		\
79
	const __typeof__( ((type *)0)->member ) *__mptr = (ptr);	\
80
	container_of_portable(__mptr, type, member);})
81
#else
82
#define container_of(ptr, type, member) container_of_portable(ptr, type, member)
83
#endif
84
85
#endif