ust: use libkcompat
[lttng-ust.git] / share / kernelcompat.h
CommitLineData
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
27static inline void *ERR_PTR(long error)
28{
59b161cd 29 return (void *) error;
c66d2821
PMF
30}
31
59b161cd
PMF
32static inline long PTR_ERR(const void *ptr)
33{
34 return (long) ptr;
35}
36
37static 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
PMF
74/* SPINLOCKS */
75
76typedef int spinlock_t;
77
78#define spin_lock(a) /* nothing */
79#define spin_unlock(a) /* nothing */
80#define spin_lock_init(a) /* nothing */
81
82
b6bf28ec 83/* MALLOCATION */
c66d2821
PMF
84
85#include <stdlib.h>
86
87#define kmalloc(s, t) malloc(s)
ba6459ba 88#define kzalloc(s, t) zmalloc(s)
c66d2821
PMF
89#define kfree(p) free((void *)p)
90#define kstrdup(s, t) strdup(s)
91
ba6459ba
PMF
92#define zmalloc(s) calloc(1, s)
93
bb07823d
PMF
94#define GFP_KERNEL
95
b6bf28ec 96/* PRINTK */
c66d2821
PMF
97
98#include <stdio.h>
99#define printk(fmt, args...) printf(fmt, ## args)
100
59b161cd
PMF
101/* MEMORY BARRIERS */
102
59b161cd
PMF
103#define smp_mb__after_atomic_inc() do {} while(0)
104
59b161cd 105/* RCU */
c66d2821 106
09938485
PMF
107#include "urcu.h"
108#define call_rcu_sched(a,b) b(a); synchronize_rcu()
109#define rcu_barrier_sched() do {} while(0) /* this nop is ok if call_rcu_sched does a synchronize_rcu() */
110#define rcu_read_lock_sched_notrace() rcu_read_lock()
111#define rcu_read_unlock_sched_notrace() rcu_read_unlock()
c66d2821 112
59b161cd 113/* ATOMICITY */
b6bf28ec 114
59b161cd
PMF
115#include <signal.h>
116
59b161cd
PMF
117static inline int atomic_dec_and_test(atomic_t *p)
118{
119 (p->counter)--;
120 return !p->counter;
121}
122
123static inline void atomic_set(atomic_t *p, int v)
124{
125 p->counter=v;
126}
127
128static inline void atomic_inc(atomic_t *p)
129{
130 p->counter++;
131}
132
133static int atomic_read(atomic_t *p)
134{
135 return p->counter;
136}
c66d2821 137
bb07823d
PMF
138#define atomic_long_t atomic_t
139#define atomic_long_set atomic_set
140#define atomic_long_read atomic_read
141
142#include "asm.h"
143
09938485 144//#define __xg(x) ((volatile long *)(x))
bb07823d
PMF
145
146#define cmpxchg(ptr, o, n) \
147 ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \
148 (unsigned long)(n), sizeof(*(ptr))))
149
c1dea0b3
PMF
150//#define local_cmpxchg cmpxchg
151#define local_cmpxchg(l, o, n) (cmpxchg(&((l)->a.counter), (o), (n)))
152
bb07823d
PMF
153#define atomic_long_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
154
155
5f54827b
PMF
156/* LOCAL OPS */
157
c1dea0b3
PMF
158//typedef int local_t;
159typedef struct
160{
161 atomic_long_t a;
162} local_t;
163
bb07823d 164
c1dea0b3 165static inline void local_inc(local_t *l)
bb07823d 166{
c1dea0b3 167 (l->a.counter)++;
bb07823d
PMF
168}
169
c1dea0b3 170static inline void local_set(local_t *l, int v)
bb07823d 171{
c1dea0b3 172 l->a.counter = v;
bb07823d
PMF
173}
174
c1dea0b3 175static inline void local_add(int v, local_t *l)
bb07823d 176{
c1dea0b3 177 l->a.counter += v;
bb07823d
PMF
178}
179
c1dea0b3 180static int local_add_return(int v, local_t *l)
bb07823d 181{
c1dea0b3 182 return l->a.counter += v;
bb07823d
PMF
183}
184
c1dea0b3 185static inline int local_read(local_t *l)
bb07823d 186{
c1dea0b3 187 return l->a.counter;
bb07823d 188}
5f54827b
PMF
189
190
191/* ATTRIBUTES */
b6bf28ec 192
59b161cd 193#define ____cacheline_aligned
5f54827b
PMF
194#define __init
195#define __exit
c66d2821 196
b6bf28ec
PMF
197/* MATH */
198
199static inline unsigned int hweight32(unsigned int w)
200{
201 unsigned int res = w - ((w >> 1) & 0x55555555);
202 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
203 res = (res + (res >> 4)) & 0x0F0F0F0F;
204 res = res + (res >> 8);
205 return (res + (res >> 16)) & 0x000000FF;
206}
207
208static inline int fls(int x)
209{
210 int r;
211//ust// #ifdef CONFIG_X86_CMOV
212 asm("bsrl %1,%0\n\t"
213 "cmovzl %2,%0"
214 : "=&r" (r) : "rm" (x), "rm" (-1));
215//ust// #else
216//ust// asm("bsrl %1,%0\n\t"
217//ust// "jnz 1f\n\t"
218//ust// "movl $-1,%0\n"
219//ust// "1:" : "=r" (r) : "rm" (x));
220//ust// #endif
221 return r + 1;
222}
223
224static __inline__ int get_count_order(unsigned int count)
225{
226 int order;
227
228 order = fls(count) - 1;
229 if (count & (count - 1))
230 order++;
231 return order;
232}
233
234
5f54827b
PMF
235
236
237#include <unistd.h>
238
239#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
240#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
241#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
242#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
bb07823d 243#define PAGE_MASK (PAGE_SIZE-1)
5f54827b
PMF
244
245
246
247
b6bf28ec
PMF
248/* ARRAYS */
249
250#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
251
252/* TRACE CLOCK */
253
98963de4
PMF
254//ust// static inline u64 trace_clock_read64(void)
255//ust// {
256//ust// uint32_t low;
257//ust// uint32_t high;
258//ust// uint64_t retval;
259//ust// __asm__ volatile ("rdtsc\n" : "=a" (low), "=d" (high));
260//ust//
261//ust// retval = high;
262//ust// retval <<= 32;
263//ust// return retval | low;
264//ust// }
265
b6bf28ec
PMF
266static inline u64 trace_clock_read64(void)
267{
98963de4
PMF
268 struct timeval tv;
269 u64 retval;
270
271 gettimeofday(&tv, NULL);
272 retval = tv.tv_sec;
273 retval *= 1000000;
274 retval += tv.tv_usec;
275
276 return retval;
b6bf28ec
PMF
277}
278
98963de4 279static inline u64 trace_clock_frequency(void)
b6bf28ec 280{
98963de4 281 return 1000000LL;
b6bf28ec
PMF
282}
283
284static inline u32 trace_clock_freq_scale(void)
285{
98963de4 286 return 1;
b6bf28ec
PMF
287}
288
8d938dbd
PMF
289
290/* LISTS */
291
292#define list_add_rcu list_add
293#define list_for_each_entry_rcu list_for_each_entry
294
295
296#define EXPORT_SYMBOL_GPL(a) /*nothing*/
297
ba6459ba
PMF
298#define smp_processor_id() (-1)
299
c66d2821 300#endif /* KERNELCOMPAT_H */
This page took 0.051988 seconds and 4 git commands to generate.