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