Zephyr Project API
3.3.0
A Scalable Open Source RTOS
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 for against a specific byte boundary */
191
#define IS_PTR_ALIGNED_BYTES(ptr, bytes) ((((uintptr_t)ptr) % bytes) == 0)
192
193
/* Check if a pointer is aligned enough for a particular data type. */
194
#define IS_PTR_ALIGNED(ptr, type) IS_PTR_ALIGNED_BYTES(ptr, __alignof(type))
195
216
#define STRUCT_SECTION_ITERABLE(struct_type, name) \
217
Z_DECL_ALIGN(struct struct_type) name \
218
__in_section(_##struct_type, static, name) __used __noasan
219
228
#define STRUCT_SECTION_ITERABLE_ALTERNATE(out_type, struct_type, name) \
229
Z_DECL_ALIGN(struct struct_type) name \
230
__in_section(_##out_type, static, name) __used __noasan
231
242
#define STRUCT_SECTION_FOREACH(struct_type, iterator) \
243
extern struct struct_type _CONCAT(_##struct_type, _list_start)[]; \
244
extern struct struct_type _CONCAT(_##struct_type, _list_end)[]; \
245
for (struct struct_type *iterator = \
246
_CONCAT(_##struct_type, _list_start); \
247
({ __ASSERT(iterator <= _CONCAT(_##struct_type, _list_end), \
248
"unexpected list end location"
); \
249
iterator < _CONCAT(_##struct_type, _list_end); }); \
250
iterator++)
251
261
#define STRUCT_SECTION_GET(struct_type, i, dst) do { \
262
extern struct struct_type _CONCAT(_##struct_type, _list_start)[]; \
263
*(dst) = &_CONCAT(_##struct_type, _list_start)[i]; \
264
} while (0)
265
272
#define STRUCT_SECTION_COUNT(struct_type, dst) do { \
273
extern struct struct_type _CONCAT(_##struct_type, _list_start)[]; \
274
extern struct struct_type _CONCAT(_##struct_type, _list_end)[]; \
275
*(dst) = ((uintptr_t)_CONCAT(_##struct_type, _list_end) - \
276
(uintptr_t)_CONCAT(_##struct_type, _list_start)) / sizeof(struct struct_type); \
277
} while (0)
278
/* end of struct_section_apis */
282
290
#define LINKER_KEEP(symbol) \
291
static const void * const symbol##_ptr __used \
292
__attribute__((__section__(".symbol_to_keep"
))) = (void *)&symbol
293
294
#define LOG2CEIL(x) \
295
((((x) <= 4) ? 2 : (((x) <= 8) ? 3 : (((x) <= 16) ? \
296
4 : (((x) <= 32) ? 5 : (((x) <= 64) ? 6 : (((x) <= 128) ? \
297
7 : (((x) <= 256) ? 8 : (((x) <= 512) ? 9 : (((x) <= 1024) ? \
298
10 : (((x) <= 2048) ? 11 : (((x) <= 4096) ? 12 : (((x) <= 8192) ? \
299
13 : (((x) <= 16384) ? 14 : (((x) <= 32768) ? 15:(((x) <= 65536) ? \
300
16 : (((x) <= 131072) ? 17 : (((x) <= 262144) ? 18:(((x) <= 524288) ? \
301
19 : (((x) <= 1048576) ? 20 : (((x) <= 2097152) ? \
302
21 : (((x) <= 4194304) ? 22 : (((x) <= 8388608) ? \
303
23 : (((x) <= 16777216) ? 24 : (((x) <= 33554432) ? \
304
25 : (((x) <= 67108864) ? 26 : (((x) <= 134217728) ? \
305
27 : (((x) <= 268435456) ? 28 : (((x) <= 536870912) ? \
306
29 : (((x) <= 1073741824) ? 30 : (((x) <= 2147483648) ? \
307
31 : 32)))))))))))))))))))))))))))))))
308
309
#endif
/* ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ */
include
zephyr
toolchain
common.h
Generated on Fri Jun 9 2023 14:09:43 for Zephyr Project API by
1.9.2