From a2ce2e9cfba9e4050d5de6b741899cbf9b05ad57 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Fri, 15 Jan 2021 11:25:03 +0100 Subject: [PATCH] memarray: Add memarray_available function --- sys/include/memarray.h | 11 ++++++++++- sys/memarray/memarray.c | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/sys/include/memarray.h b/sys/include/memarray.h index 68598c80ac..4d75b96c04 100644 --- a/sys/include/memarray.h +++ b/sys/include/memarray.h @@ -20,7 +20,7 @@ #include #include -#include +#include #include #ifdef __cplusplus @@ -110,6 +110,15 @@ static inline void memarray_free(memarray_t *mem, void *ptr) mem->free_data = ptr; } +/** + * @brief Returns the number of blocks available + * + * @param[in] mem memarray pool + * + * @returns Number of elements available in the memarray pool + */ +size_t memarray_available(memarray_t *mem); + #ifdef __cplusplus } #endif diff --git a/sys/memarray/memarray.c b/sys/memarray/memarray.c index f9f2a24efb..e618951c77 100644 --- a/sys/memarray/memarray.c +++ b/sys/memarray/memarray.c @@ -31,3 +31,14 @@ void memarray_init(memarray_t *mem, void *data, size_t size, size_t num) memarray_free(mem, element); } } + +size_t memarray_available(memarray_t *mem) +{ + size_t num = 0; + void **element = &mem->free_data; + while (*element) { + element = (void**)*element; + num++; + } + return num; +}