kernel.h 7.9 KB

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