xref: /linux/tools/testing/selftests/ublk/utils.h (revision 8804d970fab45726b3c7cd7f240b31122aa94219)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef KUBLK_UTILS_H
3 #define KUBLK_UTILS_H
4 
5 #ifndef min
6 #define min(a, b) ((a) < (b) ? (a) : (b))
7 #endif
8 
9 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
10 
11 #ifndef offsetof
12 #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
13 #endif
14 
15 #ifndef container_of
16 #define container_of(ptr, type, member) ({                              \
17 	unsigned long __mptr = (unsigned long)(ptr);                    \
18 	((type *)(__mptr - offsetof(type, member))); })
19 #endif
20 
21 #define round_up(val, rnd) \
22 	(((val) + ((rnd) - 1)) & ~((rnd) - 1))
23 
ilog2(unsigned int x)24 static inline unsigned int ilog2(unsigned int x)
25 {
26 	if (x == 0)
27 		return 0;
28 	return (sizeof(x) * 8 - 1) - __builtin_clz(x);
29 }
30 
31 #define UBLK_DBG_DEV            (1U << 0)
32 #define UBLK_DBG_THREAD         (1U << 1)
33 #define UBLK_DBG_IO_CMD         (1U << 2)
34 #define UBLK_DBG_IO             (1U << 3)
35 #define UBLK_DBG_CTRL_CMD       (1U << 4)
36 #define UBLK_LOG                (1U << 5)
37 
38 extern unsigned int ublk_dbg_mask;
39 
ublk_err(const char * fmt,...)40 static inline void ublk_err(const char *fmt, ...)
41 {
42 	va_list ap;
43 
44 	va_start(ap, fmt);
45 	vfprintf(stderr, fmt, ap);
46 }
47 
ublk_log(const char * fmt,...)48 static inline void ublk_log(const char *fmt, ...)
49 {
50 	if (ublk_dbg_mask & UBLK_LOG) {
51 		va_list ap;
52 
53 		va_start(ap, fmt);
54 		vfprintf(stdout, fmt, ap);
55 	}
56 }
57 
ublk_dbg(int level,const char * fmt,...)58 static inline void ublk_dbg(int level, const char *fmt, ...)
59 {
60 	if (level & ublk_dbg_mask) {
61 		va_list ap;
62 
63 		va_start(ap, fmt);
64 		vfprintf(stdout, fmt, ap);
65 	}
66 }
67 
68 #endif
69