kernel.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef max
  26. #define max(x, y) ({ \
  27. typeof(x) _max1 = (x); \
  28. typeof(y) _max2 = (y); \
  29. (void) (&_max1 == &_max2); \
  30. _max1 > _max2 ? _max1 : _max2; })
  31. #endif
  32. #ifndef min
  33. #define min(x, y) ({ \
  34. typeof(x) _min1 = (x); \
  35. typeof(y) _min2 = (y); \
  36. (void) (&_min1 == &_min2); \
  37. _min1 < _min2 ? _min1 : _min2; })
  38. #endif
  39. #ifndef BUG_ON
  40. #define BUG_ON(cond) assert(!(cond))
  41. #endif
  42. /*
  43. * Both need more care to handle endianness
  44. * (Don't use bitmap_copy_le() for now)
  45. */
  46. #define cpu_to_le64(x) (x)
  47. #define cpu_to_le32(x) (x)
  48. static inline int
  49. vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  50. {
  51. int i;
  52. ssize_t ssize = size;
  53. i = vsnprintf(buf, size, fmt, args);
  54. return (i >= ssize) ? (ssize - 1) : i;
  55. }
  56. static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
  57. {
  58. va_list args;
  59. ssize_t ssize = size;
  60. int i;
  61. va_start(args, fmt);
  62. i = vsnprintf(buf, size, fmt, args);
  63. va_end(args);
  64. return (i >= ssize) ? (ssize - 1) : i;
  65. }
  66. static inline unsigned long
  67. simple_strtoul(const char *nptr, char **endptr, int base)
  68. {
  69. return strtoul(nptr, endptr, base);
  70. }
  71. #ifndef pr_fmt
  72. #define pr_fmt(fmt) fmt
  73. #endif
  74. #define pr_err(fmt, ...) \
  75. do { fprintf(stderr, pr_fmt(fmt), ##__VA_ARGS__); } while (0)
  76. #define pr_warning(fmt, ...) \
  77. do { fprintf(stderr, pr_fmt(fmt), ##__VA_ARGS__); } while (0)
  78. #define pr_info(fmt, ...) \
  79. do { fprintf(stderr, pr_fmt(fmt), ##__VA_ARGS__); } while (0)
  80. #define pr_debug(fmt, ...) \
  81. eprintf(1, pr_fmt(fmt), ##__VA_ARGS__)
  82. #define pr_debugN(n, fmt, ...) \
  83. eprintf(n, pr_fmt(fmt), ##__VA_ARGS__)
  84. #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
  85. #define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__)
  86. #define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__)
  87. #endif