• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #ifndef COMMON_MACRO_H
13 #define COMMON_MACRO_H
14 
15 #ifdef CHCORE
16 #ifndef __ASM__
17 #include <common/kprint.h>
18 #include <common/backtrace.h>
19 #endif
20 #endif
21 
22 #define ALIGN(n) __attribute__((__aligned__(n)))
23 
24 #define ROUND_UP(x, n)     (((x) + (n)-1) & ~((n)-1))
25 #define ROUND_DOWN(x, n)   ((x) & ~((n)-1))
26 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
27 
28 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
29 
30 #define BIT(x) (1ULL << (x))
31 
32 #define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER)
33 #define container_of(ptr, type, field) \
34     ((type *)((void *)(ptr) - (void *)(&(((type *)(0))->field))))
35 
36 #define container_of_safe(ptr, type, field)             \
37     ({                                                  \
38         typeof(ptr) __ptr = (ptr);                      \
39         type *__obj = container_of(__ptr, type, field); \
40         (__ptr ? __obj : NULL);                         \
41     })
42 
43 #define MAX(x, y) ((x) < (y) ? (y) : (x))
44 #define MIN(x, y) ((x) < (y) ? (x) : (y))
45 
46 
47 #define BUG_ON(expr)                                                        \
48     do {                                                                    \
49         if ((expr)) {                                                       \
50             printk("BUG: %s:%d on (expr) %s\n", __func__, __LINE__, #expr); \
51             backtrace();                                                    \
52             for (;;) {                                                      \
53             }                                                               \
54         }                                                                   \
55     } while (0)
56 
57 #define BUG(str, ...)                                                      \
58     do {                                                                   \
59         printk("BUG: %s:%d " str "\n", __func__, __LINE__, ##__VA_ARGS__); \
60         backtrace();                                                       \
61         for (;;) {                                                         \
62         }                                                                  \
63     } while (0)
64 
65 #define WARN(msg) printk("WARN: %s:%d %s\n", __func__, __LINE__, msg)
66 
67 #define WARN_ON(cond, msg)                                                    \
68     do {                                                                      \
69         if ((cond)) {                                                         \
70             printk("WARN: %s:%d %s on " #cond "\n", __func__, __LINE__, msg); \
71         }                                                                     \
72     } while (0)
73 
74 #ifdef __GNUC__
75 #define likely(x)   __builtin_expect(!!(x), 1)
76 #define unlikely(x) __builtin_expect(!!(x), 0)
77 #else
78 #define likely(x)   (!!(x))
79 #define unlikely(x) (!!(x))
80 #endif // __GNUC__
81 
82 /*
83  * Different platform may have different cacheline size
84  * and may have some features like prefetch.
85  */
86 #define CACHELINE_SZ         64
87 #define pad_to_cache_line(n) (ROUND_UP(n, CACHELINE_SZ) - (n))
88 
89 /* Common enums */
90 #define OBJECT_STATE_INVALID (0)
91 #define OBJECT_STATE_VALID   (1)
92 
93 #endif /* COMMON_MACRO_H */