kernel.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <linux/math.h>
  10. #include <linux/panic.h>
  11. #include <endian.h>
  12. #include <byteswap.h>
  13. #include <linux/container_of.h>
  14. #ifndef UINT_MAX
  15. #define UINT_MAX (~0U)
  16. #endif
  17. #define _RET_IP_ ((unsigned long)__builtin_return_address(0))
  18. #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
  19. #define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  20. #ifndef offsetof
  21. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  22. #endif
  23. #ifndef max
  24. #define max(x, y) ({ \
  25. typeof(x) _max1 = (x); \
  26. typeof(y) _max2 = (y); \
  27. (void) (&_max1 == &_max2); \
  28. _max1 > _max2 ? _max1 : _max2; })
  29. #endif
  30. #ifndef min
  31. #define min(x, y) ({ \
  32. typeof(x) _min1 = (x); \
  33. typeof(y) _min2 = (y); \
  34. (void) (&_min1 == &_min2); \
  35. _min1 < _min2 ? _min1 : _min2; })
  36. #endif
  37. #define max_t(type, x, y) max((type)x, (type)y)
  38. #define min_t(type, x, y) min((type)x, (type)y)
  39. #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
  40. #ifndef BUG_ON
  41. #ifdef NDEBUG
  42. #define BUG_ON(cond) do { if (cond) {} } while (0)
  43. #else
  44. #define BUG_ON(cond) assert(!(cond))
  45. #endif
  46. #endif
  47. #define BUG() BUG_ON(1)
  48. #if __BYTE_ORDER == __BIG_ENDIAN
  49. #define cpu_to_le16 bswap_16
  50. #define cpu_to_le32 bswap_32
  51. #define cpu_to_le64 bswap_64
  52. #define le16_to_cpu bswap_16
  53. #define le32_to_cpu bswap_32
  54. #define le64_to_cpu bswap_64
  55. #define cpu_to_be16
  56. #define cpu_to_be32
  57. #define cpu_to_be64
  58. #define be16_to_cpu
  59. #define be32_to_cpu
  60. #define be64_to_cpu
  61. #else
  62. #define cpu_to_le16
  63. #define cpu_to_le32
  64. #define cpu_to_le64
  65. #define le16_to_cpu
  66. #define le32_to_cpu
  67. #define le64_to_cpu
  68. #define cpu_to_be16 bswap_16
  69. #define cpu_to_be32 bswap_32
  70. #define cpu_to_be64 bswap_64
  71. #define be16_to_cpu bswap_16
  72. #define be32_to_cpu bswap_32
  73. #define be64_to_cpu bswap_64
  74. #endif
  75. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  76. int scnprintf(char * buf, size_t size, const char * fmt, ...);
  77. int scnprintf_pad(char * buf, size_t size, const char * fmt, ...);
  78. #ifndef ARRAY_SIZE
  79. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  80. #endif
  81. #define current_gfp_context(k) 0
  82. #define synchronize_rcu()
  83. #endif