Zephyr Project API  3.4.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#define __syscall_always_inline static inline __attribute__((always_inline))
147#else
148#define __syscall
149#define __syscall_always_inline
150#endif /* ZTEST_UNITTEST */
151
152/* Definitions for struct declaration tags. These are sentinel values used by
153 * parse_syscalls.py to gather a list of names of struct declarations that
154 * have these tags applied for them.
155 */
156
157/* Indicates this is a driver subsystem */
158#define __subsystem
159
160/* Indicates this is a network socket object */
161#define __net_socket
162
163#ifndef BUILD_ASSERT
164/* Compile-time assertion that makes the build to fail.
165 * Common implementation swallows the message.
166 */
167#define BUILD_ASSERT(EXPR, MSG...) \
168 enum _CONCAT(__build_assert_enum, __COUNTER__) { \
169 _CONCAT(__build_assert, __COUNTER__) = 1 / !!(EXPR) \
170 }
171#endif
172
173/*
174 * This is meant to be used in conjunction with __in_section() and similar
175 * where scattered structure instances are concatenated together by the linker
176 * and walked by the code at run time just like a contiguous array of such
177 * structures.
178 *
179 * Assemblers and linkers may insert alignment padding by default whose
180 * size is larger than the natural alignment for those structures when
181 * gathering various section segments together, messing up the array walk.
182 * To prevent this, we need to provide an explicit alignment not to rely
183 * on the default that might just work by luck.
184 *
185 * Alignment statements in linker scripts are not sufficient as
186 * the assembler may add padding by itself to each segment when switching
187 * between sections within the same file even if it merges many such segments
188 * into a single section in the end.
189 */
190#define Z_DECL_ALIGN(type) __aligned(__alignof(type)) type
191
192/* Check if a pointer is aligned for against a specific byte boundary */
193#define IS_PTR_ALIGNED_BYTES(ptr, bytes) ((((uintptr_t)ptr) % bytes) == 0)
194
195/* Check if a pointer is aligned enough for a particular data type. */
196#define IS_PTR_ALIGNED(ptr, type) IS_PTR_ALIGNED_BYTES(ptr, __alignof(type))
197
205#define LINKER_KEEP(symbol) \
206 static const void * const symbol##_ptr __used \
207 __attribute__((__section__(".symbol_to_keep"))) = (void *)&symbol
208
209#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ */