kernel.h 2.2 KB

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