Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2014 Wind River Systems, Inc.
3 * Copyright (c) 2025 Siemens AG
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_
9#define ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_
10
11#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_H_
12#error Please do not include toolchain-specific headers directly, use <zephyr/toolchain.h> instead
13#endif
14
22/* Abstract use of extern keyword for compatibility between C and C++ */
23#ifdef __cplusplus
24#define EXTERN_C extern "C"
25#else
26#define EXTERN_C extern
27#endif
28
29/* Use TASK_ENTRY_CPP to tag task entry points defined in C++ files. */
30
31#ifdef __cplusplus
32#define TASK_ENTRY_CPP extern "C"
33#endif
34
35#ifndef ZRESTRICT
36#ifndef __cplusplus
37#define ZRESTRICT restrict
38#else
39#define ZRESTRICT
40#endif
41#endif
42
43/*
44 * Generate a reference to an external symbol.
45 * The reference indicates to the linker that the symbol is required
46 * by the module containing the reference and should be included
47 * in the image if the module is in the image.
48 *
49 * The assembler directive ".set" is used to define a local symbol.
50 * No memory is allocated, and the local symbol does not appear in
51 * the symbol table.
52 */
53
54#ifdef _ASMLANGUAGE
55 #define REQUIRES(sym) .set sym ## _Requires, sym
56#else
57 #define REQUIRES(sym) __asm__ (".set " # sym "_Requires, " # sym "\n\t");
58#endif
59
60#ifdef _ASMLANGUAGE
61 #define SECTION .section
62#endif
63
64/*
65 * General directive for assembly code, to align the following symbol, in bytes.
66 *
67 * Example:
68 *
69 * ALIGN(4)
70 * test_symbol:
71 *
72 * 'test_symbol' will get aligned to 4 bytes.
73 */
74
75#if defined(_ASMLANGUAGE) && !defined(_LINKER)
76
77 #if defined(CONFIG_X86) || defined(CONFIG_ARM) || defined(CONFIG_ARM64) || \
78 defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
79 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS) || \
80 defined(CONFIG_ARCH_POSIX)
81 #define ALIGN(x) .balign x
82 #elif defined(CONFIG_ARC)
83 /* .align assembler directive is supported by all ARC toolchains and it is
84 * implemented in the same way across ARC toolchains.
85 */
86 #define ALIGN(x) .align x
87 #elif defined(CONFIG_SPARC)
88 #define ALIGN(x) .align x
89 #else
90 #error Architecture unsupported
91 #endif
92
93#endif /* defined(_ASMLANGUAGE) && !defined(_LINKER) */
94
95/*
96 * If the project is being built for speed (i.e. not for minimum size) then
97 * align functions and branches in executable sections to improve performance.
98 */
99
100#ifdef _ASMLANGUAGE
101
102 #if defined(CONFIG_X86)
103
104 #ifdef PERF_OPT
105 #define PERFOPT_ALIGN .balign 16
106 #else
107 #define PERFOPT_ALIGN .balign 1
108 #endif
109
110 #elif defined(CONFIG_ARM) || defined(CONFIG_ARM64)
111
112 #define PERFOPT_ALIGN .balign 4
113
114 #elif defined(CONFIG_ARC)
115
116 /* .align assembler directive is supposed by all ARC toolchains and it is
117 * implemented in a same way across ARC toolchains.
118 */
119 #define PERFOPT_ALIGN .align 4
120
121 #elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
122 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
123 #define PERFOPT_ALIGN .balign 4
124
125 #elif defined(CONFIG_ARCH_POSIX)
126
127 #elif defined(CONFIG_SPARC)
128
129 #define PERFOPT_ALIGN .align 4
130
131 #else
132
133 #error Architecture unsupported
134
135 #endif
136
137 #define GC_SECTION(sym) SECTION .text.##sym, "ax"
138
139#endif /* _ASMLANGUAGE */
140
141/* force inlining a function */
142
143#if !defined(_ASMLANGUAGE)
144 #ifdef CONFIG_COVERAGE
145 /*
146 * The always_inline attribute forces a function to be inlined,
147 * even ignoring -fno-inline. So for code coverage, do not
148 * force inlining of these functions to keep their bodies around
149 * so their number of executions can be counted.
150 *
151 * Note that "inline" is kept here for kobject_hash.c and
152 * priv_stacks_hash.c. These are built without compiler flags
153 * used for coverage. ALWAYS_INLINE cannot be empty as compiler
154 * would complain about unused functions. Attaching unused
155 * attribute would result in their text sections balloon more than
156 * 10 times in size, as those functions are kept in text section.
157 * So just keep "inline" here.
158 */
159 #define ALWAYS_INLINE inline
160 #else
161 #define ALWAYS_INLINE inline __attribute__((always_inline))
162 #endif
163#endif
164
165#define Z_STRINGIFY(x) #x
166#define STRINGIFY(s) Z_STRINGIFY(s)
167
168/* concatenate the values of the arguments into one */
169#define _DO_CONCAT(x, y) x ## y
170#define _CONCAT(x, y) _DO_CONCAT(x, y)
171
172/* Additionally used as a sentinel by gen_syscalls.py to identify what
173 * functions are system calls
174 *
175 * Note POSIX unit tests don't still generate the system call stubs, so
176 * until https://github.com/zephyrproject-rtos/zephyr/issues/5006 is
177 * fixed via possibly #4174, we introduce this hack -- which will
178 * disallow us to test system calls in POSIX unit testing (currently
179 * not used).
180 */
181#ifndef ZTEST_UNITTEST
182#define __syscall static inline
183#define __syscall_always_inline static inline __attribute__((always_inline))
184#else
185#define __syscall
186#define __syscall_always_inline
187#endif /* ZTEST_UNITTEST */
188
189/* Definitions for struct declaration tags. These are sentinel values used by
190 * parse_syscalls.py to gather a list of names of struct declarations that
191 * have these tags applied for them.
192 */
193
194/* Indicates this is a driver subsystem */
195#define __subsystem
196
197/* Indicates this is a network socket object */
198#define __net_socket
199
200#ifndef BUILD_ASSERT
201/* Compile-time assertion that makes the build to fail.
202 * Common implementation swallows the message.
203 */
204#define BUILD_ASSERT(EXPR, MSG...) \
205 enum _CONCAT(__build_assert_enum, __COUNTER__) { \
206 _CONCAT(__build_assert, __COUNTER__) = 1 / !!(EXPR) \
207 }
208#endif
209
210/*
211 * This is meant to be used in conjunction with __in_section() and similar
212 * where scattered structure instances are concatenated together by the linker
213 * and walked by the code at run time just like a contiguous array of such
214 * structures.
215 *
216 * Assemblers and linkers may insert alignment padding by default whose
217 * size is larger than the natural alignment for those structures when
218 * gathering various section segments together, messing up the array walk.
219 * To prevent this, we need to provide an explicit alignment not to rely
220 * on the default that might just work by luck.
221 *
222 * Alignment statements in linker scripts are not sufficient as
223 * the assembler may add padding by itself to each segment when switching
224 * between sections within the same file even if it merges many such segments
225 * into a single section in the end.
226 */
227#define Z_DECL_ALIGN(type) __aligned(__alignof(type)) type
228
229/* Check if a pointer is aligned for against a specific byte boundary */
230#define IS_PTR_ALIGNED_BYTES(ptr, bytes) ((((uintptr_t)ptr) % bytes) == 0)
231
232/* Check if a pointer is aligned enough for a particular data type. */
233#define IS_PTR_ALIGNED(ptr, type) IS_PTR_ALIGNED_BYTES(ptr, __alignof(type))
234
242#define LINKER_KEEP(symbol) \
243 static const void * const symbol##_ptr __used \
244 __attribute__((__section__(".symbol_to_keep"))) = (void *)&symbol
245
246#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ */