PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
defs.h
Go to the documentation of this file.
1#ifndef _SYS_DEFS_H
2#define _SYS_DEFS_H 1
3
4#include <stdbool.h>
5#include <stddef.h>
6#include <stdint.h>
7
8/**
9 * @brief Defines.
10 * @ingroup libstd
11 * @defgroup libstd_sys_defs Defines
12 *
13 * @{
14 */
15
16/**
17 * @brief GCC aligned attribute.
18 *
19 * Tells the compiler to align a variable or structure field to the specified byte alignment.
20 *
21 * Usefull for caching or hardware requirements.
22 */
23#define ALIGNED(alignment) __attribute__((aligned(alignment)))
24
25/**
26 * @brief GCC packed attribute.
27 *
28 * Tells the compiler to pack a structure, meaning there will be no padding between members.
29 *
30 * Needed for most hardware structures.
31 *
32 */
33#define PACKED __attribute__((packed))
34
35/**
36 * @brief GCC noreturn function attribute.
37 *
38 * Tells the compiler that the fuction with said attribute will never return.
39 */
40#define NORETURN __attribute__((noreturn))
41
42/**
43 * @brief GCC noinline function attribute.
44 *
45 * Tells the compiler to never inline the function with said attribute.
46 *
47 */
48#define NOINLINE __attribute__((noinline))
49
50/**
51 * @brief GCC const function attribute.
52 *
53 * Tells the compiler that the fuction with said attribute only depends on the arguments passed to it, and will never
54 * access global variables.
55 *
56 */
57#define CONST_FUNC __attribute__((const))
58
59/**
60 * @brief GCC
61 *
62 * The `PURE` attribute tells gcc that the function with said attribute only depends on the arguments passed to it
63 * and potentially global variables.
64 *
65 */
66#define PURE __attribute__((pure))
67
68/**
69 * @brief Concatenates two tokens.
70 *
71 * This macro concatenates two tokens `a` and `b` into a single token.
72 *
73 * @param a The first token.
74 * @param b The second token.
75 * @return The concatenated token.
76 */
77#define CONCAT(a, b) CONCAT_INNER(a, b)
78#define CONCAT_INNER(a, b) a##b
79
80/**
81 * @brief The size of the red zone in bytes.
82 *
83 * The red zone is a region of memory below the stack pointer that is reserved and should not be modified by
84 * interrupt handlers or signal handlers. The compiler uses this area for temporary storage for the purpose of
85 * optimization.
86 */
87#define RED_ZONE_SIZE 128
88
89/**
90 * @brief Mark a variable as unused.
91 *
92 * This macro marks a variable as unused to prevent compiler warnings about unused variables.
93 *
94 * @param x The variable to mark as unused.
95 */
96#define UNUSED(x) (void)(x)
97
98/**
99 * @brief GCC unused function attribute.
100 *
101 * Tells the compiler that the function with said attribute might be unused, preventing warnings.
102 */
103#define UNUSED_FUNC __attribute__((unused))
104
105/**
106 * @brief Get the number of elements in a static array.
107 *
108 * @param x The array.
109 * @return The number of elements in the array.
110 */
111#define ARRAY_SIZE(x) ((size_t)(sizeof(x) / sizeof((x)[0])))
112
113/**
114 * @brief Mark a condition as likely.
115 *
116 * This macro marks a condition as likely to help the compiler optimize branch prediction.
117 *
118 * @param x The condition.
119 */
120#define LIKELY(x) __builtin_expect(!!(x), 1)
121
122/**
123 * @brief Mark a condition as unlikely.
124 *
125 * This macro marks a condition as unlikely to help the compiler optimize branch prediction.
126 *
127 * @param x The condition.
128 */
129#define UNLIKELY(x) __builtin_expect(!!(x), 0)
130
131/**
132 * @brief GCC constructor function attribute.
133 *
134 * Will add the function to the `.init_array` section with the given priority, the function can then be called using
135 * `INIT_CALL()`.
136 *
137 * Functions with a higher priority number are called last.
138 *
139 * @param priority The priority of the constructor function.
140 */
141#define CONSTRUCTOR(priority) __attribute__((used, constructor(priority)))
142
143/**
144 * @brief GCC destructor function attribute.
145 *
146 * Will add the function to the `.finit_array` section with the given priority, the function can then be called using
147 * `FINIT_CALL()`.
148 *
149 * Functions with a higher priority number are called last.
150 *
151 * @param priority The priority of the destructor function.
152 */
153#define DESTRUCTOR(priority) __attribute__((used, destructor(priority)))
154
155/**
156 * @brief Inline assembly macro.
157 *
158 * @param ... The assembly code to embed.
159 */
160#define ASM(...) __asm__ volatile(__VA_ARGS__)
161
162/** @} */
163
164#endif