4514b7986f0be327a0b3fdfc74d3d9d85731464a
[ust.git] / include / ust / kernelcompat.h
1 #ifndef KERNELCOMPAT_H
2 #define KERNELCOMPAT_H
3
4 #include <kcompat.h>
5
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
11 #define container_of(ptr, type, member) ({ \
12 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
13 (type *)( (char *)__mptr - offsetof(type,member) );})
14
15 /* ERROR OPS */
16 #define MAX_ERRNO 4095
17
18 #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
19
20 static inline void *ERR_PTR(long error)
21 {
22 return (void *) error;
23 }
24
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
36 /* Min / Max */
37
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 */
50
51 #include <pthread.h>
52
53 #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER;
54 #define DECLARE_MUTEX(m) extern pthread_mutex_t (m);
55
56 #define mutex_lock(m) pthread_mutex_lock(m)
57
58 #define mutex_unlock(m) pthread_mutex_unlock(m)
59
60
61 /* MALLOCATION */
62
63 #define zmalloc(s) calloc(1, s)
64
65 /* ATTRIBUTES */
66
67 /* FIXME: define this */
68 #define ____cacheline_aligned
69
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
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)
116 #define PAGE_MASK (~(PAGE_SIZE-1))
117
118
119
120
121 /* ARRAYS */
122
123 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
124
125 /* TRACE CLOCK */
126
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 */
144 static inline u64 trace_clock_read64(void)
145 {
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;
154 }
155 #endif
156
157 #include <sys/time.h>
158
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 }
171
172 static inline u64 trace_clock_frequency(void)
173 {
174 return 1000000LL;
175 }
176
177 static inline u32 trace_clock_freq_scale(void)
178 {
179 return 1;
180 }
181
182 #endif /* KERNELCOMPAT_H */
This page took 0.032005 seconds and 3 git commands to generate.