Zephyr Project API 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 ZEPHYR_INCLUDE_SYS_ITERABLE_SECTIONS_H_
14#define ZEPHYR_INCLUDE_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
31
50#define TYPE_SECTION_ITERABLE(type, varname, secname, section_postfix) \
51 Z_DECL_ALIGN(type) varname \
52 __in_section(_##secname, static, _CONCAT(section_postfix, _)) __used __noasan
53
63#define TYPE_SECTION_START(secname) _CONCAT(_##secname, _list_start)
64
73#define TYPE_SECTION_END(secname) _CONCAT(_##secname, _list_end)
74
86#define TYPE_SECTION_START_EXTERN(type, secname) \
87 extern type TYPE_SECTION_START(secname)[]
88
100#define TYPE_SECTION_END_EXTERN(type, secname) \
101 extern type TYPE_SECTION_END(secname)[]
102
113#define TYPE_SECTION_FOREACH(type, secname, iterator) \
114 TYPE_SECTION_START_EXTERN(type, secname); \
115 TYPE_SECTION_END_EXTERN(type, secname); \
116 for (type * iterator = TYPE_SECTION_START(secname); ({ \
117 __ASSERT(iterator <= TYPE_SECTION_END(secname),\
118 "unexpected list end location"); \
119 iterator < TYPE_SECTION_END(secname); \
120 }); \
121 iterator++)
122
135#define TYPE_SECTION_FOREACH_REVERSE(type, secname, iterator) \
136 TYPE_SECTION_START_EXTERN(type, secname); \
137 TYPE_SECTION_END_EXTERN(type, secname); \
138 for (type *iterator = TYPE_SECTION_END(secname); \
139 (uintptr_t)iterator > (uintptr_t)TYPE_SECTION_START(secname) && \
140 (iterator = (type *)((uintptr_t)iterator - sizeof(type)), true); \
141 )
142
153#define TYPE_SECTION_GET(type, secname, i, dst) do { \
154 TYPE_SECTION_START_EXTERN(type, secname); \
155 *(dst) = &TYPE_SECTION_START(secname)[i]; \
156} while (0)
157
165#define TYPE_SECTION_COUNT(type, secname, dst) do { \
166 TYPE_SECTION_START_EXTERN(type, secname); \
167 TYPE_SECTION_END_EXTERN(type, secname); \
168 *(dst) = ((uintptr_t)TYPE_SECTION_END(secname) - \
169 (uintptr_t)TYPE_SECTION_START(secname)) / sizeof(type); \
170} while (0)
171
177#define STRUCT_SECTION_START(struct_type) \
178 TYPE_SECTION_START(struct_type)
179
187#define STRUCT_SECTION_START_EXTERN(struct_type) \
188 TYPE_SECTION_START_EXTERN(struct struct_type, struct_type)
189
195#define STRUCT_SECTION_END(struct_type) \
196 TYPE_SECTION_END(struct_type)
197
205#define STRUCT_SECTION_END_EXTERN(struct_type) \
206 TYPE_SECTION_END_EXTERN(struct struct_type, struct_type)
207
216#define STRUCT_SECTION_ITERABLE_ALTERNATE(secname, struct_type, varname) \
217 TYPE_SECTION_ITERABLE(struct struct_type, varname, secname, varname)
218
225#define STRUCT_SECTION_ITERABLE_ARRAY_ALTERNATE(secname, struct_type, varname, \
226 size) \
227 TYPE_SECTION_ITERABLE(struct struct_type, varname[size], secname, \
228 varname)
229
244#define STRUCT_SECTION_ITERABLE(struct_type, varname) \
245 STRUCT_SECTION_ITERABLE_ALTERNATE(struct_type, struct_type, varname)
246
252#define STRUCT_SECTION_ITERABLE_ARRAY(struct_type, varname, size) \
253 STRUCT_SECTION_ITERABLE_ARRAY_ALTERNATE(struct_type, struct_type, \
254 varname, size)
255
262#define STRUCT_SECTION_ITERABLE_NAMED(struct_type, name, varname) \
263 TYPE_SECTION_ITERABLE(struct struct_type, varname, struct_type, name)
264
272#define STRUCT_SECTION_ITERABLE_NAMED_ALTERNATE(struct_type, secname, name, varname) \
273 TYPE_SECTION_ITERABLE(struct struct_type, varname, secname, name)
274
285#define STRUCT_SECTION_FOREACH_ALTERNATE(secname, struct_type, iterator) \
286 TYPE_SECTION_FOREACH(struct struct_type, secname, iterator)
287
298#define STRUCT_SECTION_FOREACH(struct_type, iterator) \
299 STRUCT_SECTION_FOREACH_ALTERNATE(struct_type, struct_type, iterator)
300
313#define STRUCT_SECTION_FOREACH_ALTERNATE_REVERSE(secname, struct_type, iterator) \
314 TYPE_SECTION_FOREACH_REVERSE(struct struct_type, secname, iterator)
315
327#define STRUCT_SECTION_FOREACH_REVERSE(struct_type, iterator) \
328 STRUCT_SECTION_FOREACH_ALTERNATE_REVERSE(struct_type, struct_type, iterator)
329
339#define STRUCT_SECTION_GET(struct_type, i, dst) \
340 TYPE_SECTION_GET(struct struct_type, struct_type, i, dst)
341
348#define STRUCT_SECTION_COUNT(struct_type, dst) \
349 TYPE_SECTION_COUNT(struct struct_type, struct_type, dst);
350 /* end of struct_section_apis */
354
355#ifdef __cplusplus
356}
357#endif
358
359#endif /* ZEPHYR_INCLUDE_SYS_ITERABLE_SECTIONS_H_ */
Macros to abstract toolchain specific capabilities.