]> git.sesse.net Git - bcachefs-tools-debian/blob - c_src/include/linux/bug.h
1a10f7e66144cb28db87a10f17df9dd7ec1ffa12
[bcachefs-tools-debian] / c_src / include / linux / bug.h
1 #ifndef __TOOLS_LINUX_BUG_H
2 #define __TOOLS_LINUX_BUG_H
3
4 #include <assert.h>
5 #include <stdio.h>
6 #include <linux/compiler.h>
7
8 #ifdef CONFIG_VALGRIND
9 #include <valgrind/memcheck.h>
10
11 #define DEBUG_MEMORY_FREED(p, len) VALGRIND_MAKE_MEM_UNDEFINED(p, len)
12 #endif
13
14 #define BUILD_BUG_ON_NOT_POWER_OF_2(n)                  \
15         BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
16 #define BUILD_BUG_ON_ZERO(e)    (sizeof(struct { int:-!!(e); }))
17 #define BUILD_BUG_ON_NULL(e)    ((void *)sizeof(struct { int:-!!(e); }))
18
19 #define BUILD_BUG_ON(cond)      ((void)sizeof(char[1 - 2*!!(cond)]))
20
21 #define BUG()                   do { fflush(stdout); assert(0); unreachable(); } while (0)
22 #define BUG_ON(cond)            assert(!(cond))
23
24 #define WARN(cond, fmt, ...)                                            \
25 ({                                                                      \
26         int __ret_warn_on = unlikely(!!(cond));                         \
27         if (__ret_warn_on)                                              \
28                 fprintf(stderr, "WARNING at " __FILE__ ":%d: " fmt "\n",\
29                         __LINE__, ##__VA_ARGS__);                       \
30         __ret_warn_on;                                                  \
31 })
32
33 #define __WARN()                                                        \
34 do {                                                                    \
35         fprintf(stderr, "WARNING at " __FILE__ ":%d\n", __LINE__);      \
36 } while (0)
37
38 #define WARN_ON(cond) ({                                                \
39         int __ret_warn_on = unlikely(!!(cond));                         \
40         if (__ret_warn_on)                                              \
41                 __WARN();                                               \
42         __ret_warn_on;                                                  \
43 })
44
45 #define WARN_ONCE(cond, fmt, ...)                                       \
46 ({                                                                      \
47         static bool __warned;                                           \
48         int __ret_warn_on = unlikely(!!(cond));                         \
49         if (__ret_warn_on && !__warned) {                               \
50                 __warned = true;                                        \
51                 __WARN();                                               \
52         }                                                               \
53         __ret_warn_on;                                                  \
54 })
55
56 #define WARN_ON_ONCE(cond) ({                                           \
57         static bool __warned;                                           \
58         int __ret_warn_on = unlikely(!!(cond));                         \
59         if (__ret_warn_on && !__warned) {                               \
60                 __warned = true;                                        \
61                 __WARN();                                               \
62         }                                                               \
63         __ret_warn_on;                                                  \
64 })
65
66 #endif /* __TOOLS_LINUX_BUG_H */