2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 #ifndef __SHARED_COMPILER_H
18 #define __SHARED_COMPILER_H
20 #define __noinline __attribute__((noinline))
21 #define __inline __attribute__((always_inline))
22 #define __hot __attribute__((hot))
23 #define __cold __attribute__((cold))
24 #define __unused __attribute__((unused))
25 #define __force __attribute__((force))
26 #define __section(s) __attribute__((section("." # s)))
27 #define __packed __attribute__((packed))
29 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
30 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
32 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
33 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
35 #define __roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
37 #define __must_be_array(a) \
38 BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
39 #define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]) + __must_be_array(arr))
41 #define BIT(b) (1 << (b))
42 #define MASK(w) (BIT(w) - 1)
45 #ifdef __compiler_offsetof
46 # define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
48 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
51 #define likely(x) __builtin_expect(!!(x), 1)
52 #define unlikely(x) __builtin_expect(!!(x), 0)
54 #define min(x, y) ({ \
55 typeof(x) _min1 = (x); \
56 typeof(y) _min2 = (y); \
57 (void) (&_min1 == &_min2); \
58 _min1 < _min2 ? _min1 : _min2; })
60 #define max(x, y) ({ \
61 typeof(x) _max1 = (x); \
62 typeof(y) _max2 = (y); \
63 (void) (&_max1 == &_max2); \
64 _max1 > _max2 ? _max1 : _max2; })
66 #define min_t(type, x, y) ({ \
69 __min1 < __min2 ? __min1 : __min2; })
71 #define max_t(type, x, y) ({ \
74 __max1 > __max2 ? __max1 : __max2; })
77 #define container_of(ptr, type, member) ({ \
78 const typeof(((type *)0)->member) * __mptr = (ptr); \
79 (type *)((char *)__mptr - offsetof(type, member)); })
81 #define MAX_ERRNO 4095
83 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
85 static inline void *ERR_PTR(long errornr)
87 return (void *) errornr;
90 static inline long PTR_ERR(const void *ptr)
95 static inline long IS_ERR(const void *ptr)
97 return IS_ERR_VALUE((unsigned long)ptr);
100 static inline long IS_ERR_OR_NULL(const void *ptr)
102 return !ptr || IS_ERR_VALUE((unsigned long)ptr);
105 #endif /* __SHARED_COMPILER_H */