kernel: add k_heap_array_get

Add `k_heap_array_get` as an alternative to `sys_heap_array_get`, which
only returns statically defined heaps (those defined with
`K_HEAP_DEFINE` or `K_HEAP_DEFINE_NOCACHE`), but doesn't depend on the
application guessing a value for `CONFIG_SYS_HEAP_ARRAY_SIZE`.

Signed-off-by: Jordan Yates <jordan@embeint.com>
This commit is contained in:
Jordan Yates 2025-06-16 14:42:18 +10:00 committed by Benjamin Cabé
commit 117b452b50
3 changed files with 23 additions and 0 deletions

View file

@ -132,6 +132,7 @@ New APIs and options
* :c:func:`timespec_normalize`
* :c:func:`timespec_from_timeout`
* :c:func:`timespec_to_timeout`
* :c:func:`k_heap_array_get`
* I2C

View file

@ -5854,6 +5854,17 @@ void k_heap_free(struct k_heap *h, void *mem) __attribute_nonnull(1);
#define K_HEAP_DEFINE_NOCACHE(name, bytes) \
Z_HEAP_DEFINE_IN_SECT(name, bytes, __nocache)
/** @brief Get the array of statically defined heaps
*
* Returns the pointer to the start of the static heap array.
* Static heaps are those declared through one of the `K_HEAP_DEFINE`
* macros.
*
* @param heap Pointer to location where heap array address is written
* @return Number of static heaps
*/
int k_heap_array_get(struct k_heap **heap);
/**
* @}
*/

View file

@ -12,6 +12,17 @@
#include <ksched.h>
#include <wait_q.h>
int k_heap_array_get(struct k_heap **heap)
{
int num;
/* Pointer to the start of the heap array */
STRUCT_SECTION_GET(k_heap, 0, heap);
/* Number of statically defined heaps */
STRUCT_SECTION_COUNT(k_heap, &num);
return num;
}
void k_heap_init(struct k_heap *heap, void *mem, size_t bytes)
{
z_waitq_init(&heap->wait_q);