kernel.h 2.3 KB

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