blob: cd4b2433010af5cfa11d22a370ae5c5d2290d504 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef __COMPILER_H
24#define __COMPILER_H
25
26#ifndef __ASSEMBLY__
27
28#if __GNUC__
29#define likely(x) __builtin_expect(!!(x), 1)
30#define unlikely(x) __builtin_expect(!!(x), 0)
31#define __UNUSED __attribute__((__unused__))
32#define __PACKED __attribute__((packed))
33#define __ALIGNED(x) __attribute__((aligned(x)))
34#define __PRINTFLIKE(__fmt,__varargs) __attribute__((__format__ (__printf__, __fmt, __varargs)))
35#define __SCANFLIKE(__fmt,__varargs) __attribute__((__format__ (__scanf__, __fmt, __varargs)))
36#define __SECTION(x) __attribute((section(x)))
37#define __PURE __attribute((pure))
38#define __CONST __attribute((const))
39#define __NO_RETURN __attribute__((noreturn))
40#define __MALLOC __attribute__((malloc))
41#define __WEAK __attribute__((weak))
42#define __GNU_INLINE __attribute__((gnu_inline))
43#define __GET_CALLER(x) __builtin_return_address(0)
44
45/* look for gcc 3.0 and above */
46#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 0)
47#define __ALWAYS_INLINE __attribute__((always_inline))
48#else
49#define __ALWAYS_INLINE
50#endif
51
52/* look for gcc 3.1 and above */
53#if !defined(__DEPRECATED) // seems to be built in in some versions of the compiler
54#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
55#define __DEPRECATED __attribute((deprecated))
56#else
57#define __DEPRECATED
58#endif
59#endif
60
61/* look for gcc 3.3 and above */
62#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
63/* the may_alias attribute was introduced in gcc 3.3; before that, there
64 * was no way to specify aliasiang rules on a type-by-type basis */
65#define __MAY_ALIAS __attribute__((may_alias))
66
67/* nonnull was added in gcc 3.3 as well */
68#define __NONNULL(x) __attribute((nonnull x))
69#else
70#define __MAY_ALIAS
71#define __NONNULL(x)
72#endif
73
74/* look for gcc 3.4 and above */
75#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
76#define __WARN_UNUSED_RESULT __attribute((warn_unused_result))
77#else
78#define __WARN_UNUSED_RESULT
79#endif
80
81#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
82#define __EXTERNALLY_VISIBLE __attribute__((externally_visible))
83#else
84#define __EXTERNALLY_VISIBLE
85#endif
86
87#else
88
89#define likely(x) (x)
90#define unlikely(x) (x)
91#define __UNUSED
92#define __PACKED
93#define __ALIGNED(x)
94#define __PRINTFLIKE(__fmt,__varargs)
95#define __SCANFLIKE(__fmt,__varargs)
96#define __SECTION(x)
97#define __PURE
98#define __CONST
99#define __NONNULL(x)
100#define __DEPRECATED
101#define __WARN_UNUSED_RESULT
102#define __ALWAYS_INLINE
103#define __MAY_ALIAS
104#define __NO_RETURN
105#endif
106
107#endif
108
109#endif