Commit | Line | Data |
---|---|---|
c66d2821 PMF |
1 | #ifndef KERNELCOMPAT_H |
2 | #define KERNELCOMPAT_H | |
3 | ||
1ae7f074 PMF |
4 | #include <kcompat.h> |
5 | ||
59b161cd PMF |
6 | #include "compiler.h" |
7 | ||
c66d2821 PMF |
8 | #include <string.h> |
9 | ||
10 | #define container_of(ptr, type, member) ({ \ | |
11 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | |
12 | (type *)( (char *)__mptr - offsetof(type,member) );}) | |
13 | ||
b6bf28ec PMF |
14 | #define KERN_DEBUG "" |
15 | #define KERN_NOTICE "" | |
16 | #define KERN_INFO "" | |
17 | #define KERN_ERR "" | |
18 | #define KERN_ALERT "" | |
bb07823d | 19 | #define KERN_WARNING "" |
c66d2821 | 20 | |
59b161cd PMF |
21 | /* ERROR OPS */ |
22 | ||
23 | #define MAX_ERRNO 4095 | |
24 | ||
25 | #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) | |
26 | ||
c66d2821 PMF |
27 | static inline void *ERR_PTR(long error) |
28 | { | |
59b161cd | 29 | return (void *) error; |
c66d2821 PMF |
30 | } |
31 | ||
59b161cd PMF |
32 | static inline long PTR_ERR(const void *ptr) |
33 | { | |
34 | return (long) ptr; | |
35 | } | |
36 | ||
37 | static inline long IS_ERR(const void *ptr) | |
38 | { | |
39 | return IS_ERR_VALUE((unsigned long)ptr); | |
40 | } | |
41 | ||
42 | ||
43 | /* FIXED SIZE INTEGERS */ | |
c66d2821 | 44 | |
1ae7f074 | 45 | //#include <stdint.h> |
c66d2821 | 46 | |
1ae7f074 PMF |
47 | //typedef uint8_t u8; |
48 | //typedef uint16_t u16; | |
49 | //typedef uint32_t u32; | |
50 | //typedef uint64_t u64; | |
c66d2821 | 51 | |
b6bf28ec PMF |
52 | #define min_t(type, x, y) ({ \ |
53 | type __min1 = (x); \ | |
54 | type __min2 = (y); \ | |
55 | __min1 < __min2 ? __min1: __min2; }) | |
56 | ||
57 | #define max_t(type, x, y) ({ \ | |
58 | type __max1 = (x); \ | |
59 | type __max2 = (y); \ | |
60 | __max1 > __max2 ? __max1: __max2; }) | |
61 | ||
62 | ||
63 | /* MUTEXES */ | |
c66d2821 PMF |
64 | |
65 | #include <pthread.h> | |
66 | ||
67 | #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER; | |
ba6459ba | 68 | #define DECLARE_MUTEX(m) extern pthread_mutex_t (m); |
c66d2821 PMF |
69 | |
70 | #define mutex_lock(m) pthread_mutex_lock(m) | |
71 | ||
72 | #define mutex_unlock(m) pthread_mutex_unlock(m) | |
73 | ||
bb07823d | 74 | |
b6bf28ec | 75 | /* MALLOCATION */ |
c66d2821 PMF |
76 | |
77 | #include <stdlib.h> | |
78 | ||
79 | #define kmalloc(s, t) malloc(s) | |
ba6459ba | 80 | #define kzalloc(s, t) zmalloc(s) |
c66d2821 PMF |
81 | #define kfree(p) free((void *)p) |
82 | #define kstrdup(s, t) strdup(s) | |
83 | ||
ba6459ba PMF |
84 | #define zmalloc(s) calloc(1, s) |
85 | ||
bb07823d PMF |
86 | #define GFP_KERNEL |
87 | ||
b6bf28ec | 88 | /* PRINTK */ |
c66d2821 PMF |
89 | |
90 | #include <stdio.h> | |
91 | #define printk(fmt, args...) printf(fmt, ## args) | |
92 | ||
59b161cd PMF |
93 | /* MEMORY BARRIERS */ |
94 | ||
59b161cd PMF |
95 | #define smp_mb__after_atomic_inc() do {} while(0) |
96 | ||
59b161cd | 97 | /* RCU */ |
c66d2821 | 98 | |
09938485 PMF |
99 | #include "urcu.h" |
100 | #define call_rcu_sched(a,b) b(a); synchronize_rcu() | |
101 | #define rcu_barrier_sched() do {} while(0) /* this nop is ok if call_rcu_sched does a synchronize_rcu() */ | |
102 | #define rcu_read_lock_sched_notrace() rcu_read_lock() | |
103 | #define rcu_read_unlock_sched_notrace() rcu_read_unlock() | |
c66d2821 | 104 | |
59b161cd | 105 | /* ATOMICITY */ |
b6bf28ec | 106 | |
59b161cd PMF |
107 | #include <signal.h> |
108 | ||
59b161cd PMF |
109 | static inline int atomic_dec_and_test(atomic_t *p) |
110 | { | |
111 | (p->counter)--; | |
112 | return !p->counter; | |
113 | } | |
114 | ||
115 | static inline void atomic_set(atomic_t *p, int v) | |
116 | { | |
117 | p->counter=v; | |
118 | } | |
119 | ||
120 | static inline void atomic_inc(atomic_t *p) | |
121 | { | |
122 | p->counter++; | |
123 | } | |
124 | ||
125 | static int atomic_read(atomic_t *p) | |
126 | { | |
127 | return p->counter; | |
128 | } | |
c66d2821 | 129 | |
bb07823d PMF |
130 | #define atomic_long_t atomic_t |
131 | #define atomic_long_set atomic_set | |
132 | #define atomic_long_read atomic_read | |
133 | ||
09938485 | 134 | //#define __xg(x) ((volatile long *)(x)) |
bb07823d PMF |
135 | |
136 | #define cmpxchg(ptr, o, n) \ | |
137 | ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \ | |
138 | (unsigned long)(n), sizeof(*(ptr)))) | |
139 | ||
c1dea0b3 PMF |
140 | //#define local_cmpxchg cmpxchg |
141 | #define local_cmpxchg(l, o, n) (cmpxchg(&((l)->a.counter), (o), (n))) | |
142 | ||
bb07823d PMF |
143 | #define atomic_long_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new))) |
144 | ||
145 | ||
5f54827b PMF |
146 | /* LOCAL OPS */ |
147 | ||
c1dea0b3 PMF |
148 | //typedef int local_t; |
149 | typedef struct | |
150 | { | |
151 | atomic_long_t a; | |
152 | } local_t; | |
153 | ||
bb07823d | 154 | |
c1dea0b3 | 155 | static inline void local_inc(local_t *l) |
bb07823d | 156 | { |
c1dea0b3 | 157 | (l->a.counter)++; |
bb07823d PMF |
158 | } |
159 | ||
c1dea0b3 | 160 | static inline void local_set(local_t *l, int v) |
bb07823d | 161 | { |
c1dea0b3 | 162 | l->a.counter = v; |
bb07823d PMF |
163 | } |
164 | ||
c1dea0b3 | 165 | static inline void local_add(int v, local_t *l) |
bb07823d | 166 | { |
c1dea0b3 | 167 | l->a.counter += v; |
bb07823d PMF |
168 | } |
169 | ||
c1dea0b3 | 170 | static int local_add_return(int v, local_t *l) |
bb07823d | 171 | { |
c1dea0b3 | 172 | return l->a.counter += v; |
bb07823d PMF |
173 | } |
174 | ||
c1dea0b3 | 175 | static inline int local_read(local_t *l) |
bb07823d | 176 | { |
c1dea0b3 | 177 | return l->a.counter; |
bb07823d | 178 | } |
5f54827b PMF |
179 | |
180 | ||
181 | /* ATTRIBUTES */ | |
b6bf28ec | 182 | |
59b161cd | 183 | #define ____cacheline_aligned |
5f54827b PMF |
184 | #define __init |
185 | #define __exit | |
c66d2821 | 186 | |
b6bf28ec PMF |
187 | /* MATH */ |
188 | ||
189 | static inline unsigned int hweight32(unsigned int w) | |
190 | { | |
191 | unsigned int res = w - ((w >> 1) & 0x55555555); | |
192 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); | |
193 | res = (res + (res >> 4)) & 0x0F0F0F0F; | |
194 | res = res + (res >> 8); | |
195 | return (res + (res >> 16)) & 0x000000FF; | |
196 | } | |
197 | ||
198 | static inline int fls(int x) | |
199 | { | |
200 | int r; | |
201 | //ust// #ifdef CONFIG_X86_CMOV | |
202 | asm("bsrl %1,%0\n\t" | |
203 | "cmovzl %2,%0" | |
204 | : "=&r" (r) : "rm" (x), "rm" (-1)); | |
205 | //ust// #else | |
206 | //ust// asm("bsrl %1,%0\n\t" | |
207 | //ust// "jnz 1f\n\t" | |
208 | //ust// "movl $-1,%0\n" | |
209 | //ust// "1:" : "=r" (r) : "rm" (x)); | |
210 | //ust// #endif | |
211 | return r + 1; | |
212 | } | |
213 | ||
214 | static __inline__ int get_count_order(unsigned int count) | |
215 | { | |
216 | int order; | |
217 | ||
218 | order = fls(count) - 1; | |
219 | if (count & (count - 1)) | |
220 | order++; | |
221 | return order; | |
222 | } | |
223 | ||
224 | ||
5f54827b PMF |
225 | |
226 | ||
227 | #include <unistd.h> | |
228 | ||
229 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) | |
230 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) | |
231 | #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE) | |
232 | #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) | |
bb07823d | 233 | #define PAGE_MASK (PAGE_SIZE-1) |
5f54827b PMF |
234 | |
235 | ||
236 | ||
237 | ||
b6bf28ec PMF |
238 | /* ARRAYS */ |
239 | ||
240 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | |
241 | ||
242 | /* TRACE CLOCK */ | |
243 | ||
98963de4 PMF |
244 | //ust// static inline u64 trace_clock_read64(void) |
245 | //ust// { | |
246 | //ust// uint32_t low; | |
247 | //ust// uint32_t high; | |
248 | //ust// uint64_t retval; | |
249 | //ust// __asm__ volatile ("rdtsc\n" : "=a" (low), "=d" (high)); | |
250 | //ust// | |
251 | //ust// retval = high; | |
252 | //ust// retval <<= 32; | |
253 | //ust// return retval | low; | |
254 | //ust// } | |
255 | ||
b6bf28ec PMF |
256 | static inline u64 trace_clock_read64(void) |
257 | { | |
98963de4 PMF |
258 | struct timeval tv; |
259 | u64 retval; | |
260 | ||
261 | gettimeofday(&tv, NULL); | |
262 | retval = tv.tv_sec; | |
263 | retval *= 1000000; | |
264 | retval += tv.tv_usec; | |
265 | ||
266 | return retval; | |
b6bf28ec PMF |
267 | } |
268 | ||
98963de4 | 269 | static inline u64 trace_clock_frequency(void) |
b6bf28ec | 270 | { |
98963de4 | 271 | return 1000000LL; |
b6bf28ec PMF |
272 | } |
273 | ||
274 | static inline u32 trace_clock_freq_scale(void) | |
275 | { | |
98963de4 | 276 | return 1; |
b6bf28ec PMF |
277 | } |
278 | ||
8d938dbd PMF |
279 | |
280 | /* LISTS */ | |
281 | ||
282 | #define list_add_rcu list_add | |
283 | #define list_for_each_entry_rcu list_for_each_entry | |
284 | ||
285 | ||
286 | #define EXPORT_SYMBOL_GPL(a) /*nothing*/ | |
287 | ||
ba6459ba PMF |
288 | #define smp_processor_id() (-1) |
289 | ||
c66d2821 | 290 | #endif /* KERNELCOMPAT_H */ |