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