Skip to content

Commit 1df93bc

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 d2210e3 commit 1df93bc

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

zephyr/include/rtos/alloc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ void rfree(void *ptr);
120120
*/
121121
void l3_heap_save(void);
122122

123+
void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
124+
size_t alignment);
125+
void sof_heap_free(struct k_heap *heap, void *addr);
126+
123127
/* TODO: remove - debug only - only needed for linking */
124128
static inline void heap_trace_all(int force) {}
125129

zephyr/lib/alloc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,30 @@ void rfree(void *ptr)
609609
}
610610
EXPORT_SYMBOL(rfree);
611611

612+
/*
613+
* To match the fall-back SOF main heap all private heaps should also be in the
614+
* uncached address range.
615+
*/
616+
void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
617+
size_t alignment)
618+
{
619+
if (!heap)
620+
heap = &sof_heap;
621+
622+
if (flags & SOF_MEM_FLAG_COHERENT)
623+
return heap_alloc_aligned(heap, alignment, bytes);
624+
625+
return (__sparse_force void *)heap_alloc_aligned_cached(heap, alignment, bytes);
626+
}
627+
628+
void sof_heap_free(struct k_heap *heap, void *addr)
629+
{
630+
if (!heap)
631+
heap = &sof_heap;
632+
633+
heap_free(heap, addr);
634+
}
635+
612636
static int heap_init(void)
613637
{
614638
sys_heap_init(&sof_heap.heap, heapmem, HEAPMEM_SIZE - SHARED_BUFFER_HEAP_MEM_SIZE);

0 commit comments

Comments
 (0)