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