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