| 1 | #ifndef KERNELCOMPAT_H |
| 2 | #define KERNELCOMPAT_H |
| 3 | |
| 4 | #include <kcompat.h> |
| 5 | |
| 6 | #include "compiler.h" |
| 7 | |
| 8 | #include <string.h> |
| 9 | #include <sys/time.h> |
| 10 | |
| 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 | |
| 16 | #define container_of(ptr, type, member) ({ \ |
| 17 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 18 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 19 | |
| 20 | #define KERN_DEBUG "" |
| 21 | #define KERN_NOTICE "" |
| 22 | #define KERN_INFO "" |
| 23 | #define KERN_ERR "" |
| 24 | #define KERN_ALERT "" |
| 25 | #define KERN_WARNING "" |
| 26 | |
| 27 | /* ERROR OPS */ |
| 28 | |
| 29 | #define MAX_ERRNO 4095 |
| 30 | |
| 31 | #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) |
| 32 | |
| 33 | static inline void *ERR_PTR(long error) |
| 34 | { |
| 35 | return (void *) error; |
| 36 | } |
| 37 | |
| 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 | |
| 49 | /* Min / Max */ |
| 50 | |
| 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 */ |
| 63 | |
| 64 | #include <pthread.h> |
| 65 | |
| 66 | #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER; |
| 67 | #define DECLARE_MUTEX(m) extern pthread_mutex_t (m); |
| 68 | |
| 69 | #define mutex_lock(m) pthread_mutex_lock(m) |
| 70 | |
| 71 | #define mutex_unlock(m) pthread_mutex_unlock(m) |
| 72 | |
| 73 | |
| 74 | /* MALLOCATION */ |
| 75 | |
| 76 | #include <stdlib.h> |
| 77 | |
| 78 | #define kmalloc(s, t) malloc(s) |
| 79 | #define kzalloc(s, t) zmalloc(s) |
| 80 | #define kfree(p) free((void *)p) |
| 81 | #define kstrdup(s, t) strdup(s) |
| 82 | |
| 83 | #define zmalloc(s) calloc(1, s) |
| 84 | |
| 85 | #define GFP_KERNEL |
| 86 | |
| 87 | /* PRINTK */ |
| 88 | |
| 89 | #include <stdio.h> |
| 90 | #define printk(fmt, args...) printf(fmt, ## args) |
| 91 | |
| 92 | |
| 93 | /* ATTRIBUTES */ |
| 94 | |
| 95 | #define ____cacheline_aligned |
| 96 | #define __init |
| 97 | #define __exit |
| 98 | |
| 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 | |
| 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) |
| 145 | #define PAGE_MASK (PAGE_SIZE-1) |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | /* ARRAYS */ |
| 151 | |
| 152 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) |
| 153 | |
| 154 | /* TRACE CLOCK */ |
| 155 | |
| 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 | |
| 168 | static inline u64 trace_clock_read64(void) |
| 169 | { |
| 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; |
| 179 | } |
| 180 | |
| 181 | static inline u64 trace_clock_frequency(void) |
| 182 | { |
| 183 | return 1000000LL; |
| 184 | } |
| 185 | |
| 186 | static inline u32 trace_clock_freq_scale(void) |
| 187 | { |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 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 | |
| 200 | #define smp_processor_id() (-1) |
| 201 | |
| 202 | #endif /* KERNELCOMPAT_H */ |