kernel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/compiler.h>
  8. #include <endian.h>
  9. #include <byteswap.h>
  10. #ifndef UINT_MAX
  11. #define UINT_MAX (~0U)
  12. #endif
  13. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  14. #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
  15. #define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  16. #ifndef offsetof
  17. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  18. #endif
  19. #ifndef container_of
  20. /**
  21. * container_of - cast a member of a structure out to the containing structure
  22. * @ptr: the pointer to the member.
  23. * @type: the type of the container struct this is embedded in.
  24. * @member: the name of the member within the struct.
  25. *
  26. */
  27. #define container_of(ptr, type, member) ({ \
  28. const typeof(((type *)0)->member) * __mptr = (ptr); \
  29. (type *)((char *)__mptr - offsetof(type, member)); })
  30. #endif
  31. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  32. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  33. #ifndef max
  34. #define max(x, y) ({ \
  35. typeof(x) _max1 = (x); \
  36. typeof(y) _max2 = (y); \
  37. (void) (&_max1 == &_max2); \
  38. _max1 > _max2 ? _max1 : _max2; })
  39. #endif
  40. #ifndef min
  41. #define min(x, y) ({ \
  42. typeof(x) _min1 = (x); \
  43. typeof(y) _min2 = (y); \
  44. (void) (&_min1 == &_min2); \
  45. _min1 < _min2 ? _min1 : _min2; })
  46. #endif
  47. #ifndef roundup
  48. #define roundup(x, y) ( \
  49. { \
  50. const typeof(y) __y = y; \
  51. (((x) + (__y - 1)) / __y) * __y; \
  52. } \
  53. )
  54. #endif
  55. #ifndef BUG_ON
  56. #ifdef NDEBUG
  57. #define BUG_ON(cond) do { if (cond) {} } while (0)
  58. #else
  59. #define BUG_ON(cond) assert(!(cond))
  60. #endif
  61. #endif
  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. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  92. /*
  93. * This looks more complex than it should be. But we need to
  94. * get the type for the ~ right in round_down (it needs to be
  95. * as wide as the result!), and we want to evaluate the macro
  96. * arguments just once each.
  97. */
  98. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  99. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  100. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  101. #define current_gfp_context(k) 0
  102. #define synchronize_sched()
  103. #endif