continue working on build system
[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 8#include <string.h>
0b0cd937 9#include <sys/time.h>
c66d2821
PMF
10
11#define container_of(ptr, type, member) ({ \
12 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
13 (type *)( (char *)__mptr - offsetof(type,member) );})
14
b6bf28ec
PMF
15#define KERN_DEBUG ""
16#define KERN_NOTICE ""
17#define KERN_INFO ""
18#define KERN_ERR ""
19#define KERN_ALERT ""
bb07823d 20#define KERN_WARNING ""
c66d2821 21
59b161cd
PMF
22/* ERROR OPS */
23
24#define MAX_ERRNO 4095
25
26#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
27
c66d2821
PMF
28static inline void *ERR_PTR(long error)
29{
59b161cd 30 return (void *) error;
c66d2821
PMF
31}
32
59b161cd
PMF
33static inline long PTR_ERR(const void *ptr)
34{
35 return (long) ptr;
36}
37
38static inline long IS_ERR(const void *ptr)
39{
40 return IS_ERR_VALUE((unsigned long)ptr);
41}
42
43
44/* FIXED SIZE INTEGERS */
c66d2821 45
1ae7f074 46//#include <stdint.h>
c66d2821 47
1ae7f074
PMF
48//typedef uint8_t u8;
49//typedef uint16_t u16;
50//typedef uint32_t u32;
51//typedef uint64_t u64;
c66d2821 52
b6bf28ec
PMF
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 */
c66d2821
PMF
65
66#include <pthread.h>
67
68#define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER;
ba6459ba 69#define DECLARE_MUTEX(m) extern pthread_mutex_t (m);
c66d2821
PMF
70
71#define mutex_lock(m) pthread_mutex_lock(m)
72
73#define mutex_unlock(m) pthread_mutex_unlock(m)
74
bb07823d 75
b6bf28ec 76/* MALLOCATION */
c66d2821
PMF
77
78#include <stdlib.h>
79
80#define kmalloc(s, t) malloc(s)
ba6459ba 81#define kzalloc(s, t) zmalloc(s)
c66d2821
PMF
82#define kfree(p) free((void *)p)
83#define kstrdup(s, t) strdup(s)
84
ba6459ba
PMF
85#define zmalloc(s) calloc(1, s)
86
bb07823d
PMF
87#define GFP_KERNEL
88
b6bf28ec 89/* PRINTK */
c66d2821
PMF
90
91#include <stdio.h>
92#define printk(fmt, args...) printf(fmt, ## args)
93
59b161cd
PMF
94/* MEMORY BARRIERS */
95
59b161cd
PMF
96#define smp_mb__after_atomic_inc() do {} while(0)
97
6cb88bc0
PMF
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()
c66d2821 105
59b161cd 106/* ATOMICITY */
b6bf28ec 107
59b161cd
PMF
108#include <signal.h>
109
59b161cd
PMF
110static inline int atomic_dec_and_test(atomic_t *p)
111{
112 (p->counter)--;
113 return !p->counter;
114}
115
116static inline void atomic_set(atomic_t *p, int v)
117{
118 p->counter=v;
119}
120
121static inline void atomic_inc(atomic_t *p)
122{
123 p->counter++;
124}
125
126static int atomic_read(atomic_t *p)
127{
128 return p->counter;
129}
c66d2821 130
bb07823d
PMF
131#define atomic_long_t atomic_t
132#define atomic_long_set atomic_set
133#define atomic_long_read atomic_read
134
09938485 135//#define __xg(x) ((volatile long *)(x))
bb07823d
PMF
136
137#define cmpxchg(ptr, o, n) \
138 ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \
139 (unsigned long)(n), sizeof(*(ptr))))
140
c1dea0b3
PMF
141//#define local_cmpxchg cmpxchg
142#define local_cmpxchg(l, o, n) (cmpxchg(&((l)->a.counter), (o), (n)))
143
bb07823d
PMF
144#define atomic_long_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
145
146
5f54827b
PMF
147/* LOCAL OPS */
148
c1dea0b3
PMF
149//typedef int local_t;
150typedef struct
151{
152 atomic_long_t a;
153} local_t;
154
bb07823d 155
c1dea0b3 156static inline void local_inc(local_t *l)
bb07823d 157{
c1dea0b3 158 (l->a.counter)++;
bb07823d
PMF
159}
160
c1dea0b3 161static inline void local_set(local_t *l, int v)
bb07823d 162{
c1dea0b3 163 l->a.counter = v;
bb07823d
PMF
164}
165
c1dea0b3 166static inline void local_add(int v, local_t *l)
bb07823d 167{
c1dea0b3 168 l->a.counter += v;
bb07823d
PMF
169}
170
c1dea0b3 171static int local_add_return(int v, local_t *l)
bb07823d 172{
c1dea0b3 173 return l->a.counter += v;
bb07823d
PMF
174}
175
c1dea0b3 176static inline int local_read(local_t *l)
bb07823d 177{
c1dea0b3 178 return l->a.counter;
bb07823d 179}
5f54827b
PMF
180
181
182/* ATTRIBUTES */
b6bf28ec 183
59b161cd 184#define ____cacheline_aligned
5f54827b
PMF
185#define __init
186#define __exit
c66d2821 187
b6bf28ec
PMF
188/* MATH */
189
190static 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
199static 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
215static __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
5f54827b
PMF
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)
bb07823d 234#define PAGE_MASK (PAGE_SIZE-1)
5f54827b
PMF
235
236
237
238
b6bf28ec
PMF
239/* ARRAYS */
240
241#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
242
243/* TRACE CLOCK */
244
98963de4
PMF
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
b6bf28ec
PMF
257static inline u64 trace_clock_read64(void)
258{
98963de4
PMF
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;
b6bf28ec
PMF
268}
269
98963de4 270static inline u64 trace_clock_frequency(void)
b6bf28ec 271{
98963de4 272 return 1000000LL;
b6bf28ec
PMF
273}
274
275static inline u32 trace_clock_freq_scale(void)
276{
98963de4 277 return 1;
b6bf28ec
PMF
278}
279
8d938dbd
PMF
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
ba6459ba
PMF
289#define smp_processor_id() (-1)
290
c66d2821 291#endif /* KERNELCOMPAT_H */
This page took 0.034814 seconds and 4 git commands to generate.