diff --git a/pkg/tlsf/contrib/include/tlsf-malloc.h b/pkg/tlsf/contrib/include/tlsf-malloc.h index 48ef8952b8..2e9c15a45f 100644 --- a/pkg/tlsf/contrib/include/tlsf-malloc.h +++ b/pkg/tlsf/contrib/include/tlsf-malloc.h @@ -43,6 +43,28 @@ extern "C" { #endif +/** + * @brief Struct to hold the total sizes of free and used blocks + * Used for @ref tlsf_size_walker() + */ +typedef struct { + unsigned free; /**< total free size */ + unsigned used; /**< total used size */ +} tlsf_size_container_t; + +/** + * Walk the memory pool to print all block sizes and to calculate + * the total amount of free and used block sizes. + * + * @note This function is passed to tlsf_walk_pool() + * + * @param ptr Pointer to the current block. + * @param size Size of the current block at @p ptr. + * @param used Shows whether the current block is used or free. + * @param user Custom data expected to be of type ``pointer to tlsf_size_container_t`` + */ +void tlsf_size_walker(void* ptr, size_t size, int used, void* user); + /** * Add an area of memory to the global allocator pool. * diff --git a/pkg/tlsf/contrib/tlsf-malloc.c b/pkg/tlsf/contrib/tlsf-malloc.c index b62f2111bd..809e237221 100644 --- a/pkg/tlsf/contrib/tlsf-malloc.c +++ b/pkg/tlsf/contrib/tlsf-malloc.c @@ -63,6 +63,18 @@ tlsf_t *_tlsf_get_global_control(void) return gheap; } +void tlsf_size_walker(void* ptr, size_t size, int used, void* user) +{ + printf("\t%p %s size: %u (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, ptr); + + if (used) { + ((tlsf_size_container_t *)user)->used += (unsigned int)size; + } + else { + ((tlsf_size_container_t *)user)->free += (unsigned int)size; + } +} + /** * Allocate a block of size "bytes" */ diff --git a/sys/ps/ps.c b/sys/ps/ps.c index dd88059005..526d578ab4 100644 --- a/sys/ps/ps.c +++ b/sys/ps/ps.c @@ -147,7 +147,10 @@ void ps(void) overall_stacksz, overall_used); # ifdef MODULE_TLSF_MALLOC puts("\nHeap usage:"); - tlsf_walk_pool(tlsf_get_pool(_tlsf_get_global_control()), NULL, NULL); + tlsf_size_container_t sizes = { .free = 0, .used = 0 }; + tlsf_walk_pool(tlsf_get_pool(_tlsf_get_global_control()), tlsf_size_walker, &sizes); + printf("\tTotal free size: %u\n", sizes.free); + printf("\tTotal used size: %u\n", sizes.used); # endif #endif }