From 4bd7e0bc94de479cccb7de6fae1f9287a769fe39 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Mon, 16 Jun 2025 14:53:41 +1000 Subject: [PATCH] samples: sys_heap: extend with `k_heap_array_get` Extend the sample with a demonstration of `k_heap_array_get`. Signed-off-by: Jordan Yates --- samples/basic/sys_heap/README.rst | 15 +++++++++----- samples/basic/sys_heap/src/main.c | 33 +++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/samples/basic/sys_heap/README.rst b/samples/basic/sys_heap/README.rst index 74eb2b9e332..e6d6a11e6fb 100644 --- a/samples/basic/sys_heap/README.rst +++ b/samples/basic/sys_heap/README.rst @@ -33,9 +33,14 @@ Sample Output .. code-block:: console - System heap sample + System heap sample - allocated 0, free 196, max allocated 0, heap size 256 - allocated 156, free 36, max allocated 156, heap size 256 - allocated 100, free 92, max allocated 156, heap size 256 - allocated 0, free 196, max allocated 156, heap size 256 + allocated 0, free 196, max allocated 0, heap size 256 + allocated 156, free 36, max allocated 156, heap size 256 + allocated 100, free 92, max allocated 156, heap size 256 + allocated 0, free 196, max allocated 156, heap size 256 + 1 static heap(s) allocated: + 0 - address 0x80552a8 allocated 12, free 180, max allocated 12, heap size 256 + 2 heap(s) allocated (including static): + 0 - address 0x80552a8 allocated 12, free 180, max allocated 12, heap size 256 + 1 - address 0x805530c allocated 0, free 196, max allocated 156, heap size 256 diff --git a/samples/basic/sys_heap/src/main.c b/samples/basic/sys_heap/src/main.c index e59ab749283..7bf186f5f3a 100644 --- a/samples/basic/sys_heap/src/main.c +++ b/samples/basic/sys_heap/src/main.c @@ -15,6 +15,7 @@ static char heap_mem[HEAP_SIZE]; static struct sys_heap heap; static void print_sys_memory_stats(struct sys_heap *); +static void print_static_heaps(void); static void print_all_heaps(void); int main(void) @@ -35,7 +36,17 @@ int main(void) sys_heap_free(&heap, p); print_sys_memory_stats(&heap); + /* Allocate from our custom k_heap to make the output + * more interesting and prevent compiler optimisations from + * discarding it. + */ + p = k_heap_alloc(&my_kernel_heap, 10, K_FOREVER); + + print_static_heaps(); print_all_heaps(); + + /* Cleanup memory */ + k_heap_free(&my_kernel_heap, p); return 0; } @@ -50,16 +61,30 @@ static void print_sys_memory_stats(struct sys_heap *hp) stats.max_allocated_bytes, HEAP_SIZE); } +static void print_static_heaps(void) +{ + struct k_heap *ha; + int n; + + n = k_heap_array_get(&ha); + printk("%d static heap(s) allocated:\n", n); + + for (int i = 0; i < n; i++) { + printk("\t%d - address %p ", i, &ha[i]); + print_sys_memory_stats(&ha[i].heap); + } +} + static void print_all_heaps(void) { struct sys_heap **ha; - size_t i, n; + int n; n = sys_heap_array_get(&ha); - printk("There are %zu heaps allocated:\n", n); + printk("%d heap(s) allocated (including static):\n", n); - for (i = 0; i < n; i++) { - printk("\t%zu - address %p ", i, ha[i]); + for (int i = 0; i < n; i++) { + printk("\t%d - address %p ", i, ha[i]); print_sys_memory_stats(ha[i]); } }