Update BUG_ON and WARN_ON
[platform/upstream/btrfs-progs.git] / kerncompat.h
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #ifndef __KERNCOMPAT
20 #define __KERNCOMPAT
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <endian.h>
26 #include <byteswap.h>
27 #include <assert.h>
28
29 #ifndef READ
30 #define READ 0
31 #define WRITE 1
32 #define READA 2
33 #endif
34
35 #define gfp_t int
36 #define get_cpu_var(p) (p)
37 #define __get_cpu_var(p) (p)
38 #define BITS_PER_LONG (sizeof(long) * 8)
39 #define __GFP_BITS_SHIFT 20
40 #define __GFP_BITS_MASK ((int)((1 << __GFP_BITS_SHIFT) - 1))
41 #define GFP_KERNEL 0
42 #define GFP_NOFS 0
43 #define __read_mostly
44 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
45 #define ULONG_MAX       (~0UL)
46 #define BUG() abort()
47 #ifdef __CHECKER__
48 #define __force    __attribute__((force))
49 #define __bitwise__ __attribute__((bitwise))
50 #else
51 #define __force
52 #define __bitwise__
53 #endif
54
55 #ifndef __CHECKER__
56 #include <asm/types.h>
57 typedef __u32 u32;
58 typedef __u64 u64;
59 typedef __u16 u16;
60 typedef __u8 u8;
61 #else
62 typedef unsigned int u32;
63 typedef unsigned int __u32;
64 typedef unsigned long long u64;
65 typedef unsigned char u8;
66 typedef unsigned short u16;
67 #endif
68
69
70 struct vma_shared { int prio_tree_node; };
71 struct vm_area_struct {
72         unsigned long vm_pgoff;
73         unsigned long vm_start;
74         unsigned long vm_end;
75         struct vma_shared shared;
76 };
77
78 struct page {
79         unsigned long index;
80 };
81
82 struct mutex {
83         unsigned long lock;
84 };
85
86 #define mutex_init(m)                                           \
87 do {                                                            \
88         (m)->lock = 1;                                          \
89 } while (0)
90
91 static inline void mutex_lock(struct mutex *m)
92 {
93         m->lock--;
94 }
95
96 static inline void mutex_unlock(struct mutex *m)
97 {
98         m->lock++;
99 }
100
101 static inline int mutex_is_locked(struct mutex *m)
102 {
103         return (m->lock != 1);
104 }
105
106 #define cond_resched()          do { } while (0)
107 #define preempt_enable()        do { } while (0)
108 #define preempt_disable()       do { } while (0)
109
110 #define BITOP_MASK(nr)          (1UL << ((nr) % BITS_PER_LONG))
111 #define BITOP_WORD(nr)          ((nr) / BITS_PER_LONG)
112
113 /**
114  * __set_bit - Set a bit in memory
115  * @nr: the bit to set
116  * @addr: the address to start counting from
117  *
118  * Unlike set_bit(), this function is non-atomic and may be reordered.
119  * If it's called on the same region of memory simultaneously, the effect
120  * may be that only one operation succeeds.
121  */
122 static inline void __set_bit(int nr, volatile unsigned long *addr)
123 {
124         unsigned long mask = BITOP_MASK(nr);
125         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
126
127         *p  |= mask;
128 }
129
130 static inline void __clear_bit(int nr, volatile unsigned long *addr)
131 {
132         unsigned long mask = BITOP_MASK(nr);
133         unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
134
135         *p &= ~mask;
136 }
137
138 /**
139  * test_bit - Determine whether a bit is set
140  * @nr: bit number to test
141  * @addr: Address to start counting from
142  */
143 static inline int test_bit(int nr, const volatile unsigned long *addr)
144 {
145         return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
146 }
147
148 /*
149  * error pointer
150  */
151 #define MAX_ERRNO       4095
152 #define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)
153
154 static inline void *ERR_PTR(long error)
155 {
156         return (void *) error;
157 }
158
159 static inline long PTR_ERR(const void *ptr)
160 {
161         return (long) ptr;
162 }
163
164 static inline long IS_ERR(const void *ptr)
165 {
166         return IS_ERR_VALUE((unsigned long)ptr);
167 }
168
169 /*
170  * max/min macro
171  */
172 #define min(x,y) ({ \
173         typeof(x) _x = (x);     \
174         typeof(y) _y = (y);     \
175         (void) (&_x == &_y);            \
176         _x < _y ? _x : _y; })
177
178 #define max(x,y) ({ \
179         typeof(x) _x = (x);     \
180         typeof(y) _y = (y);     \
181         (void) (&_x == &_y);            \
182         _x > _y ? _x : _y; })
183
184 #define min_t(type,x,y) \
185         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
186 #define max_t(type,x,y) \
187         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
188
189 /*
190  * printk
191  */
192 #define printk(fmt, args...) fprintf(stderr, fmt, ##args)
193 #define KERN_CRIT       ""
194
195 /*
196  * kmalloc/kfree
197  */
198 #define kmalloc(x, y) malloc(x)
199 #define kzalloc(x, y) calloc(1, x)
200 #define kstrdup(x, y) strdup(x)
201 #define kfree(x) free(x)
202
203 #define BUG_ON(c) assert(!(c))
204 #define WARN_ON(c) assert(!(c))
205
206 #undef offsetof
207 #ifdef __compiler_offsetof
208 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
209 #else
210 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
211 #endif
212
213 #define container_of(ptr, type, member) ({                      \
214         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
215                 (type *)( (char *)__mptr - offsetof(type,member) );})
216 #ifdef __CHECKER__
217 #define __CHECK_ENDIAN__
218 #define __bitwise __bitwise__
219 #else
220 #define __bitwise
221 #endif
222
223 typedef u16 __bitwise __le16;
224 typedef u16 __bitwise __be16;
225 typedef u32 __bitwise __le32;
226 typedef u32 __bitwise __be32;
227 typedef u64 __bitwise __le64;
228 typedef u64 __bitwise __be64;
229
230 /* Macros to generate set/get funcs for the struct fields
231  * assume there is a lefoo_to_cpu for every type, so lets make a simple
232  * one for u8:
233  */
234 #define le8_to_cpu(v) (v)
235 #define cpu_to_le8(v) (v)
236 #define __le8 u8
237
238 #if __BYTE_ORDER == __BIG_ENDIAN
239 #define cpu_to_le64(x) ((__force __le64)(u64)(bswap_64(x)))
240 #define le64_to_cpu(x) ((__force u64)(__le64)(bswap_64(x)))
241 #define cpu_to_le32(x) ((__force __le32)(u32)(bswap_32(x)))
242 #define le32_to_cpu(x) ((__force u32)(__le32)(bswap_32(x)))
243 #define cpu_to_le16(x) ((__force __le16)(u16)(bswap_16(x)))
244 #define le16_to_cpu(x) ((__force u16)(__le16)(bswap_16(x)))
245 #else
246 #define cpu_to_le64(x) ((__force __le64)(u64)(x))
247 #define le64_to_cpu(x) ((__force u64)(__le64)(x))
248 #define cpu_to_le32(x) ((__force __le32)(u32)(x))
249 #define le32_to_cpu(x) ((__force u32)(__le32)(x))
250 #define cpu_to_le16(x) ((__force __le16)(u16)(x))
251 #define le16_to_cpu(x) ((__force u16)(__le16)(x))
252 #endif
253 #endif
254
255 #ifndef noinline
256 #define noinline
257 #endif