kernel.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef __TOOLS_LINUX_KERNEL_H
  2. #define __TOOLS_LINUX_KERNEL_H
  3. #include <stdarg.h>
  4. #include <stddef.h>
  5. #include <assert.h>
  6. #include <linux/compiler.h>
  7. #ifndef UINT_MAX
  8. #define UINT_MAX (~0U)
  9. #endif
  10. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  11. #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
  12. #define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  13. #ifndef offsetof
  14. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  15. #endif
  16. #ifndef container_of
  17. /**
  18. * container_of - cast a member of a structure out to the containing structure
  19. * @ptr: the pointer to the member.
  20. * @type: the type of the container struct this is embedded in.
  21. * @member: the name of the member within the struct.
  22. *
  23. */
  24. #define container_of(ptr, type, member) ({ \
  25. const typeof(((type *)0)->member) * __mptr = (ptr); \
  26. (type *)((char *)__mptr - offsetof(type, member)); })
  27. #endif
  28. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  29. #ifndef max
  30. #define max(x, y) ({ \
  31. typeof(x) _max1 = (x); \
  32. typeof(y) _max2 = (y); \
  33. (void) (&_max1 == &_max2); \
  34. _max1 > _max2 ? _max1 : _max2; })
  35. #endif
  36. #ifndef min
  37. #define min(x, y) ({ \
  38. typeof(x) _min1 = (x); \
  39. typeof(y) _min2 = (y); \
  40. (void) (&_min1 == &_min2); \
  41. _min1 < _min2 ? _min1 : _min2; })
  42. #endif
  43. #ifndef roundup
  44. #define roundup(x, y) ( \
  45. { \
  46. const typeof(y) __y = y; \
  47. (((x) + (__y - 1)) / __y) * __y; \
  48. } \
  49. )
  50. #endif
  51. #ifndef BUG_ON
  52. #ifdef NDEBUG
  53. #define BUG_ON(cond) do { if (cond) {} } while (0)
  54. #else
  55. #define BUG_ON(cond) assert(!(cond))
  56. #endif
  57. #endif
  58. /*
  59. * Both need more care to handle endianness
  60. * (Don't use bitmap_copy_le() for now)
  61. */
  62. #define cpu_to_le64(x) (x)
  63. #define cpu_to_le32(x) (x)
  64. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  65. int scnprintf(char * buf, size_t size, const char * fmt, ...);
  66. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  67. /*
  68. * This looks more complex than it should be. But we need to
  69. * get the type for the ~ right in round_down (it needs to be
  70. * as wide as the result!), and we want to evaluate the macro
  71. * arguments just once each.
  72. */
  73. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  74. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  75. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  76. #endif