feat: twifs - pseudo file system for lunaix kernel state exposure
[lunaix-os.git] / lunaix-os / includes / lunaix / spike.h
1 #ifndef __LUNAIX_SPIKE_H
2 #define __LUNAIX_SPIKE_H
3
4 // Some helper functions. As helpful as Spike the Dragon! :)
5
6 // 除法向上取整
7 #define CEIL(v, k) (((v) + (1 << (k)) - 1) >> (k))
8
9 #define ICEIL(x, y) ((x) / (y) + ((x) % (y) != 0))
10
11 // 除法向下取整
12 #define FLOOR(v, k) ((v) >> (k))
13
14 // 获取v最近的最大k倍数
15 #define ROUNDUP(v, k) (((v) + (k)-1) & ~((k)-1))
16
17 // 获取v最近的最小k倍数
18 #define ROUNDDOWN(v, k) ((v) & ~((k)-1))
19
20 #define MIN(a, b) ((a) < (b) ? (a) : (b))
21 #define MAX(a, b) ((a) > (b) ? (a) : (b))
22
23 #define __USER__ __attribute__((section(".usrtext")))
24
25 inline static void
26 spin()
27 {
28     while (1)
29         ;
30 }
31
32 #ifdef __LUNAIXOS_DEBUG__
33 #define assert(cond)                                                           \
34     if (!(cond)) {                                                             \
35         __assert_fail(#cond, __FILE__, __LINE__);                              \
36     }
37
38 #define assert_msg(cond, msg)                                                  \
39     if (!(cond)) {                                                             \
40         __assert_fail(msg, __FILE__, __LINE__);                                \
41     }
42 void
43 __assert_fail(const char* expr, const char* file, unsigned int line)
44   __attribute__((noinline, noreturn));
45 #else
46 #define assert(cond) (void)(cond);          // assert nothing
47 #define assert_msg(cond, msg) (void)(cond); // assert nothing
48 #endif
49
50 void
51 panick(const char* msg);
52
53 #define wait_until(cond)                                                       \
54     while (!(cond))                                                            \
55         ;
56 #define loop_until(cond)                                                       \
57     while (!(cond))                                                            \
58         ;
59
60 #define wait_until_expire(cond, max)                                           \
61     ({                                                                         \
62         unsigned int __wcounter__ = (max);                                     \
63         while (!(cond) && __wcounter__-- > 1)                                  \
64             ;                                                                  \
65         __wcounter__;                                                          \
66     })
67
68 #endif /* __LUNAIX_SPIKE_H */