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