kernel.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #define BUG_ON(cond) assert(!(cond))
  42. #endif
  43. /*
  44. * Both need more care to handle endianness
  45. * (Don't use bitmap_copy_le() for now)
  46. */
  47. #define cpu_to_le64(x) (x)
  48. #define cpu_to_le32(x) (x)
  49. static inline int
  50. vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  51. {
  52. int i;
  53. ssize_t ssize = size;
  54. i = vsnprintf(buf, size, fmt, args);
  55. return (i >= ssize) ? (ssize - 1) : i;
  56. }
  57. static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
  58. {
  59. va_list args;
  60. ssize_t ssize = size;
  61. int i;
  62. va_start(args, fmt);
  63. i = vsnprintf(buf, size, fmt, args);
  64. va_end(args);
  65. return (i >= ssize) ? (ssize - 1) : i;
  66. }
  67. static inline unsigned long
  68. simple_strtoul(const char *nptr, char **endptr, int base)
  69. {
  70. return strtoul(nptr, endptr, base);
  71. }
  72. int eprintf(int level,
  73. const char *fmt, ...) __attribute__((format(printf, 2, 3)));
  74. #ifndef pr_fmt
  75. #define pr_fmt(fmt) fmt
  76. #endif
  77. #define pr_err(fmt, ...) \
  78. eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
  79. #define pr_warning(fmt, ...) \
  80. eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
  81. #define pr_info(fmt, ...) \
  82. eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
  83. #define pr_debug(fmt, ...) \
  84. eprintf(1, pr_fmt(fmt), ##__VA_ARGS__)
  85. #define pr_debugN(n, fmt, ...) \
  86. eprintf(n, pr_fmt(fmt), ##__VA_ARGS__)
  87. #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
  88. #define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__)
  89. #define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__)
  90. #endif