4b43b7e3de02b03ab61156e06719b4ffe9600531
[ust.git] / include / ust / kernelcompat.h
1 #ifndef KERNELCOMPAT_H
2 #define KERNELCOMPAT_H
3
4 #include <kcompat.h>
5
6 #include <string.h>
7 #include <sys/time.h>
8
9 /* FIXME: libkcompat must not define arch-specific local ops, as ust *must*
10 * fallback to the normal atomic ops. Fix things so we don't add them and
11 * break things accidentally.
12 */
13
14 #define container_of(ptr, type, member) ({ \
15 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
16 (type *)( (char *)__mptr - offsetof(type,member) );})
17
18 #define KERN_DEBUG ""
19 #define KERN_NOTICE ""
20 #define KERN_INFO ""
21 #define KERN_ERR ""
22 #define KERN_ALERT ""
23 #define KERN_WARNING ""
24
25 #define WARN_ON_ONCE(msg) WARN_ON(msg)
26
27 /* ERROR OPS */
28
29 #define MAX_ERRNO 4095
30
31 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
32
33 static inline void *ERR_PTR(long error)
34 {
35 return (void *) error;
36 }
37
38 static inline long PTR_ERR(const void *ptr)
39 {
40 return (long) ptr;
41 }
42
43 static inline long IS_ERR(const void *ptr)
44 {
45 return IS_ERR_VALUE((unsigned long)ptr);
46 }
47
48
49 /* Min / Max */
50
51 #define min_t(type, x, y) ({ \
52 type __min1 = (x); \
53 type __min2 = (y); \
54 __min1 < __min2 ? __min1: __min2; })
55
56 #define max_t(type, x, y) ({ \
57 type __max1 = (x); \
58 type __max2 = (y); \
59 __max1 > __max2 ? __max1: __max2; })
60
61
62 /* MUTEXES */
63
64 #include <pthread.h>
65
66 #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER;
67 #define DECLARE_MUTEX(m) extern pthread_mutex_t (m);
68
69 #define mutex_lock(m) pthread_mutex_lock(m)
70
71 #define mutex_unlock(m) pthread_mutex_unlock(m)
72
73
74 /* MALLOCATION */
75
76 #include <stdlib.h>
77
78 #define kmalloc(s, t) malloc(s)
79 #define kzalloc(s, t) zmalloc(s)
80 #define kfree(p) free((void *)p)
81 #define kstrdup(s, t) strdup(s)
82
83 #define zmalloc(s) calloc(1, s)
84
85 #define GFP_KERNEL
86
87 /* ATTRIBUTES */
88
89 #define ____cacheline_aligned
90
91 /* MATH */
92
93 static inline unsigned int hweight32(unsigned int w)
94 {
95 unsigned int res = w - ((w >> 1) & 0x55555555);
96 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
97 res = (res + (res >> 4)) & 0x0F0F0F0F;
98 res = res + (res >> 8);
99 return (res + (res >> 16)) & 0x000000FF;
100 }
101
102 static inline int fls(int x)
103 {
104 int r;
105 //ust// #ifdef CONFIG_X86_CMOV
106 asm("bsrl %1,%0\n\t"
107 "cmovzl %2,%0"
108 : "=&r" (r) : "rm" (x), "rm" (-1));
109 //ust// #else
110 //ust// asm("bsrl %1,%0\n\t"
111 //ust// "jnz 1f\n\t"
112 //ust// "movl $-1,%0\n"
113 //ust// "1:" : "=r" (r) : "rm" (x));
114 //ust// #endif
115 return r + 1;
116 }
117
118 static __inline__ int get_count_order(unsigned int count)
119 {
120 int order;
121
122 order = fls(count) - 1;
123 if (count & (count - 1))
124 order++;
125 return order;
126 }
127
128
129
130
131 #include <unistd.h>
132
133 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
134 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
135 #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
136 #define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
137 #define PAGE_MASK (~(PAGE_SIZE-1))
138
139
140
141
142 /* ARRAYS */
143
144 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
145
146 /* TRACE CLOCK */
147
148 /* There are two types of clocks that can be used.
149 - TSC based clock
150 - gettimeofday() clock
151
152 Microbenchmarks on Linux 2.6.30 on Core2 Duo 3GHz (functions are inlined):
153 Calls (100000000) to tsc(): 4004035641 cycles or 40 cycles/call
154 Calls (100000000) to gettimeofday(): 9723158352 cycles or 97 cycles/call
155
156 For merging traces with the kernel, a time source compatible with that of
157 the kernel is necessary.
158
159 */
160
161 #if 0
162 /* WARNING: Make sure to set frequency and scaling functions that will not
163 * result in lttv timestamps (sec.nsec) with seconds greater than 2**32-1.
164 */
165 static inline u64 trace_clock_read64(void)
166 {
167 uint32_t low;
168 uint32_t high;
169 uint64_t retval;
170 __asm__ volatile ("rdtsc\n" : "=a" (low), "=d" (high));
171
172 retval = high;
173 retval <<= 32;
174 return retval | low;
175 }
176 #endif
177
178 static inline u64 trace_clock_read64(void)
179 {
180 struct timeval tv;
181 u64 retval;
182
183 gettimeofday(&tv, NULL);
184 retval = tv.tv_sec;
185 retval *= 1000000;
186 retval += tv.tv_usec;
187
188 return retval;
189 }
190
191 static inline u64 trace_clock_frequency(void)
192 {
193 return 1000000LL;
194 }
195
196 static inline u32 trace_clock_freq_scale(void)
197 {
198 return 1;
199 }
200
201
202 /* PERCPU */
203
204 #define __get_cpu_var(x) x
205
206 #endif /* KERNELCOMPAT_H */
This page took 0.03231 seconds and 3 git commands to generate.