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