Commit e684ed9f authored by Christopher Friedt's avatar Christopher Friedt Committed by Carles Cufi
Browse files

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: default avatarChris Friedt <cfriedt@meta.com>
parent eef95536
Loading
Loading
Loading
Loading
+21 −8
Changes for include/zephyr/toolchain/common.h: 21 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -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.