toolchain: common: iterable sections: add alternate foreach

Previously, it was possible to declare an iterable section using
the alternate form (where the `out_type` does not correspond
to the `struct_type`) but there was no convenient means to
iterate over them.

Add `STRUCT_SECTION_FOREACH_ALTERNATE()` to fill the gap.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
This commit is contained in:
Chris Friedt 2023-03-04 16:22:44 -05:00 committed by Carles Cufí
parent eef9553669
commit e684ed9f24

View file

@ -229,6 +229,26 @@
Z_DECL_ALIGN(struct struct_type) name \
__in_section(_##out_type, static, name) __used __noasan
/**
* @brief Iterate over a specified iterable section (alternate).
*
* @details
* Iterator for structure instances gathered by STRUCT_SECTION_ITERABLE().
* The linker must provide a _<out_type>_list_start symbol and a
* _<out_type>_list_end symbol to mark the start and the end of the
* list of struct objects to iterate over. This is normally done using
* ITERABLE_SECTION_ROM() or ITERABLE_SECTION_RAM() in the linker script.
*/
#define STRUCT_SECTION_FOREACH_ALTERNATE(out_type, struct_type, iterator) \
extern struct struct_type _CONCAT(_##out_type, _list_start)[]; \
extern struct struct_type _CONCAT(_##out_type, _list_end)[]; \
for (struct struct_type *iterator = _CONCAT(_##out_type, _list_start); ({ \
__ASSERT(iterator <= _CONCAT(_##out_type, _list_end), \
"unexpected list end location"); \
iterator < _CONCAT(_##out_type, _list_end); \
}); \
iterator++)
/**
* @brief Iterate over a specified iterable section.
*
@ -240,14 +260,7 @@
* ITERABLE_SECTION_ROM() or ITERABLE_SECTION_RAM() in the linker script.
*/
#define STRUCT_SECTION_FOREACH(struct_type, iterator) \
extern struct struct_type _CONCAT(_##struct_type, _list_start)[]; \
extern struct struct_type _CONCAT(_##struct_type, _list_end)[]; \
for (struct struct_type *iterator = \
_CONCAT(_##struct_type, _list_start); \
({ __ASSERT(iterator <= _CONCAT(_##struct_type, _list_end), \
"unexpected list end location"); \
iterator < _CONCAT(_##struct_type, _list_end); }); \
iterator++)
STRUCT_SECTION_FOREACH_ALTERNATE(struct_type, struct_type, iterator)
/**
* @brief Get element from section.