kernel.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef PERF_LINUX_KERNEL_H_
  2. #define PERF_LINUX_KERNEL_H_
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  8. #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
  9. #define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  10. #ifndef offsetof
  11. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  12. #endif
  13. #ifndef container_of
  14. /**
  15. * container_of - cast a member of a structure out to the containing structure
  16. * @ptr: the pointer to the member.
  17. * @type: the type of the container struct this is embedded in.
  18. * @member: the name of the member within the struct.
  19. *
  20. */
  21. #define container_of(ptr, type, member) ({ \
  22. const typeof(((type *)0)->member) * __mptr = (ptr); \
  23. (type *)((char *)__mptr - offsetof(type, member)); })
  24. #endif
  25. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  26. #ifndef max
  27. #define max(x, y) ({ \
  28. typeof(x) _max1 = (x); \
  29. typeof(y) _max2 = (y); \
  30. (void) (&_max1 == &_max2); \
  31. _max1 > _max2 ? _max1 : _max2; })
  32. #endif
  33. #ifndef min
  34. #define min(x, y) ({ \
  35. typeof(x) _min1 = (x); \
  36. typeof(y) _min2 = (y); \
  37. (void) (&_min1 == &_min2); \
  38. _min1 < _min2 ? _min1 : _min2; })
  39. #endif
  40. #ifndef roundup
  41. #define roundup(x, y) ( \
  42. { \
  43. const typeof(y) __y = y; \
  44. (((x) + (__y - 1)) / __y) * __y; \
  45. } \
  46. )
  47. #endif
  48. #ifndef BUG_ON
  49. #ifdef NDEBUG
  50. #define BUG_ON(cond) do { if (cond) {} } while (0)
  51. #else
  52. #define BUG_ON(cond) assert(!(cond))
  53. #endif
  54. #endif
  55. /*
  56. * Both need more care to handle endianness
  57. * (Don't use bitmap_copy_le() for now)
  58. */
  59. #define cpu_to_le64(x) (x)
  60. #define cpu_to_le32(x) (x)
  61. static inline int
  62. vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  63. {
  64. int i;
  65. ssize_t ssize = size;
  66. i = vsnprintf(buf, size, fmt, args);
  67. return (i >= ssize) ? (ssize - 1) : i;
  68. }
  69. static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
  70. {
  71. va_list args;
  72. ssize_t ssize = size;
  73. int i;
  74. va_start(args, fmt);
  75. i = vsnprintf(buf, size, fmt, args);
  76. va_end(args);
  77. return (i >= ssize) ? (ssize - 1) : i;
  78. }
  79. static inline unsigned long
  80. simple_strtoul(const char *nptr, char **endptr, int base)
  81. {
  82. return strtoul(nptr, endptr, base);
  83. }
  84. int eprintf(int level,
  85. const char *fmt, ...) __attribute__((format(printf, 2, 3)));
  86. #ifndef pr_fmt
  87. #define pr_fmt(fmt) fmt
  88. #endif
  89. #define pr_err(fmt, ...) \
  90. eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
  91. #define pr_warning(fmt, ...) \
  92. eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
  93. #define pr_info(fmt, ...) \
  94. eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
  95. #define pr_debug(fmt, ...) \
  96. eprintf(1, pr_fmt(fmt), ##__VA_ARGS__)
  97. #define pr_debugN(n, fmt, ...) \
  98. eprintf(n, pr_fmt(fmt), ##__VA_ARGS__)
  99. #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
  100. #define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__)
  101. #define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__)
  102. /*
  103. * This looks more complex than it should be. But we need to
  104. * get the type for the ~ right in round_down (it needs to be
  105. * as wide as the result!), and we want to evaluate the macro
  106. * arguments just once each.
  107. */
  108. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  109. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  110. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  111. #endif