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