Moving align macros into vm utils header.
[akaros.git] / kern / include / common.h
1 #pragma once
2
3 #include <ros/common.h>
4 #include <compiler.h>
5
6 /* Force a rebuild of the whole kernel if 64BIT-ness changed */
7 #ifdef CONFIG_64BIT
8 #endif
9
10 #define SIZE_MAX        (~(size_t)0)
11
12 // Efficient min and max operations
13 #define MIN(_a, _b)                                             \
14 ({                                                              \
15         typeof(_a) __a = (_a);                                  \
16         typeof(_b) __b = (_b);                                  \
17         __a <= __b ? __a : __b;                                 \
18 })
19 #define MAX(_a, _b)                                             \
20 ({                                                              \
21         typeof(_a) __a = (_a);                                  \
22         typeof(_b) __b = (_b);                                  \
23         __a >= __b ? __a : __b;                                 \
24 })
25
26 /* Test for alignment, e.g. 2^6 */
27 #define ALIGNED(p, a)   (!(((uintptr_t)(p)) & ((a)-1)))
28 /* Aligns x up to the mask, e.g. (2^6 - 1) (round up if any mask bits are set)*/
29 #define __ALIGN_MASK(x, mask) (((uintptr_t)(x) + (mask)) & ~(mask))
30 /* Aligns x up to the alignment, e.g. 2^6. */
31 #define ALIGN(x, a) ((typeof(x)) __ALIGN_MASK(x, (a) - 1))
32 /* Aligns x down to the mask, e.g. (2^6 - 1)
33  * (round down if any mask bits are set)*/
34 #define __ALIGN_MASK_DOWN(x, mask) ((uintptr_t)(x) & ~(mask))
35 /* Aligns x down to the alignment, e.g. 2^6. */
36 #define ALIGN_DOWN(x, a) ((typeof(x)) __ALIGN_MASK_DOWN(x, (a) - 1))
37 /* Will return false for 0.  Debatable, based on what you want. */
38 #define IS_PWR2(x) ((x) && !((x) & (x - 1)))
39
40 #define ARRAY_SIZE(x) COUNT_OF(x)
41
42 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
43  * other callers spin til the func is complete. */
44 #define run_once(func)                                                         \
45 do {                                                                           \
46         static bool ran_once = FALSE;                                              \
47         static bool is_running = FALSE;                                            \
48         if (!ran_once) {                                                           \
49                 /* fetch and set TRUE, without a header or test_and_set weirdness */   \
50                 if (!__sync_fetch_and_or(&is_running, TRUE)) {                         \
51                         /* we won the race and get to run the func */                      \
52                         func;                                                              \
53                         wmb();  /* don't let the ran_once write pass previous writes */    \
54                         ran_once = TRUE;                                                   \
55                 } else {                                                               \
56                         /* someone else won, wait til they are done to break out */        \
57                         while (!ran_once)                                                  \
58                                 cpu_relax();                                                   \
59                 }                                                                      \
60         }                                                                          \
61 } while (0)
62
63 /* Unprotected, single-threaded version, makes sure func is run exactly once */
64 #define run_once_racy(func)                                                    \
65 do {                                                                           \
66         static bool ran_once = FALSE;                                              \
67         if (!ran_once) {                                                           \
68                 func;                                                                  \
69                 ran_once = TRUE;                                                       \
70         }                                                                          \
71 } while (0)
72
73 #ifndef __ASSEMBLER__
74
75 static inline uint32_t low32(uint64_t val)
76 {
77         return val & 0xffffffff;
78 }
79
80 static inline uint32_t high32(uint64_t val)
81 {
82         return val >> 32;
83 }
84
85 #endif /* !__ASSEMBLER__ */