Commit | Line | Data |
---|---|---|
c66d2821 PMF |
1 | #ifndef KERNELCOMPAT_H |
2 | #define KERNELCOMPAT_H | |
3 | ||
769d0157 | 4 | #include <compiler.h> |
1ae7f074 PMF |
5 | #include <kcompat.h> |
6 | ||
c66d2821 | 7 | #include <string.h> |
0b0cd937 | 8 | #include <sys/time.h> |
c66d2821 | 9 | |
981e27d9 PMF |
10 | /* FIXME: libkcompat must not define arch-specific local ops, as ust *must* |
11 | * fallback to the normal atomic ops. Fix things so we don't add them and | |
12 | * break things accidentally. | |
13 | */ | |
14 | ||
c66d2821 PMF |
15 | #define container_of(ptr, type, member) ({ \ |
16 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | |
17 | (type *)( (char *)__mptr - offsetof(type,member) );}) | |
18 | ||
b6bf28ec PMF |
19 | #define KERN_DEBUG "" |
20 | #define KERN_NOTICE "" | |
21 | #define KERN_INFO "" | |
22 | #define KERN_ERR "" | |
23 | #define KERN_ALERT "" | |
bb07823d | 24 | #define KERN_WARNING "" |
c66d2821 | 25 | |
59b161cd PMF |
26 | /* ERROR OPS */ |
27 | ||
28 | #define MAX_ERRNO 4095 | |
29 | ||
30 | #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) | |
31 | ||
c66d2821 PMF |
32 | static inline void *ERR_PTR(long error) |
33 | { | |
59b161cd | 34 | return (void *) error; |
c66d2821 PMF |
35 | } |
36 | ||
59b161cd PMF |
37 | static inline long PTR_ERR(const void *ptr) |
38 | { | |
39 | return (long) ptr; | |
40 | } | |
41 | ||
42 | static inline long IS_ERR(const void *ptr) | |
43 | { | |
44 | return IS_ERR_VALUE((unsigned long)ptr); | |
45 | } | |
46 | ||
47 | ||
981e27d9 | 48 | /* Min / Max */ |
c66d2821 | 49 | |
b6bf28ec PMF |
50 | #define min_t(type, x, y) ({ \ |
51 | type __min1 = (x); \ | |
52 | type __min2 = (y); \ | |
53 | __min1 < __min2 ? __min1: __min2; }) | |
54 | ||
55 | #define max_t(type, x, y) ({ \ | |
56 | type __max1 = (x); \ | |
57 | type __max2 = (y); \ | |
58 | __max1 > __max2 ? __max1: __max2; }) | |
59 | ||
60 | ||
61 | /* MUTEXES */ | |
c66d2821 PMF |
62 | |
63 | #include <pthread.h> | |
64 | ||
65 | #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER; | |
ba6459ba | 66 | #define DECLARE_MUTEX(m) extern pthread_mutex_t (m); |
c66d2821 PMF |
67 | |
68 | #define mutex_lock(m) pthread_mutex_lock(m) | |
69 | ||
70 | #define mutex_unlock(m) pthread_mutex_unlock(m) | |
71 | ||
bb07823d | 72 | |
b6bf28ec | 73 | /* MALLOCATION */ |
c66d2821 PMF |
74 | |
75 | #include <stdlib.h> | |
76 | ||
77 | #define kmalloc(s, t) malloc(s) | |
ba6459ba | 78 | #define kzalloc(s, t) zmalloc(s) |
c66d2821 PMF |
79 | #define kfree(p) free((void *)p) |
80 | #define kstrdup(s, t) strdup(s) | |
81 | ||
ba6459ba PMF |
82 | #define zmalloc(s) calloc(1, s) |
83 | ||
bb07823d PMF |
84 | #define GFP_KERNEL |
85 | ||
b6bf28ec | 86 | /* PRINTK */ |
c66d2821 PMF |
87 | |
88 | #include <stdio.h> | |
89 | #define printk(fmt, args...) printf(fmt, ## args) | |
90 | ||
5f54827b PMF |
91 | |
92 | /* ATTRIBUTES */ | |
b6bf28ec | 93 | |
59b161cd | 94 | #define ____cacheline_aligned |
5f54827b PMF |
95 | #define __init |
96 | #define __exit | |
c66d2821 | 97 | |
b6bf28ec PMF |
98 | /* MATH */ |
99 | ||
100 | static inline unsigned int hweight32(unsigned int w) | |
101 | { | |
102 | unsigned int res = w - ((w >> 1) & 0x55555555); | |
103 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); | |
104 | res = (res + (res >> 4)) & 0x0F0F0F0F; | |
105 | res = res + (res >> 8); | |
106 | return (res + (res >> 16)) & 0x000000FF; | |
107 | } | |
108 | ||
109 | static inline int fls(int x) | |
110 | { | |
111 | int r; | |
112 | //ust// #ifdef CONFIG_X86_CMOV | |
113 | asm("bsrl %1,%0\n\t" | |
114 | "cmovzl %2,%0" | |
115 | : "=&r" (r) : "rm" (x), "rm" (-1)); | |
116 | //ust// #else | |
117 | //ust// asm("bsrl %1,%0\n\t" | |
118 | //ust// "jnz 1f\n\t" | |
119 | //ust// "movl $-1,%0\n" | |
120 | //ust// "1:" : "=r" (r) : "rm" (x)); | |
121 | //ust// #endif | |
122 | return r + 1; | |
123 | } | |
124 | ||
125 | static __inline__ int get_count_order(unsigned int count) | |
126 | { | |
127 | int order; | |
128 | ||
129 | order = fls(count) - 1; | |
130 | if (count & (count - 1)) | |
131 | order++; | |
132 | return order; | |
133 | } | |
134 | ||
135 | ||
5f54827b PMF |
136 | |
137 | ||
138 | #include <unistd.h> | |
139 | ||
140 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) | |
141 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) | |
142 | #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE) | |
143 | #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) | |
bb07823d | 144 | #define PAGE_MASK (PAGE_SIZE-1) |
5f54827b PMF |
145 | |
146 | ||
147 | ||
148 | ||
b6bf28ec PMF |
149 | /* ARRAYS */ |
150 | ||
151 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | |
152 | ||
153 | /* TRACE CLOCK */ | |
154 | ||
98963de4 PMF |
155 | //ust// static inline u64 trace_clock_read64(void) |
156 | //ust// { | |
157 | //ust// uint32_t low; | |
158 | //ust// uint32_t high; | |
159 | //ust// uint64_t retval; | |
160 | //ust// __asm__ volatile ("rdtsc\n" : "=a" (low), "=d" (high)); | |
161 | //ust// | |
162 | //ust// retval = high; | |
163 | //ust// retval <<= 32; | |
164 | //ust// return retval | low; | |
165 | //ust// } | |
166 | ||
b6bf28ec PMF |
167 | static inline u64 trace_clock_read64(void) |
168 | { | |
98963de4 PMF |
169 | struct timeval tv; |
170 | u64 retval; | |
171 | ||
172 | gettimeofday(&tv, NULL); | |
173 | retval = tv.tv_sec; | |
174 | retval *= 1000000; | |
175 | retval += tv.tv_usec; | |
176 | ||
177 | return retval; | |
b6bf28ec PMF |
178 | } |
179 | ||
98963de4 | 180 | static inline u64 trace_clock_frequency(void) |
b6bf28ec | 181 | { |
98963de4 | 182 | return 1000000LL; |
b6bf28ec PMF |
183 | } |
184 | ||
185 | static inline u32 trace_clock_freq_scale(void) | |
186 | { | |
98963de4 | 187 | return 1; |
b6bf28ec PMF |
188 | } |
189 | ||
8d938dbd | 190 | |
c66d2821 | 191 | #endif /* KERNELCOMPAT_H */ |