Skip to content
Snippets Groups Projects
Unverified Commit 91c65e40 authored by Martine Lenders's avatar Martine Lenders Committed by GitHub
Browse files

Merge pull request #9327 from cgundogan/pr/tlsf_total_sizes

tlsf: add custom walker to get total free / used sizes
parents 8fd85e1c 00092130
No related branches found
No related tags found
No related merge requests found
...@@ -43,6 +43,28 @@ ...@@ -43,6 +43,28 @@
extern "C" { extern "C" {
#endif #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. * Add an area of memory to the global allocator pool.
* *
......
...@@ -63,6 +63,18 @@ tlsf_t *_tlsf_get_global_control(void) ...@@ -63,6 +63,18 @@ tlsf_t *_tlsf_get_global_control(void)
return gheap; 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" * Allocate a block of size "bytes"
*/ */
......
...@@ -147,7 +147,10 @@ void ps(void) ...@@ -147,7 +147,10 @@ void ps(void)
overall_stacksz, overall_used); overall_stacksz, overall_used);
# ifdef MODULE_TLSF_MALLOC # ifdef MODULE_TLSF_MALLOC
puts("\nHeap usage:"); 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
#endif #endif
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment