kernel.h 7.0 KB

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