From 01a49d3d194fb77eda33f4fc653522ce9b873288 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 5 Nov 2021 15:27:51 +0100 Subject: [PATCH] core/include/kernel_defines.h: add index_of() macro --- core/include/kernel_defines.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/include/kernel_defines.h b/core/include/kernel_defines.h index df26784ea1..f52830e669 100644 --- a/core/include/kernel_defines.h +++ b/core/include/kernel_defines.h @@ -63,6 +63,16 @@ extern "C" { ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))) #endif +/** + * @def index_of(ARRAY, ELEMENT) + * @brief Returns the index of a pointer to an array element. + + * @param[in] ARRAY an array + * @param[in] ELEMENT pointer to an array element + * @return Index of the element in the array + */ +#define index_of(ARRAY, ELEMENT) (((uintptr_t)(ELEMENT) - (uintptr_t)(ARRAY)) / sizeof(*(ELEMENT))) + /** * @def NORETURN * @brief The *NORETURN* keyword tells the compiler to assume that the function