Skip to content

Commit 23802ef

Browse files
committed
alloc: add functions to use a private heap
Add sof_heap_alloc() and sof_heap_free() to allocate and free memory on a private heap. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 310b58f commit 23802ef

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

zephyr/include/rtos/alloc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ void rfree(void *ptr);
106106
*/
107107
void l3_heap_save(void);
108108

109+
void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
110+
size_t alignment);
111+
void sof_heap_free(struct k_heap *heap, void *addr);
112+
109113
/* TODO: remove - debug only - only needed for linking */
110114
static inline void heap_trace_all(int force) {}
111115

zephyr/lib/alloc.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ static void __sparse_cache *heap_alloc_aligned_cached(struct k_heap *h,
353353
*/
354354
#ifdef CONFIG_SOF_ZEPHYR_HEAP_CACHED
355355
min_align = MAX(PLATFORM_DCACHE_ALIGN, min_align);
356-
bytes = ALIGN_UP(bytes, min_align);
356+
bytes = ALIGN_UP(bytes, PLATFORM_DCACHE_ALIGN);
357357
#endif
358358

359359
ptr = (__sparse_force void __sparse_cache *)heap_alloc_aligned(h, min_align, bytes);
@@ -533,6 +533,30 @@ void rfree(void *ptr)
533533
}
534534
EXPORT_SYMBOL(rfree);
535535

536+
/*
537+
* To match the fall-back SOF main heap all private heaps should also be in the
538+
* uncached address range.
539+
*/
540+
void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
541+
size_t alignment)
542+
{
543+
if (!heap)
544+
heap = &sof_heap;
545+
546+
if (flags & SOF_MEM_FLAG_COHERENT)
547+
return heap_alloc_aligned(heap, alignment, bytes);
548+
549+
return (__sparse_force void *)heap_alloc_aligned_cached(heap, alignment, bytes);
550+
}
551+
552+
void sof_heap_free(struct k_heap *heap, void *addr)
553+
{
554+
if (!heap)
555+
heap = &sof_heap;
556+
557+
heap_free(heap, addr);
558+
}
559+
536560
static int heap_init(void)
537561
{
538562
sys_heap_init(&sof_heap.heap, heapmem, HEAPMEM_SIZE);

0 commit comments

Comments
 (0)