kernel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
  9. #define __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 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. /*
  48. * Both need more care to handle endianness
  49. * (Don't use bitmap_copy_le() for now)
  50. */
  51. #define cpu_to_le64(x) (x)
  52. #define cpu_to_le32(x) (x)
  53. static inline int
  54. vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  55. {
  56. int i;
  57. ssize_t ssize = size;
  58. i = vsnprintf(buf, size, fmt, args);
  59. return (i >= ssize) ? (ssize - 1) : i;
  60. }
  61. static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
  62. {
  63. va_list args;
  64. ssize_t ssize = size;
  65. int i;
  66. va_start(args, fmt);
  67. i = vsnprintf(buf, size, fmt, args);
  68. va_end(args);
  69. return (i >= ssize) ? (ssize - 1) : i;
  70. }
  71. static inline unsigned long
  72. simple_strtoul(const char *nptr, char **endptr, int base)
  73. {
  74. return strtoul(nptr, endptr, base);
  75. }
  76. int eprintf(int level,
  77. const char *fmt, ...) __attribute__((format(printf, 2, 3)));
  78. #ifndef pr_fmt
  79. #define pr_fmt(fmt) fmt
  80. #endif
  81. #define pr_err(fmt, ...) \
  82. eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
  83. #define pr_warning(fmt, ...) \
  84. eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
  85. #define pr_info(fmt, ...) \
  86. eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
  87. #define pr_debug(fmt, ...) \
  88. eprintf(1, pr_fmt(fmt), ##__VA_ARGS__)
  89. #define pr_debugN(n, fmt, ...) \
  90. eprintf(n, pr_fmt(fmt), ##__VA_ARGS__)
  91. #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
  92. #define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__)
  93. #define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__)
  94. /*
  95. * This looks more complex than it should be. But we need to
  96. * get the type for the ~ right in round_down (it needs to be
  97. * as wide as the result!), and we want to evaluate the macro
  98. * arguments just once each.
  99. */
  100. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  101. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  102. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  103. #endif