kernel.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef __TOOLS_LINUX_KERNEL_H
  2. #define __TOOLS_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. /*
  80. * This looks more complex than it should be. But we need to
  81. * get the type for the ~ right in round_down (it needs to be
  82. * as wide as the result!), and we want to evaluate the macro
  83. * arguments just once each.
  84. */
  85. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  86. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  87. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  88. #endif