kernel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __TOOLS_LINUX_KERNEL_H
  3. #define __TOOLS_LINUX_KERNEL_H
  4. #include <stdarg.h>
  5. #include <stddef.h>
  6. #include <assert.h>
  7. #include <linux/build_bug.h>
  8. #include <linux/compiler.h>
  9. #include <endian.h>
  10. #include <byteswap.h>
  11. #ifndef UINT_MAX
  12. #define UINT_MAX (~0U)
  13. #endif
  14. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  15. #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
  16. #define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  17. #ifndef offsetof
  18. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  19. #endif
  20. #ifndef container_of
  21. /**
  22. * container_of - cast a member of a structure out to the containing structure
  23. * @ptr: the pointer to the member.
  24. * @type: the type of the container struct this is embedded in.
  25. * @member: the name of the member within the struct.
  26. *
  27. */
  28. #define container_of(ptr, type, member) ({ \
  29. const typeof(((type *)0)->member) * __mptr = (ptr); \
  30. (type *)((char *)__mptr - offsetof(type, member)); })
  31. #endif
  32. #ifndef max
  33. #define max(x, y) ({ \
  34. typeof(x) _max1 = (x); \
  35. typeof(y) _max2 = (y); \
  36. (void) (&_max1 == &_max2); \
  37. _max1 > _max2 ? _max1 : _max2; })
  38. #endif
  39. #ifndef min
  40. #define min(x, y) ({ \
  41. typeof(x) _min1 = (x); \
  42. typeof(y) _min2 = (y); \
  43. (void) (&_min1 == &_min2); \
  44. _min1 < _min2 ? _min1 : _min2; })
  45. #endif
  46. #ifndef roundup
  47. #define roundup(x, y) ( \
  48. { \
  49. const typeof(y) __y = y; \
  50. (((x) + (__y - 1)) / __y) * __y; \
  51. } \
  52. )
  53. #endif
  54. #ifndef BUG_ON
  55. #ifdef NDEBUG
  56. #define BUG_ON(cond) do { if (cond) {} } while (0)
  57. #else
  58. #define BUG_ON(cond) assert(!(cond))
  59. #endif
  60. #endif
  61. #define BUG() BUG_ON(1)
  62. #if __BYTE_ORDER == __BIG_ENDIAN
  63. #define cpu_to_le16 bswap_16
  64. #define cpu_to_le32 bswap_32
  65. #define cpu_to_le64 bswap_64
  66. #define le16_to_cpu bswap_16
  67. #define le32_to_cpu bswap_32
  68. #define le64_to_cpu bswap_64
  69. #define cpu_to_be16
  70. #define cpu_to_be32
  71. #define cpu_to_be64
  72. #define be16_to_cpu
  73. #define be32_to_cpu
  74. #define be64_to_cpu
  75. #else
  76. #define cpu_to_le16
  77. #define cpu_to_le32
  78. #define cpu_to_le64
  79. #define le16_to_cpu
  80. #define le32_to_cpu
  81. #define le64_to_cpu
  82. #define cpu_to_be16 bswap_16
  83. #define cpu_to_be32 bswap_32
  84. #define cpu_to_be64 bswap_64
  85. #define be16_to_cpu bswap_16
  86. #define be32_to_cpu bswap_32
  87. #define be64_to_cpu bswap_64
  88. #endif
  89. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  90. int scnprintf(char * buf, size_t size, const char * fmt, ...);
  91. int scnprintf_pad(char * buf, size_t size, const char * fmt, ...);
  92. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  93. /*
  94. * This looks more complex than it should be. But we need to
  95. * get the type for the ~ right in round_down (it needs to be
  96. * as wide as the result!), and we want to evaluate the macro
  97. * arguments just once each.
  98. */
  99. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  100. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  101. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  102. #define current_gfp_context(k) 0
  103. #define synchronize_rcu()
  104. #endif