Browse Source
To request heap statistics, a pointer to a heap structure is required. This is straightforward for a user-defined heap. However, such a pointer is not known for heaps created by other components or libraries, like libc. Therefore, it is not possible to calculate the total heap memory. The proposed solution is to use an array of pointers, which is filled in on every sys_heap_init() call. One can then iterate through it to sum up the total memory allocated for all heaps. The array size is configurable. The default array size is zero, which means the feature is disabled. Any other integer greater then zero defines the array size and enables the feature. A list of pointers instead of an array could be another approach, but it requeres a heap, which is not always available. Signed-off-by: Ivan Pankratov <ivan.pankratov@silabs.com>pull/84195/head
7 changed files with 98 additions and 7 deletions
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Silicon Laboratories Inc. www.silabs.com |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
#include <zephyr/sys/sys_heap.h> |
||||
#include <zephyr/kernel.h> |
||||
|
||||
static size_t i; |
||||
static struct sys_heap *heaps[CONFIG_SYS_HEAP_ARRAY_SIZE]; |
||||
|
||||
int sys_heap_array_save(struct sys_heap *heap) |
||||
{ |
||||
if (heap == NULL) { |
||||
return -EINVAL; |
||||
} |
||||
|
||||
if (i < CONFIG_SYS_HEAP_ARRAY_SIZE) { |
||||
heaps[i++] = heap; |
||||
} else { |
||||
return -EINVAL; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int sys_heap_array_get(struct sys_heap ***heap) |
||||
{ |
||||
if (heap == NULL) { |
||||
return -EINVAL; |
||||
} |
||||
|
||||
*heap = heaps; |
||||
|
||||
return i; |
||||
} |
@ -1 +1,2 @@
@@ -1 +1,2 @@
|
||||
CONFIG_SYS_HEAP_RUNTIME_STATS=y |
||||
CONFIG_SYS_HEAP_ARRAY_SIZE=4 |
||||
|
Loading…
Reference in new issue