From 39cc10b893bd0d4e23eaa16900014b9ad5589dec Mon Sep 17 00:00:00 2001 From: Adrian Warecki Date: Thu, 11 Dec 2025 16:51:01 +0100 Subject: [PATCH 1/2] zephyr: alloc: Use generic is_heap_pointer for shared heap Replace the is_shared_buffer_heap_pointer function with the generic is_heap_pointer for shared heap. This change simplifies the memory handling path by relying on a common utility instead of a specialized function. Signed-off-by: Adrian Warecki --- zephyr/lib/alloc.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 9be37f93c10e..877619d72bab 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -177,17 +177,6 @@ static bool is_heap_pointer(const struct k_heap *heap, void *ptr) #if CONFIG_SOF_USERSPACE_USE_SHARED_HEAP static struct k_heap shared_buffer_heap; -static bool is_shared_buffer_heap_pointer(void *ptr) -{ - uintptr_t shd_heap_start = POINTER_TO_UINT(shared_heapmem); - uintptr_t shd_heap_end = POINTER_TO_UINT(shared_heapmem + SHARED_BUFFER_HEAP_MEM_SIZE); - - if (sys_cache_is_ptr_cached(ptr)) - ptr = sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)ptr); - - return (POINTER_TO_UINT(ptr) >= shd_heap_start) && (POINTER_TO_UINT(ptr) < shd_heap_end); -} - /** * Returns the start of HPSRAM Shared memory heap. * @return Pointer to the HPSRAM Shared memory location which can be used @@ -634,7 +623,7 @@ void rfree(void *ptr) #endif #if CONFIG_SOF_USERSPACE_USE_SHARED_HEAP - if (is_shared_buffer_heap_pointer(ptr)) { + if (is_heap_pointer(&shared_buffer_heap, ptr)) { heap_free(&shared_buffer_heap, ptr); return; } @@ -676,6 +665,8 @@ static int heap_init(void) sys_heap_init(&sof_heap.heap, heapmem, HEAPMEM_SIZE - SHARED_BUFFER_HEAP_MEM_SIZE); #if CONFIG_SOF_USERSPACE_USE_SHARED_HEAP + shared_buffer_heap.heap.init_mem = shared_heapmem; + shared_buffer_heap.heap.init_bytes = SHARED_BUFFER_HEAP_MEM_SIZE; sys_heap_init(&shared_buffer_heap.heap, shared_heapmem, SHARED_BUFFER_HEAP_MEM_SIZE); #endif From 78f0fd5bf119ef07bc83c1717254894c30d75fff Mon Sep 17 00:00:00 2001 From: Adrian Warecki Date: Thu, 11 Dec 2025 16:59:25 +0100 Subject: [PATCH 2/2] zephyr: alloc: Use generic is_heap_pointer for L3 heap Replace the is_l3_heap_pointer function with the generic is_heap_pointer for L3 heap. This change simplifies the memory handling path by relying on a common utility instead of a specialized function. Signed-off-by: Adrian Warecki --- zephyr/lib/alloc.c | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 877619d72bab..8a2fed79ee41 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -244,22 +244,6 @@ void l3_heap_save(void) get_l3_heap_size()); } -/** - * Checks whether pointer is from L3 heap memory range. - * @param ptr Pointer to memory being checked. - * @return True if pointer falls into L3 heap region, false otherwise. - */ -static bool is_l3_heap_pointer(void *ptr) -{ - uintptr_t l3_heap_start = get_l3_heap_start(); - uintptr_t l3_heap_end = l3_heap_start + get_l3_heap_size(); - - if ((POINTER_TO_UINT(ptr) >= l3_heap_start) && (POINTER_TO_UINT(ptr) < l3_heap_end)) - return true; - - return false; -} - static void *l3_heap_alloc_aligned(struct k_heap *h, size_t min_align, size_t bytes) { k_spinlock_key_t key; @@ -609,7 +593,7 @@ void rfree(void *ptr) return; #if CONFIG_L3_HEAP - if (is_l3_heap_pointer(ptr)) { + if (is_heap_pointer(&l3_heap, ptr)) { l3_heap_free(&l3_heap, ptr); return; } @@ -680,10 +664,13 @@ static int heap_init(void) arch_mem_map(l3_heap_start, va, l3_heap_size, K_MEM_PERM_RW | K_MEM_CACHE_WB); #endif - if (l3_heap_copy.heap.heap) + if (l3_heap_copy.heap.heap) { l3_heap = l3_heap_copy; - else + } else { + l3_heap.heap.init_mem = l3_heap_start; + l3_heap.heap.init_bytes = l3_heap_size; sys_heap_init(&l3_heap.heap, l3_heap_start, l3_heap_size); + } } #endif