Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
common.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2014 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_
8#define ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_
16/* Abstract use of extern keyword for compatibility between C and C++ */
17#ifdef __cplusplus
18#define EXTERN_C extern "C"
19#else
20#define EXTERN_C extern
21#endif
22
23/* Use TASK_ENTRY_CPP to tag task entry points defined in C++ files. */
24
25#ifdef __cplusplus
26#define TASK_ENTRY_CPP extern "C"
27#endif
28
29#ifndef ZRESTRICT
30#ifndef __cplusplus
31#define ZRESTRICT restrict
32#else
33#define ZRESTRICT
34#endif
35#endif
36
37/*
38 * Generate a reference to an external symbol.
39 * The reference indicates to the linker that the symbol is required
40 * by the module containing the reference and should be included
41 * in the image if the module is in the image.
42 *
43 * The assembler directive ".set" is used to define a local symbol.
44 * No memory is allocated, and the local symbol does not appear in
45 * the symbol table.
46 */
47
48#ifdef _ASMLANGUAGE
49 #define REQUIRES(sym) .set sym ## _Requires, sym
50#else
51 #define REQUIRES(sym) __asm__ (".set " # sym "_Requires, " # sym "\n\t");
52#endif
53
54#ifdef _ASMLANGUAGE
55 #define SECTION .section
56#endif
57
58/*
59 * If the project is being built for speed (i.e. not for minimum size) then
60 * align functions and branches in executable sections to improve performance.
61 */
62
63#ifdef _ASMLANGUAGE
64
65 #if defined(CONFIG_X86)
66
67 #ifdef PERF_OPT
68 #define PERFOPT_ALIGN .balign 16
69 #else
70 #define PERFOPT_ALIGN .balign 1
71 #endif
72
73 #elif defined(CONFIG_ARM) || defined(CONFIG_ARM64)
74
75 #define PERFOPT_ALIGN .balign 4
76
77 #elif defined(CONFIG_ARC)
78
79 /* .align assembler directive is supposed by all ARC toolchains and it is
80 * implemented in a same way across ARC toolchains.
81 */
82 #define PERFOPT_ALIGN .align 4
83
84 #elif defined(CONFIG_NIOS2) || defined(CONFIG_RISCV) || \
85 defined(CONFIG_XTENSA) || defined(CONFIG_MIPS)
86 #define PERFOPT_ALIGN .balign 4
87
88 #elif defined(CONFIG_ARCH_POSIX)
89
90 #elif defined(CONFIG_SPARC)
91
92 #define PERFOPT_ALIGN .align 4
93
94 #else
95
96 #error Architecture unsupported
97
98 #endif
99
100 #define GC_SECTION(sym) SECTION .text.##sym, "ax"
101
102#endif /* _ASMLANGUAGE */
103
104/* force inlining a function */
105
106#if !defined(_ASMLANGUAGE)
107 #ifdef CONFIG_COVERAGE
108 /*
109 * The always_inline attribute forces a function to be inlined,
110 * even ignoring -fno-inline. So for code coverage, do not
111 * force inlining of these functions to keep their bodies around
112 * so their number of executions can be counted.
113 *
114 * Note that "inline" is kept here for kobject_hash.c and
115 * priv_stacks_hash.c. These are built without compiler flags
116 * used for coverage. ALWAYS_INLINE cannot be empty as compiler
117 * would complain about unused functions. Attaching unused
118 * attribute would result in their text sections balloon more than
119 * 10 times in size, as those functions are kept in text section.
120 * So just keep "inline" here.
121 */
122 #define ALWAYS_INLINE inline
123 #else
124 #define ALWAYS_INLINE inline __attribute__((always_inline))
125 #endif
126#endif
127
128#define Z_STRINGIFY(x) #x
129#define STRINGIFY(s) Z_STRINGIFY(s)
130
131/* concatenate the values of the arguments into one */
132#define _DO_CONCAT(x, y) x ## y
133#define _CONCAT(x, y) _DO_CONCAT(x, y)
134
135/* Additionally used as a sentinel by gen_syscalls.py to identify what
136 * functions are system calls
137 *
138 * Note POSIX unit tests don't still generate the system call stubs, so
139 * until https://github.com/zephyrproject-rtos/zephyr/issues/5006 is
140 * fixed via possibly #4174, we introduce this hack -- which will
141 * disallow us to test system calls in POSIX unit testing (currently
142 * not used).
143 */
144#ifndef ZTEST_UNITTEST
145#define __syscall static inline
146#else
147#define __syscall
148#endif /* ZTEST_UNITTEST */
149
150/* Definitions for struct declaration tags. These are sentinel values used by
151 * parse_syscalls.py to gather a list of names of struct declarations that
152 * have these tags applied for them.
153 */
154
155/* Indicates this is a driver subsystem */
156#define __subsystem
157
158/* Indicates this is a network socket object */
159#define __net_socket
160
161#ifndef BUILD_ASSERT
162/* Compile-time assertion that makes the build to fail.
163 * Common implementation swallows the message.
164 */
165#define BUILD_ASSERT(EXPR, MSG...) \
166 enum _CONCAT(__build_assert_enum, __COUNTER__) { \
167 _CONCAT(__build_assert, __COUNTER__) = 1 / !!(EXPR) \
168 }
169#endif
170
171/*
172 * This is meant to be used in conjunction with __in_section() and similar
173 * where scattered structure instances are concatenated together by the linker
174 * and walked by the code at run time just like a contiguous array of such
175 * structures.
176 *
177 * Assemblers and linkers may insert alignment padding by default whose
178 * size is larger than the natural alignment for those structures when
179 * gathering various section segments together, messing up the array walk.
180 * To prevent this, we need to provide an explicit alignment not to rely
181 * on the default that might just work by luck.
182 *
183 * Alignment statements in linker scripts are not sufficient as
184 * the assembler may add padding by itself to each segment when switching
185 * between sections within the same file even if it merges many such segments
186 * into a single section in the end.
187 */
188#define Z_DECL_ALIGN(type) __aligned(__alignof(type)) type
189
190/* Check if a pointer is aligned enough for a particular data type. */
191#define IS_PTR_ALIGNED(ptr, type) ((((uintptr_t)ptr) % __alignof(type)) == 0)
192
210#define STRUCT_SECTION_ITERABLE(struct_type, name) \
211 Z_DECL_ALIGN(struct struct_type) name \
212 __in_section(_##struct_type, static, name) __used
213
214#define Z_STRUCT_SECTION_ITERABLE(struct_type, name) \
215 __DEPRECATED_MACRO \
216 STRUCT_SECTION_ITERABLE(struct_type, name)
217
226#define STRUCT_SECTION_ITERABLE_ALTERNATE(out_type, struct_type, name) \
227 Z_DECL_ALIGN(struct struct_type) name \
228 __in_section(_##out_type, static, name) __used
229
230#define Z_STRUCT_SECTION_ITERABLE_ALTERNATE(out_type, struct_type, name) \
231 __DEPRECATED_MACRO \
232 STRUCT_SECTION_ITERABLE_ALTERNATE(out_type, struct_type, name)
233
244#define STRUCT_SECTION_FOREACH(struct_type, iterator) \
245 extern struct struct_type _CONCAT(_##struct_type, _list_start)[]; \
246 extern struct struct_type _CONCAT(_##struct_type, _list_end)[]; \
247 for (struct struct_type *iterator = \
248 _CONCAT(_##struct_type, _list_start); \
249 ({ __ASSERT(iterator <= _CONCAT(_##struct_type, _list_end), \
250 "unexpected list end location"); \
251 iterator < _CONCAT(_##struct_type, _list_end); }); \
252 iterator++)
253
254#define Z_STRUCT_SECTION_FOREACH(struct_type, iterator) \
255 __DEPRECATED_MACRO \
256 STRUCT_SECTION_FOREACH(struct_type, iterator)
257 /* end of struct_section_apis */
261
262#define LOG2CEIL(x) \
263 ((((x) <= 4) ? 2 : (((x) <= 8) ? 3 : (((x) <= 16) ? \
264 4 : (((x) <= 32) ? 5 : (((x) <= 64) ? 6 : (((x) <= 128) ? \
265 7 : (((x) <= 256) ? 8 : (((x) <= 512) ? 9 : (((x) <= 1024) ? \
266 10 : (((x) <= 2048) ? 11 : (((x) <= 4096) ? 12 : (((x) <= 8192) ? \
267 13 : (((x) <= 16384) ? 14 : (((x) <= 32768) ? 15:(((x) <= 65536) ? \
268 16 : (((x) <= 131072) ? 17 : (((x) <= 262144) ? 18:(((x) <= 524288) ? \
269 19 : (((x) <= 1048576) ? 20 : (((x) <= 2097152) ? \
270 21 : (((x) <= 4194304) ? 22 : (((x) <= 8388608) ? \
271 23 : (((x) <= 16777216) ? 24 : (((x) <= 33554432) ? \
272 25 : (((x) <= 67108864) ? 26 : (((x) <= 134217728) ? \
273 27 : (((x) <= 268435456) ? 28 : (((x) <= 536870912) ? \
274 29 : (((x) <= 1073741824) ? 30 : (((x) <= 2147483648) ? \
275 31 : 32)))))))))))))))))))))))))))))))
276
277#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ */