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