1 #ifndef _LINUX_KERNEL_H 2 #define _LINUX_KERNEL_H 3 4 #include <linux/types.h> 5 #include <linux/printk.h> /* for printf/pr_* utilities */ 6 #include <limits.h> 7 8 #define STACK_MAGIC 0xdeadbeef 9 10 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 11 12 #define ALIGN(x,a) __ALIGN_MASK((x),(typeof(x))(a)-1) 13 #define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a)) 14 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) 15 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 16 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 17 18 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 19 20 /* 21 * This looks more complex than it should be. But we need to 22 * get the type for the ~ right in round_down (it needs to be 23 * as wide as the result!), and we want to evaluate the macro 24 * arguments just once each. 25 */ 26 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 27 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 28 #define round_down(x, y) ((x) & ~__round_mask(x, y)) 29 30 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 31 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 32 33 #define DIV_ROUND_DOWN_ULL(ll, d) \ 34 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) 35 36 #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d)) 37 38 #define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 39 40 #if BITS_PER_LONG == 32 41 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) 42 #else 43 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) 44 #endif 45 46 /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */ 47 #define roundup(x, y) ( \ 48 { \ 49 const typeof(y) __y = y; \ 50 (((x) + (__y - 1)) / __y) * __y; \ 51 } \ 52 ) 53 #define rounddown(x, y) ( \ 54 { \ 55 typeof(x) __x = (x); \ 56 __x - (__x % (y)); \ 57 } \ 58 ) 59 60 /* 61 * Divide positive or negative dividend by positive divisor and round 62 * to closest integer. Result is undefined for negative divisors and 63 * for negative dividends if the divisor variable type is unsigned. 64 */ 65 #define DIV_ROUND_CLOSEST(x, divisor)( \ 66 { \ 67 typeof(x) __x = x; \ 68 typeof(divisor) __d = divisor; \ 69 (((typeof(x))-1) > 0 || \ 70 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \ 71 (((__x) + ((__d) / 2)) / (__d)) : \ 72 (((__x) - ((__d) / 2)) / (__d)); \ 73 } \ 74 ) 75 /* 76 * Same as above but for u64 dividends. divisor must be a 32-bit 77 * number. 78 */ 79 #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \ 80 { \ 81 typeof(divisor) __d = divisor; \ 82 unsigned long long _tmp = (x) + (__d) / 2; \ 83 do_div(_tmp, __d); \ 84 _tmp; \ 85 } \ 86 ) 87 88 /* 89 * Multiplies an integer by a fraction, while avoiding unnecessary 90 * overflow or loss of precision. 91 */ 92 #define mult_frac(x, numer, denom)( \ 93 { \ 94 typeof(x) quot = (x) / (denom); \ 95 typeof(x) rem = (x) % (denom); \ 96 (quot * (numer)) + ((rem * (numer)) / (denom)); \ 97 } \ 98 ) 99 100 /** 101 * upper_32_bits - return bits 32-63 of a number 102 * @n: the number we're accessing 103 * 104 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 105 * the "right shift count >= width of type" warning when that quantity is 106 * 32-bits. 107 */ 108 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 109 110 /** 111 * lower_32_bits - return bits 0-31 of a number 112 * @n: the number we're accessing 113 */ 114 #define lower_32_bits(n) ((u32)(n)) 115 116 /* 117 * abs() handles unsigned and signed longs, ints, shorts and chars. For all 118 * input types abs() returns a signed long. 119 * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64() 120 * for those. 121 */ 122 #define abs(x) ({ \ 123 long ret; \ 124 if (sizeof(x) == sizeof(long)) { \ 125 long __x = (x); \ 126 ret = (__x < 0) ? -__x : __x; \ 127 } else { \ 128 int __x = (x); \ 129 ret = (__x < 0) ? -__x : __x; \ 130 } \ 131 ret; \ 132 }) 133 134 #define abs64(x) ({ \ 135 s64 __x = (x); \ 136 (__x < 0) ? -__x : __x; \ 137 }) 138 139 /* 140 * min()/max()/clamp() macros that also do 141 * strict type-checking.. See the 142 * "unnecessary" pointer comparison. 143 */ 144 #define min(x, y) ({ \ 145 typeof(x) _min1 = (x); \ 146 typeof(y) _min2 = (y); \ 147 (void) (&_min1 == &_min2); \ 148 _min1 < _min2 ? _min1 : _min2; }) 149 150 #define max(x, y) ({ \ 151 typeof(x) _max1 = (x); \ 152 typeof(y) _max2 = (y); \ 153 (void) (&_max1 == &_max2); \ 154 _max1 > _max2 ? _max1 : _max2; }) 155 156 #define min3(x, y, z) min((typeof(x))min(x, y), z) 157 #define max3(x, y, z) max((typeof(x))max(x, y), z) 158 159 /** 160 * min_not_zero - return the minimum that is _not_ zero, unless both are zero 161 * @x: value1 162 * @y: value2 163 */ 164 #define min_not_zero(x, y) ({ \ 165 typeof(x) __x = (x); \ 166 typeof(y) __y = (y); \ 167 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) 168 169 /** 170 * clamp - return a value clamped to a given range with strict typechecking 171 * @val: current value 172 * @lo: lowest allowable value 173 * @hi: highest allowable value 174 * 175 * This macro does strict typechecking of lo/hi to make sure they are of the 176 * same type as val. See the unnecessary pointer comparisons. 177 */ 178 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) 179 180 /* 181 * ..and if you can't take the strict 182 * types, you can specify one yourself. 183 * 184 * Or not use min/max/clamp at all, of course. 185 */ 186 #define min_t(type, x, y) ({ \ 187 type __min1 = (x); \ 188 type __min2 = (y); \ 189 __min1 < __min2 ? __min1: __min2; }) 190 191 #define max_t(type, x, y) ({ \ 192 type __max1 = (x); \ 193 type __max2 = (y); \ 194 __max1 > __max2 ? __max1: __max2; }) 195 196 /** 197 * clamp_t - return a value clamped to a given range using a given type 198 * @type: the type of variable to use 199 * @val: current value 200 * @lo: minimum allowable value 201 * @hi: maximum allowable value 202 * 203 * This macro does no typechecking and uses temporary variables of type 204 * 'type' to make all the comparisons. 205 */ 206 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) 207 208 /** 209 * clamp_val - return a value clamped to a given range using val's type 210 * @val: current value 211 * @lo: minimum allowable value 212 * @hi: maximum allowable value 213 * 214 * This macro does no typechecking and uses temporary variables of whatever 215 * type the input argument 'val' is. This is useful when val is an unsigned 216 * type and min and max are literals that will otherwise be assigned a signed 217 * integer type. 218 */ 219 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 220 221 /* 222 * swap - swap value of @a and @b 223 */ 224 #define swap(a, b) \ 225 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 226 227 /** 228 * container_of - cast a member of a structure out to the containing structure 229 * @ptr: the pointer to the member. 230 * @type: the type of the container struct this is embedded in. 231 * @member: the name of the member within the struct. 232 * 233 */ 234 #define container_of(ptr, type, member) ({ \ 235 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 236 (type *)( (char *)__mptr - offsetof(type,member) );}) 237 238 /* 239 * check_member() - Check the offset of a structure member 240 * 241 * @structure: Name of structure (e.g. global_data) 242 * @member: Name of member (e.g. baudrate) 243 * @offset: Expected offset in bytes 244 */ 245 #define check_member(structure, member, offset) _Static_assert( \ 246 offsetof(struct structure, member) == (offset), \ 247 "`struct " #structure "` offset for `" #member "` is not " #offset) 248 249 #define __find_closest(x, a, as, op) \ 250 ({ \ 251 typeof(as) __fc_i, __fc_as = (as) - 1; \ 252 typeof(x) __fc_x = (x); \ 253 typeof(*a) const *__fc_a = (a); \ 254 for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) { \ 255 if (__fc_x op DIV_ROUND_CLOSEST(__fc_a[__fc_i] + \ 256 __fc_a[__fc_i + 1], 2)) \ 257 break; \ 258 } \ 259 (__fc_i); \ 260 }) 261 262 /** 263 * find_closest - locate the closest element in a sorted array 264 * @x: The reference value. 265 * @a: The array in which to look for the closest element. Must be sorted 266 * in ascending order. 267 * @as: Size of 'a'. 268 * 269 * Returns the index of the element closest to 'x'. 270 */ 271 #define find_closest(x, a, as) __find_closest(x, a, as, <=) 272 273 #endif 274