From 4c9633c2aaa7271668669c14cb3be30a920eedeb Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Tue, 15 Feb 2022 10:17:04 +0100 Subject: [PATCH] toolchain: common: Extending macro set for memory sections Added macro for getting nth element from section. Added macro for counting elements in the section. Signed-off-by: Krzysztof Chruscinski --- include/zephyr/toolchain/common.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/include/zephyr/toolchain/common.h b/include/zephyr/toolchain/common.h index 5e851103781..a359697490c 100644 --- a/include/zephyr/toolchain/common.h +++ b/include/zephyr/toolchain/common.h @@ -243,6 +243,33 @@ iterator < _CONCAT(_##struct_type, _list_end); }); \ iterator++) +/** + * @brief Get element from section. + * + * @note There is no protection against reading beyond the section. + * + * @param[in] struct_type Struct type. + * @param[in] i Index. + * @param[out] dst Pointer to location where pointer to element is written. + */ +#define STRUCT_SECTION_GET(struct_type, i, dst) do { \ + extern struct struct_type _CONCAT(_##struct_type, _list_start)[]; \ + *(dst) = &_CONCAT(_##struct_type, _list_start)[i]; \ +} while (0) + +/** + * @brief Count elements in a section. + * + * @param[in] struct_type Struct type + * @param[out] dst Pointer to location where result is written. + */ +#define STRUCT_SECTION_COUNT(struct_type, dst) do { \ + extern struct struct_type _CONCAT(_##struct_type, _list_start)[]; \ + extern struct struct_type _CONCAT(_##struct_type, _list_end)[]; \ + *(dst) = ((uintptr_t)_CONCAT(_##struct_type, _list_end) - \ + (uintptr_t)_CONCAT(_##struct_type, _list_start)) / sizeof(struct struct_type); \ +} while (0) + /** * @} */ /* end of struct_section_apis */