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