ust: first try at blocking support for consumer
[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 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
109static inline int atomic_dec_and_test(atomic_t *p)
110{
111 (p->counter)--;
112 return !p->counter;
113}
114
115static inline void atomic_set(atomic_t *p, int v)
116{
117 p->counter=v;
118}
119
120static inline void atomic_inc(atomic_t *p)
121{
122 p->counter++;
123}
124
125static 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
134#include "asm.h"
135
09938485 136//#define __xg(x) ((volatile long *)(x))
bb07823d
PMF
137
138#define cmpxchg(ptr, o, n) \
139 ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \
140 (unsigned long)(n), sizeof(*(ptr))))
141
c1dea0b3
PMF
142//#define local_cmpxchg cmpxchg
143#define local_cmpxchg(l, o, n) (cmpxchg(&((l)->a.counter), (o), (n)))
144
bb07823d
PMF
145#define atomic_long_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
146
147
5f54827b
PMF
148/* LOCAL OPS */
149
c1dea0b3
PMF
150//typedef int local_t;
151typedef struct
152{
153 atomic_long_t a;
154} local_t;
155
bb07823d 156
c1dea0b3 157static inline void local_inc(local_t *l)
bb07823d 158{
c1dea0b3 159 (l->a.counter)++;
bb07823d
PMF
160}
161
c1dea0b3 162static inline void local_set(local_t *l, int v)
bb07823d 163{
c1dea0b3 164 l->a.counter = v;
bb07823d
PMF
165}
166
c1dea0b3 167static inline void local_add(int v, local_t *l)
bb07823d 168{
c1dea0b3 169 l->a.counter += v;
bb07823d
PMF
170}
171
c1dea0b3 172static int local_add_return(int v, local_t *l)
bb07823d 173{
c1dea0b3 174 return l->a.counter += v;
bb07823d
PMF
175}
176
c1dea0b3 177static inline int local_read(local_t *l)
bb07823d 178{
c1dea0b3 179 return l->a.counter;
bb07823d 180}
5f54827b
PMF
181
182
183/* ATTRIBUTES */
b6bf28ec 184
59b161cd 185#define ____cacheline_aligned
5f54827b
PMF
186#define __init
187#define __exit
c66d2821 188
b6bf28ec
PMF
189/* MATH */
190
191static inline unsigned int hweight32(unsigned int w)
192{
193 unsigned int res = w - ((w >> 1) & 0x55555555);
194 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
195 res = (res + (res >> 4)) & 0x0F0F0F0F;
196 res = res + (res >> 8);
197 return (res + (res >> 16)) & 0x000000FF;
198}
199
200static inline int fls(int x)
201{
202 int r;
203//ust// #ifdef CONFIG_X86_CMOV
204 asm("bsrl %1,%0\n\t"
205 "cmovzl %2,%0"
206 : "=&r" (r) : "rm" (x), "rm" (-1));
207//ust// #else
208//ust// asm("bsrl %1,%0\n\t"
209//ust// "jnz 1f\n\t"
210//ust// "movl $-1,%0\n"
211//ust// "1:" : "=r" (r) : "rm" (x));
212//ust// #endif
213 return r + 1;
214}
215
216static __inline__ int get_count_order(unsigned int count)
217{
218 int order;
219
220 order = fls(count) - 1;
221 if (count & (count - 1))
222 order++;
223 return order;
224}
225
226
5f54827b
PMF
227
228
229#include <unistd.h>
230
231#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
232#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
233#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
234#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
bb07823d 235#define PAGE_MASK (PAGE_SIZE-1)
5f54827b
PMF
236
237
238
239
b6bf28ec
PMF
240/* ARRAYS */
241
242#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
243
244/* TRACE CLOCK */
245
98963de4
PMF
246//ust// static inline u64 trace_clock_read64(void)
247//ust// {
248//ust// uint32_t low;
249//ust// uint32_t high;
250//ust// uint64_t retval;
251//ust// __asm__ volatile ("rdtsc\n" : "=a" (low), "=d" (high));
252//ust//
253//ust// retval = high;
254//ust// retval <<= 32;
255//ust// return retval | low;
256//ust// }
257
b6bf28ec
PMF
258static inline u64 trace_clock_read64(void)
259{
98963de4
PMF
260 struct timeval tv;
261 u64 retval;
262
263 gettimeofday(&tv, NULL);
264 retval = tv.tv_sec;
265 retval *= 1000000;
266 retval += tv.tv_usec;
267
268 return retval;
b6bf28ec
PMF
269}
270
98963de4 271static inline u64 trace_clock_frequency(void)
b6bf28ec 272{
98963de4 273 return 1000000LL;
b6bf28ec
PMF
274}
275
276static inline u32 trace_clock_freq_scale(void)
277{
98963de4 278 return 1;
b6bf28ec
PMF
279}
280
8d938dbd
PMF
281
282/* LISTS */
283
284#define list_add_rcu list_add
285#define list_for_each_entry_rcu list_for_each_entry
286
287
288#define EXPORT_SYMBOL_GPL(a) /*nothing*/
289
ba6459ba
PMF
290#define smp_processor_id() (-1)
291
c66d2821 292#endif /* KERNELCOMPAT_H */
This page took 0.045568 seconds and 4 git commands to generate.