utils.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef KUBLK_UTILS_H
  3. #define KUBLK_UTILS_H
  4. #ifndef min
  5. #define min(a, b) ((a) < (b) ? (a) : (b))
  6. #endif
  7. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  8. #ifndef offsetof
  9. #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
  10. #endif
  11. #ifndef container_of
  12. #define container_of(ptr, type, member) ({ \
  13. unsigned long __mptr = (unsigned long)(ptr); \
  14. ((type *)(__mptr - offsetof(type, member))); })
  15. #endif
  16. #define round_up(val, rnd) \
  17. (((val) + ((rnd) - 1)) & ~((rnd) - 1))
  18. /* small sized & per-thread allocator */
  19. struct allocator {
  20. unsigned int size;
  21. cpu_set_t *set;
  22. };
  23. static inline int allocator_init(struct allocator *a, unsigned size)
  24. {
  25. a->set = CPU_ALLOC(size);
  26. a->size = size;
  27. if (a->set)
  28. return 0;
  29. return -ENOMEM;
  30. }
  31. static inline void allocator_deinit(struct allocator *a)
  32. {
  33. CPU_FREE(a->set);
  34. a->set = NULL;
  35. a->size = 0;
  36. }
  37. static inline int allocator_get(struct allocator *a)
  38. {
  39. int i;
  40. for (i = 0; i < a->size; i += 1) {
  41. size_t set_size = CPU_ALLOC_SIZE(a->size);
  42. if (!CPU_ISSET_S(i, set_size, a->set)) {
  43. CPU_SET_S(i, set_size, a->set);
  44. return i;
  45. }
  46. }
  47. return -1;
  48. }
  49. static inline void allocator_put(struct allocator *a, int i)
  50. {
  51. size_t set_size = CPU_ALLOC_SIZE(a->size);
  52. if (i >= 0 && i < a->size)
  53. CPU_CLR_S(i, set_size, a->set);
  54. }
  55. static inline int allocator_get_val(struct allocator *a, int i)
  56. {
  57. size_t set_size = CPU_ALLOC_SIZE(a->size);
  58. return CPU_ISSET_S(i, set_size, a->set);
  59. }
  60. static inline unsigned int ilog2(unsigned int x)
  61. {
  62. if (x == 0)
  63. return 0;
  64. return (sizeof(x) * 8 - 1) - __builtin_clz(x);
  65. }
  66. #define UBLK_DBG_DEV (1U << 0)
  67. #define UBLK_DBG_THREAD (1U << 1)
  68. #define UBLK_DBG_IO_CMD (1U << 2)
  69. #define UBLK_DBG_IO (1U << 3)
  70. #define UBLK_DBG_CTRL_CMD (1U << 4)
  71. #define UBLK_LOG (1U << 5)
  72. extern unsigned int ublk_dbg_mask;
  73. static inline void ublk_err(const char *fmt, ...)
  74. {
  75. va_list ap;
  76. va_start(ap, fmt);
  77. vfprintf(stderr, fmt, ap);
  78. va_end(ap);
  79. }
  80. static inline void ublk_log(const char *fmt, ...)
  81. {
  82. if (ublk_dbg_mask & UBLK_LOG) {
  83. va_list ap;
  84. va_start(ap, fmt);
  85. vfprintf(stdout, fmt, ap);
  86. va_end(ap);
  87. }
  88. }
  89. static inline void ublk_dbg(int level, const char *fmt, ...)
  90. {
  91. if (level & ublk_dbg_mask) {
  92. va_list ap;
  93. va_start(ap, fmt);
  94. vfprintf(stdout, fmt, ap);
  95. va_end(ap);
  96. }
  97. }
  98. #define ublk_assert(x) do { \
  99. if (!(x)) { \
  100. ublk_err("%s %d: assert!\n", __func__, __LINE__); \
  101. assert(x); \
  102. } \
  103. } while (0)
  104. #endif