1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright 2023 Red Hat
4 */
5
6 #ifndef PERMASSERT_H
7 #define PERMASSERT_H
8
9 #include <linux/compiler.h>
10
11 #include "errors.h"
12
13 /* Utilities for asserting that certain conditions are met */
14
15 #define STRINGIFY(X) #X
16
17 /*
18 * A hack to apply the "warn if unused" attribute to an integral expression.
19 *
20 * Since GCC doesn't propagate the warn_unused_result attribute to conditional expressions
21 * incorporating calls to functions with that attribute, this function can be used to wrap such an
22 * expression. With optimization enabled, this function contributes no additional instructions, but
23 * the warn_unused_result attribute still applies to the code calling it.
24 */
vdo_must_use(int value)25 static inline int __must_check vdo_must_use(int value)
26 {
27 return value;
28 }
29
30 /* Assert that an expression is true and return an error if it is not. */
31 #define VDO_ASSERT(expr, ...) vdo_must_use(__VDO_ASSERT(expr, __VA_ARGS__))
32
33 /* Log a message if the expression is not true. */
34 #define VDO_ASSERT_LOG_ONLY(expr, ...) __VDO_ASSERT(expr, __VA_ARGS__)
35
36 #define __VDO_ASSERT(expr, ...) \
37 (likely(expr) ? VDO_SUCCESS \
38 : vdo_assertion_failed(STRINGIFY(expr), __FILE__, __LINE__, __VA_ARGS__))
39
40 /* Log an assertion failure message. */
41 int vdo_assertion_failed(const char *expression_string, const char *file_name,
42 int line_number, const char *format, ...)
43 __printf(4, 5);
44
45 #endif /* PERMASSERT_H */
46