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