Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
iterable_sections.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020, Intel Corporation
3 * Copyright (C) 2023, Nordic Semiconductor ASA
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef INCLUDE_ZEPHYR_SYS_ITERABLE_SECTIONS_H_
14#define INCLUDE_ZEPHYR_SYS_ITERABLE_SECTIONS_H_
15
16#include <zephyr/sys/__assert.h>
17#include <zephyr/toolchain.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
29
48#define TYPE_SECTION_ITERABLE(type, varname, secname, section_postfix) \
49 Z_DECL_ALIGN(type) varname \
50 __in_section(_##secname, static, _CONCAT(section_postfix, _)) __used __noasan
51
61#define TYPE_SECTION_START(secname) _CONCAT(_##secname, _list_start)
62
71#define TYPE_SECTION_END(secname) _CONCAT(_##secname, _list_end)
72
84#define TYPE_SECTION_START_EXTERN(type, secname) \
85 extern type TYPE_SECTION_START(secname)[]
86
98#define TYPE_SECTION_END_EXTERN(type, secname) \
99 extern type TYPE_SECTION_END(secname)[]
100
111#define TYPE_SECTION_FOREACH(type, secname, iterator) \
112 TYPE_SECTION_START_EXTERN(type, secname); \
113 TYPE_SECTION_END_EXTERN(type, secname); \
114 for (type * iterator = TYPE_SECTION_START(secname); ({ \
115 __ASSERT(iterator <= TYPE_SECTION_END(secname),\
116 "unexpected list end location"); \
117 iterator < TYPE_SECTION_END(secname); \
118 }); \
119 iterator++)
120
133#define TYPE_SECTION_FOREACH_REVERSE(type, secname, iterator) \
134 TYPE_SECTION_START_EXTERN(type, secname); \
135 TYPE_SECTION_END_EXTERN(type, secname); \
136 for (type *iterator = TYPE_SECTION_END(secname); \
137 (uintptr_t)iterator > (uintptr_t)TYPE_SECTION_START(secname) && \
138 (iterator = (type *)((uintptr_t)iterator - sizeof(type)), true); \
139 )
140
151#define TYPE_SECTION_GET(type, secname, i, dst) do { \
152 TYPE_SECTION_START_EXTERN(type, secname); \
153 *(dst) = &TYPE_SECTION_START(secname)[i]; \
154} while (0)
155
163#define TYPE_SECTION_COUNT(type, secname, dst) do { \
164 TYPE_SECTION_START_EXTERN(type, secname); \
165 TYPE_SECTION_END_EXTERN(type, secname); \
166 *(dst) = ((uintptr_t)TYPE_SECTION_END(secname) - \
167 (uintptr_t)TYPE_SECTION_START(secname)) / sizeof(type); \
168} while (0)
169
175#define STRUCT_SECTION_START(struct_type) \
176 TYPE_SECTION_START(struct_type)
177
185#define STRUCT_SECTION_START_EXTERN(struct_type) \
186 TYPE_SECTION_START_EXTERN(struct struct_type, struct_type)
187
193#define STRUCT_SECTION_END(struct_type) \
194 TYPE_SECTION_END(struct_type)
195
203#define STRUCT_SECTION_END_EXTERN(struct_type) \
204 TYPE_SECTION_END_EXTERN(struct struct_type, struct_type)
205
214#define STRUCT_SECTION_ITERABLE_ALTERNATE(secname, struct_type, varname) \
215 TYPE_SECTION_ITERABLE(struct struct_type, varname, secname, varname)
216
223#define STRUCT_SECTION_ITERABLE_ARRAY_ALTERNATE(secname, struct_type, varname, \
224 size) \
225 TYPE_SECTION_ITERABLE(struct struct_type, varname[size], secname, \
226 varname)
227
242#define STRUCT_SECTION_ITERABLE(struct_type, varname) \
243 STRUCT_SECTION_ITERABLE_ALTERNATE(struct_type, struct_type, varname)
244
250#define STRUCT_SECTION_ITERABLE_ARRAY(struct_type, varname, size) \
251 STRUCT_SECTION_ITERABLE_ARRAY_ALTERNATE(struct_type, struct_type, \
252 varname, size)
253
260#define STRUCT_SECTION_ITERABLE_NAMED(struct_type, name, varname) \
261 TYPE_SECTION_ITERABLE(struct struct_type, varname, struct_type, name)
262
270#define STRUCT_SECTION_ITERABLE_NAMED_ALTERNATE(struct_type, secname, name, varname) \
271 TYPE_SECTION_ITERABLE(struct struct_type, varname, secname, name)
272
283#define STRUCT_SECTION_FOREACH_ALTERNATE(secname, struct_type, iterator) \
284 TYPE_SECTION_FOREACH(struct struct_type, secname, iterator)
285
296#define STRUCT_SECTION_FOREACH(struct_type, iterator) \
297 STRUCT_SECTION_FOREACH_ALTERNATE(struct_type, struct_type, iterator)
298
311#define STRUCT_SECTION_FOREACH_ALTERNATE_REVERSE(secname, struct_type, iterator) \
312 TYPE_SECTION_FOREACH_REVERSE(struct struct_type, secname, iterator)
313
325#define STRUCT_SECTION_FOREACH_REVERSE(struct_type, iterator) \
326 STRUCT_SECTION_FOREACH_ALTERNATE_REVERSE(struct_type, struct_type, iterator)
327
337#define STRUCT_SECTION_GET(struct_type, i, dst) \
338 TYPE_SECTION_GET(struct struct_type, struct_type, i, dst)
339
346#define STRUCT_SECTION_COUNT(struct_type, dst) \
347 TYPE_SECTION_COUNT(struct struct_type, struct_type, dst);
348 /* end of struct_section_apis */
352
353#ifdef __cplusplus
354}
355#endif
356
357#endif /* INCLUDE_ZEPHYR_SYS_ITERABLE_SECTIONS_H_ */
Macros to abstract toolchain specific capabilities.