MOS Source Code
Loading...
Searching...
No Matches
mos_global.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#ifdef __MOS_KERNEL__
6#include <abi-bits/errno.h>
7#endif
8#include <mos/compiler.h>
9#include <stddef.h>
10
11#ifdef __cplusplus
12#define MOS_STATIC_ASSERT static_assert
13#else
14#define MOS_STATIC_ASSERT _Static_assert
15#endif
16
17// clang-format off
18#if defined(__MOS_KERNEL__) && defined(__cplusplus)
19#define __BEGIN_DECLS extern "C" {
20#define __END_DECLS }
21#else
22#define __BEGIN_DECLS
23#define __END_DECLS
24#endif
25// clang-format on
26
27#define __aligned(x) __attribute__((__aligned__(x)))
28#define __malloc __attribute__((__malloc__))
29#define __packed __attribute__((__packed__))
30#define __printf(a, b) __attribute__((__format__(__printf__, a, b)))
31#define __pure __attribute__((__pure__))
32#define __section(S) __attribute__((__section__(S)))
33#define __maybe_unused __attribute__((__unused__))
34#define __used __attribute__((__used__))
35#define __nodiscard __attribute__((__warn_unused_result__))
36
37#define should_inline __maybe_unused static inline
38
39#define likely(x) __builtin_expect(!!(x), 1)
40#define unlikely(x) __builtin_expect(!!(x), 0)
41
42#define __types_compatible(a, b) __builtin_types_compatible_p(__typeof(a), __typeof(b))
43
44#ifndef __cplusplus
45#define do_container_of(ptr, type, member) \
46 __extension__({ \
47 void *real_ptr = (void *) (ptr); \
48 _Static_assert(__types_compatible(*(ptr), ((type *) 0)->member) | __types_compatible(*(ptr), void), "type mismatch: (" #type ") vs (" #ptr "->" #member ")"); \
49 ((type *) (real_ptr - offsetof(type, member))); \
50 })
51
52#define container_of(ptr, type, member) \
53 _Generic(ptr, const __typeof(*(ptr)) *: ((const type *) do_container_of(ptr, type, member)), default: ((type *) do_container_of(ptr, type, member)))
54#endif
55
56#define is_aligned(ptr, alignment) (((ptr_t) ptr & (alignment - 1)) == 0)
57
58#define GET_BIT(x, n) (((x) >> (n)) & 1)
59#define MASK_BITS(value, width) ((value) & ((1 << (width)) - 1))
60#define SET_BITS(bit, width, value) (MASK_BITS(value, width) << (bit))
61
62#define MOS_STRINGIFY2(x) #x
63#define MOS_STRINGIFY(x) MOS_STRINGIFY2(x)
64
65#define MOS_UNUSED(x) (void) (x)
66
67#define MOS_CONCAT_INNER(a, b) a##b
68#define MOS_CONCAT(a, b) MOS_CONCAT_INNER(a, b)
69
70#define MOS_WARNING_PUSH MOS_PRAGMA(diagnostic push)
71#define MOS_WARNING_POP MOS_PRAGMA(diagnostic pop)
72#define MOS_WARNING_DISABLE(text) MOS_PRAGMA(diagnostic ignored text)
73
74#define ALIGN_UP(addr, size) (((addr) + (size - 1)) & ~(size - 1))
75#define ALIGN_DOWN(addr, size) ((addr) & ~(size - 1))
76#define ALIGN_UP_TO_PAGE(addr) ALIGN_UP(addr, MOS_PAGE_SIZE)
77#define ALIGN_DOWN_TO_PAGE(addr) ALIGN_DOWN(addr, MOS_PAGE_SIZE)
78
79#define MOS_IN_RANGE(addr, start, end) ((addr) >= (start) && (addr) < (end))
80
81#define MOS_FOURCC(a, b, c, d) ((u32) (a) | ((u32) (b) << 8) | ((u32) (c) << 16) | ((u32) (d) << 24))
82#define MOS_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
83
84#define MOS_MAX_VADDR ((ptr_t) ~0)
85
86#define READ_ONCE(x) (*(volatile typeof(x) *) &(x))
87
88// clang-format off
89#define KB * 1024
90#define MB * 1024 KB
91#define GB * (u64) 1024 MB
92#define TB * (u64) 1024 GB
93#define statement_expr(type, ...) __extension__({ type retval; __VA_ARGS__; retval; })
94// clang-format on
95
96#define __NO_OP(...)
97
98#define BIT(x) (1ull << (x))
99
100#ifdef __cplusplus
101#define MOSAPI extern "C"
102#else
103#define MOSAPI extern
104#endif
105
106// If the feature is enabled, the expression will be 1, otherwise -1.
107// If the given feature is not defined, the expression will be 0, which throws a division by zero error.
108#define MOS_CONFIG(feat) (1 / feat == 1)
109
110#define MOS_DEBUG_FEATURE(feat) MOS_CONFIG(MOS_CONCAT(MOS_DEBUG_, feat))
111
115#define once() \
116 __extension__({ \
117 static bool __seen = false; \
118 bool ret = false; \
119 if (__seen) \
120 ret = false; \
121 else \
122 __seen = true, ret = true; \
123 ret; \
124 })
125
126#define MOS_PUT_IN_SECTION(_section, _struct, _var, ...) static const _struct _var __used __section(_section) = __VA_ARGS__
127#define IS_ERR_VALUE(x) unlikely((unsigned long) (void *) (x) >= (unsigned long) -4095)
128
129#define MOS_STUB_IMPL(...) \
130 MOS_WARNING_PUSH \
131 MOS_WARNING_DISABLE("-Wunused-parameter") \
132 __VA_ARGS__ \
133 { \
134 MOS_UNREACHABLE_X("unimplemented: file %s, line %d", __FILE__, __LINE__); \
135 } \
136 MOS_WARNING_POP