13#ifndef ZEPHYR_INCLUDE_SYS_ITERABLE_SECTIONS_H_
14#define ZEPHYR_INCLUDE_SYS_ITERABLE_SECTIONS_H_
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
63#define TYPE_SECTION_START(secname) _CONCAT(_##secname, _list_start)
73#define TYPE_SECTION_END(secname) _CONCAT(_##secname, _list_end)
86#define TYPE_SECTION_START_EXTERN(type, secname) \
87 extern type TYPE_SECTION_START(secname)[]
100#define TYPE_SECTION_END_EXTERN(type, secname) \
101 extern type TYPE_SECTION_END(secname)[]
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); \
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); \
153#define TYPE_SECTION_GET(type, secname, i, dst) do { \
154 TYPE_SECTION_START_EXTERN(type, secname); \
155 *(dst) = &TYPE_SECTION_START(secname)[i]; \
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); \
177#define STRUCT_SECTION_START(struct_type) \
178 TYPE_SECTION_START(struct_type)
187#define STRUCT_SECTION_START_EXTERN(struct_type) \
188 TYPE_SECTION_START_EXTERN(struct struct_type, struct_type)
195#define STRUCT_SECTION_END(struct_type) \
196 TYPE_SECTION_END(struct_type)
205#define STRUCT_SECTION_END_EXTERN(struct_type) \
206 TYPE_SECTION_END_EXTERN(struct struct_type, struct_type)
216#define STRUCT_SECTION_ITERABLE_ALTERNATE(secname, struct_type, varname) \
217 TYPE_SECTION_ITERABLE(struct struct_type, varname, secname, varname)
225#define STRUCT_SECTION_ITERABLE_ARRAY_ALTERNATE(secname, struct_type, varname, \
227 TYPE_SECTION_ITERABLE(struct struct_type, varname[size], secname, \
244#define STRUCT_SECTION_ITERABLE(struct_type, varname) \
245 STRUCT_SECTION_ITERABLE_ALTERNATE(struct_type, struct_type, varname)
252#define STRUCT_SECTION_ITERABLE_ARRAY(struct_type, varname, size) \
253 STRUCT_SECTION_ITERABLE_ARRAY_ALTERNATE(struct_type, struct_type, \
262#define STRUCT_SECTION_ITERABLE_NAMED(struct_type, name, varname) \
263 TYPE_SECTION_ITERABLE(struct struct_type, varname, struct_type, name)
272#define STRUCT_SECTION_ITERABLE_NAMED_ALTERNATE(struct_type, secname, name, varname) \
273 TYPE_SECTION_ITERABLE(struct struct_type, varname, secname, name)
285#define STRUCT_SECTION_FOREACH_ALTERNATE(secname, struct_type, iterator) \
286 TYPE_SECTION_FOREACH(struct struct_type, secname, iterator)
298#define STRUCT_SECTION_FOREACH(struct_type, iterator) \
299 STRUCT_SECTION_FOREACH_ALTERNATE(struct_type, struct_type, iterator)
313#define STRUCT_SECTION_FOREACH_ALTERNATE_REVERSE(secname, struct_type, iterator) \
314 TYPE_SECTION_FOREACH_REVERSE(struct struct_type, secname, iterator)
327#define STRUCT_SECTION_FOREACH_REVERSE(struct_type, iterator) \
328 STRUCT_SECTION_FOREACH_ALTERNATE_REVERSE(struct_type, struct_type, iterator)
339#define STRUCT_SECTION_GET(struct_type, i, dst) \
340 TYPE_SECTION_GET(struct struct_type, struct_type, i, dst)
348#define STRUCT_SECTION_COUNT(struct_type, dst) \
349 TYPE_SECTION_COUNT(struct struct_type, struct_type, dst);