5-malloc.md (#25)
[lunaix-os.git] / lunaix-os / includes / lunaix / spike.h
1 #ifndef __LUNAIX_SPIKE_H
2 #define __LUNAIX_SPIKE_H
3
4 #include <lunaix/compiler.h>
5
6 /** Some helper functions. As helpful as Spike the Dragon! :) **/
7
8 // 除法 v/(2^k) 向上取整
9 #define CEIL(v, k) (((v) + (1 << (k)) - 1) >> (k))
10
11 #define ICEIL(x, y) ((x) / (y) + ((x) % (y) != 0))
12
13 // 除法 v/(2^k) 向下取整
14 #define FLOOR(v, k) ((v) >> (k))
15
16 // 获取v最近的最大k倍数 (k=2^n)
17 #define ROUNDUP(v, k) (((v) + (k)-1) & ~((k)-1))
18
19 // 获取v最近的最小k倍数 (k=2^m)
20 #define ROUNDDOWN(v, k) ((v) & ~((k)-1))
21
22 #define MIN(a, b) ((a) < (b) ? (a) : (b))
23 #define MAX(a, b) ((a) > (b) ? (a) : (b))
24
25 #define is_pot(val) (((val) != 0) || ((val) & ((val)-1)) == 0)
26
27 /**
28  * @brief Fast log base 2 for integer, utilizing constant unfolding.
29  * Adopted from
30  * https://elixir.bootlin.com/linux/v4.4/source/include/linux/log2.h#L85
31  *
32  */
33 #define ILOG2(x)                                                               \
34     (__builtin_constant_p(x) ? ((x) == 0              ? 0                      \
35                                 : ((x) & (1ul << 31)) ? 31                     \
36                                 : ((x) & (1ul << 30)) ? 30                     \
37                                 : ((x) & (1ul << 29)) ? 29                     \
38                                 : ((x) & (1ul << 28)) ? 28                     \
39                                 : ((x) & (1ul << 27)) ? 27                     \
40                                 : ((x) & (1ul << 26)) ? 26                     \
41                                 : ((x) & (1ul << 25)) ? 25                     \
42                                 : ((x) & (1ul << 24)) ? 24                     \
43                                 : ((x) & (1ul << 23)) ? 23                     \
44                                 : ((x) & (1ul << 22)) ? 22                     \
45                                 : ((x) & (1ul << 21)) ? 21                     \
46                                 : ((x) & (1ul << 20)) ? 20                     \
47                                 : ((x) & (1ul << 19)) ? 19                     \
48                                 : ((x) & (1ul << 18)) ? 18                     \
49                                 : ((x) & (1ul << 17)) ? 17                     \
50                                 : ((x) & (1ul << 16)) ? 16                     \
51                                 : ((x) & (1ul << 15)) ? 15                     \
52                                 : ((x) & (1ul << 14)) ? 14                     \
53                                 : ((x) & (1ul << 13)) ? 13                     \
54                                 : ((x) & (1ul << 12)) ? 12                     \
55                                 : ((x) & (1ul << 11)) ? 11                     \
56                                 : ((x) & (1ul << 10)) ? 10                     \
57                                 : ((x) & (1ul << 9))  ? 9                      \
58                                 : ((x) & (1ul << 8))  ? 8                      \
59                                 : ((x) & (1ul << 7))  ? 7                      \
60                                 : ((x) & (1ul << 6))  ? 6                      \
61                                 : ((x) & (1ul << 5))  ? 5                      \
62                                 : ((x) & (1ul << 4))  ? 4                      \
63                                 : ((x) & (1ul << 3))  ? 3                      \
64                                 : ((x) & (1ul << 2))  ? 2                      \
65                                 : ((x) & (1ul << 1))  ? 1                      \
66                                                       : 0)                      \
67                              : (31 - clz(x)))
68
69 #ifndef __LUNAIXOS_NASSERT__
70 #define assert(cond)                                                           \
71     do {                                                                       \
72         if (unlikely(!(cond))) {                                                         \
73             __assert_fail(#cond, __FILE__, __LINE__);                          \
74         }                                                                      \
75     } while(0)
76
77 #define assert_msg(cond, msg)                                                  \
78     do {                                                                       \
79         if (unlikely(!(cond))) {                                                         \
80             __assert_fail(msg, __FILE__, __LINE__);                            \
81         }                                                                      \
82     } while(0)
83
84 #define must_success(statement)                                                \
85     do {                                                                       \
86         int err = (statement);                                                 \
87         if (err) panickf(#statement "failed with errcode=%d", err);           \
88     } while(0)
89
90 #define fail(msg) __assert_fail(msg, __FILE__, __LINE__);
91
92 void
93 __assert_fail(const char* expr, const char* file, unsigned int line)
94     __attribute__((noinline, noreturn));
95 #else
96 #define assert(cond) (void)(cond);          // assert nothing
97 #define assert_msg(cond, msg) (void)(cond); // assert nothing
98
99 #endif // __LUNAIXOS_NASSERT__
100
101 void noret
102 panick(const char* msg);
103
104 void noret
105 panickf(const char* fmt, ...);
106
107 #define wait_until(cond)                                                       \
108     while (!(cond))                                                            \
109         ;
110 #define loop_until(cond)                                                       \
111     while (!(cond))                                                            \
112         ;
113
114 #define wait_until_expire(cond, max)                                           \
115     ({                                                                         \
116         unsigned int __wcounter__ = (max);                                     \
117         while (!(cond) && __wcounter__-- > 1)                                  \
118             ;                                                                  \
119         __wcounter__;                                                          \
120     })
121
122 #endif /* __LUNAIX_SPIKE_H */