aboutsummaryrefslogtreecommitdiff
path: root/include/apos/assert.h
blob: 97523c7cb8e4134af4ea84a03b1822f4468a1a6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef APOS_ASSERT_H
#define APOS_ASSERT_H

/**
 * @file assert.h
 * Assertions. Note that contrary to how assertios usually function, apos has
 * three different levels of assertions: Catastrophic, hard and soft.
 *
 * Soft assertions merely warn about something that might cause issues, but let
 * the execution continue normally.
 *
 * Hard assertions warn about the assertion not holding and returns from the
 * function.
 *
 * Catastrophic assertions warn about the assertion and crash the kernel.
 */

#include <apos/debug.h>
#include <apos/utils.h>

/* TODO: should this exit or do something explosive like that? */
#if !defined(DNDEBUG)

/**
 * The kernel is in an irrepairable state, just give up.
 *
 * @param x Condition to check for.
 */
#define catastrophic_assert(x)                                                 \
	do {                                                                   \
		if (unlikely(!(x))) {                                          \
			error("catastrophic assertion failed: " QUOTE(x) "\n"); \
			while (1) {                                            \
			}                                                      \
		}                                                              \
	} while (0);

/**
 * The function cannot continue without this assertion, but doesn't necessarily
 * mean that the kernel is borked.
 *
 * @warning Implicit return.
 *
 * @param x Condition to check for.
 * @param r Return value on failed check.
 */
#define hard_assert(x, r)                                                      \
	{                                                                      \
		if (unlikely(!(x))) {                                          \
			warn("hard assertion failed: " QUOTE(x) "\n");         \
			return r;                                              \
		}                                                              \
	}

/**
 * Unexpected case, but not likely to cause problems, likely a bug.
 *
 * @param x Condition to check for.
 */
#define soft_assert(x)                                                         \
	do {                                                                   \
		if (unlikely(!(x))) {                                          \
			info("soft assertion failed: "  QUOTE(x) "\n");        \
		}                                                              \
	} while (0);
#else
#define catastrophic_assert(x)
#define hard_assert(x, r)
#define soft_assert(x)
#endif

/**
 * Use when return value doesn't exist.
 *
 * Example:
 * @code{.c}
 * void func() { hard_assert(x, RETURN_VOID); }
 * @endcode
 */
#define RETURN_VOID

#endif /* APOS_ASSERT_H */