From 10490387b4fda1a43d68bdf2113f623060119cdc Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Mon, 30 Aug 2021 10:36:59 -0700 Subject: [PATCH] kernel: kheap: introduce K_HEAP_DEFINE_NOCACHE() This allows a kheap to be defined in the uncached memory. For example, this can be used for DMA transfer buffers. Signed-off-by: Daniel Leung --- include/kernel.h | 55 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/include/kernel.h b/include/kernel.h index e07be5a78a5..3a53f480c11 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -5023,6 +5023,33 @@ void k_heap_free(struct k_heap *h, void *mem); */ #define Z_HEAP_MIN_SIZE (sizeof(void *) > 4 ? 56 : 44) +/** + * @brief Define a static k_heap in the specified linker section + * + * This macro defines and initializes a static memory region and + * k_heap of the requested size in the specified linker section. + * After kernel start, &name can be used as if k_heap_init() had + * been called. + * + * Note that this macro enforces a minimum size on the memory region + * to accommodate metadata requirements. Very small heaps will be + * padded to fit. + * + * @param name Symbol name for the struct k_heap object + * @param bytes Size of memory region, in bytes + * @param in_section __attribute__((section(name)) + */ +#define Z_HEAP_DEFINE_IN_SECT(name, bytes, in_section) \ + char in_section \ + __aligned(8) /* CHUNK_UNIT */ \ + kheap_##name[MAX(bytes, Z_HEAP_MIN_SIZE)]; \ + STRUCT_SECTION_ITERABLE(k_heap, name) = { \ + .heap = { \ + .init_mem = kheap_##name, \ + .init_bytes = MAX(bytes, Z_HEAP_MIN_SIZE), \ + }, \ + } + /** * @brief Define a static k_heap * @@ -5038,15 +5065,25 @@ void k_heap_free(struct k_heap *h, void *mem); * @param bytes Size of memory region, in bytes */ #define K_HEAP_DEFINE(name, bytes) \ - char __noinit_named(kheap_buf_##name) \ - __aligned(8) /* CHUNK_UNIT */ \ - kheap_##name[MAX(bytes, Z_HEAP_MIN_SIZE)]; \ - STRUCT_SECTION_ITERABLE(k_heap, name) = { \ - .heap = { \ - .init_mem = kheap_##name, \ - .init_bytes = MAX(bytes, Z_HEAP_MIN_SIZE), \ - }, \ - } + Z_HEAP_DEFINE_IN_SECT(name, bytes, \ + __noinit_named(kheap_buf_##name)) + +/** + * @brief Define a static k_heap in uncached memory + * + * This macro defines and initializes a static memory region and + * k_heap of the requested size in uncache memory. After kernel + * start, &name can be used as if k_heap_init() had been called. + * + * Note that this macro enforces a minimum size on the memory region + * to accommodate metadata requirements. Very small heaps will be + * padded to fit. + * + * @param name Symbol name for the struct k_heap object + * @param bytes Size of memory region, in bytes + */ +#define K_HEAP_DEFINE_NOCACHE(name, bytes) \ + Z_HEAP_DEFINE_IN_SECT(name, bytes, __nocache) /** * @}