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 | |
e17571a5 PMF |
25 | #define WARN_ON_ONCE(msg) WARN_ON(msg) |
26 | ||
59b161cd PMF |
27 | /* ERROR OPS */ |
28 | ||
29 | #define MAX_ERRNO 4095 | |
30 | ||
31 | #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) | |
32 | ||
c66d2821 PMF |
33 | static inline void *ERR_PTR(long error) |
34 | { | |
59b161cd | 35 | return (void *) error; |
c66d2821 PMF |
36 | } |
37 | ||
59b161cd PMF |
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 | ||
981e27d9 | 49 | /* Min / Max */ |
c66d2821 | 50 | |
b6bf28ec PMF |
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 */ | |
c66d2821 PMF |
63 | |
64 | #include <pthread.h> | |
65 | ||
66 | #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER; | |
ba6459ba | 67 | #define DECLARE_MUTEX(m) extern pthread_mutex_t (m); |
c66d2821 PMF |
68 | |
69 | #define mutex_lock(m) pthread_mutex_lock(m) | |
70 | ||
71 | #define mutex_unlock(m) pthread_mutex_unlock(m) | |
72 | ||
bb07823d | 73 | |
b6bf28ec | 74 | /* MALLOCATION */ |
c66d2821 | 75 | |
ba6459ba PMF |
76 | #define zmalloc(s) calloc(1, s) |
77 | ||
5f54827b | 78 | /* ATTRIBUTES */ |
b6bf28ec | 79 | |
59b161cd | 80 | #define ____cacheline_aligned |
c66d2821 | 81 | |
b6bf28ec PMF |
82 | /* MATH */ |
83 | ||
84 | static inline unsigned int hweight32(unsigned int w) | |
85 | { | |
86 | unsigned int res = w - ((w >> 1) & 0x55555555); | |
87 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); | |
88 | res = (res + (res >> 4)) & 0x0F0F0F0F; | |
89 | res = res + (res >> 8); | |
90 | return (res + (res >> 16)) & 0x000000FF; | |
91 | } | |
92 | ||
93 | static inline int fls(int x) | |
94 | { | |
95 | int r; | |
96 | //ust// #ifdef CONFIG_X86_CMOV | |
97 | asm("bsrl %1,%0\n\t" | |
98 | "cmovzl %2,%0" | |
99 | : "=&r" (r) : "rm" (x), "rm" (-1)); | |
100 | //ust// #else | |
101 | //ust// asm("bsrl %1,%0\n\t" | |
102 | //ust// "jnz 1f\n\t" | |
103 | //ust// "movl $-1,%0\n" | |
104 | //ust// "1:" : "=r" (r) : "rm" (x)); | |
105 | //ust// #endif | |
106 | return r + 1; | |
107 | } | |
108 | ||
109 | static __inline__ int get_count_order(unsigned int count) | |
110 | { | |
111 | int order; | |
112 | ||
113 | order = fls(count) - 1; | |
114 | if (count & (count - 1)) | |
115 | order++; | |
116 | return order; | |
117 | } | |
118 | ||
119 | ||
5f54827b PMF |
120 | |
121 | ||
122 | #include <unistd.h> | |
123 | ||
124 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) | |
125 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) | |
126 | #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE) | |
127 | #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) | |
d6355fb2 | 128 | #define PAGE_MASK (~(PAGE_SIZE-1)) |
5f54827b PMF |
129 | |
130 | ||
131 | ||
132 | ||
b6bf28ec PMF |
133 | /* ARRAYS */ |
134 | ||
135 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | |
136 | ||
137 | /* TRACE CLOCK */ | |
138 | ||
79cb2536 PMF |
139 | /* There are two types of clocks that can be used. |
140 | - TSC based clock | |
141 | - gettimeofday() clock | |
142 | ||
143 | Microbenchmarks on Linux 2.6.30 on Core2 Duo 3GHz (functions are inlined): | |
144 | Calls (100000000) to tsc(): 4004035641 cycles or 40 cycles/call | |
145 | Calls (100000000) to gettimeofday(): 9723158352 cycles or 97 cycles/call | |
146 | ||
147 | For merging traces with the kernel, a time source compatible with that of | |
148 | the kernel is necessary. | |
149 | ||
150 | */ | |
151 | ||
152 | #if 0 | |
153 | /* WARNING: Make sure to set frequency and scaling functions that will not | |
154 | * result in lttv timestamps (sec.nsec) with seconds greater than 2**32-1. | |
155 | */ | |
b6bf28ec PMF |
156 | static inline u64 trace_clock_read64(void) |
157 | { | |
5f004661 PMF |
158 | uint32_t low; |
159 | uint32_t high; | |
160 | uint64_t retval; | |
161 | __asm__ volatile ("rdtsc\n" : "=a" (low), "=d" (high)); | |
162 | ||
163 | retval = high; | |
164 | retval <<= 32; | |
165 | return retval | low; | |
b6bf28ec | 166 | } |
79cb2536 | 167 | #endif |
b6bf28ec | 168 | |
08230db7 PMF |
169 | static inline u64 trace_clock_read64(void) |
170 | { | |
171 | struct timeval tv; | |
172 | u64 retval; | |
173 | ||
174 | gettimeofday(&tv, NULL); | |
175 | retval = tv.tv_sec; | |
176 | retval *= 1000000; | |
177 | retval += tv.tv_usec; | |
178 | ||
179 | return retval; | |
180 | } | |
5f004661 | 181 | |
98963de4 | 182 | static inline u64 trace_clock_frequency(void) |
b6bf28ec | 183 | { |
98963de4 | 184 | return 1000000LL; |
b6bf28ec PMF |
185 | } |
186 | ||
187 | static inline u32 trace_clock_freq_scale(void) | |
188 | { | |
98963de4 | 189 | return 1; |
b6bf28ec PMF |
190 | } |
191 | ||
8d938dbd | 192 | |
b73a4c47 PMF |
193 | /* PERCPU */ |
194 | ||
195 | #define __get_cpu_var(x) x | |
196 | ||
c66d2821 | 197 | #endif /* KERNELCOMPAT_H */ |