Skip to content

Commit 9ff0a7a

Browse files
softwareckilgirdwood
authored andcommitted
alloc: sof_heap: Add missing support for shared buffers in sof_heap_alloc
Add missing support for the SOF_MEM_FLAG_USER_SHARED_BUFFER flag in the sof_heap_alloc function. Add a generic function is_heap_pointer that allows checking whether a given pointer belongs to a given heap. It is used in the sof_heap_free function to ensure that the freed pointer belongs to the specified heap, and if not, to free it using rfree. Signed-off-by: Adrian Warecki <[email protected]>
1 parent c30f5bc commit 9ff0a7a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/audio/module_adapter/module_adapter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ static struct k_heap *module_adapter_dp_heap_new(const struct comp_ipc_config *c
7070
void *mod_heap_buf = mod_heap_mem + heap_prefix_size;
7171

7272
k_heap_init(mod_heap, mod_heap_buf, heap_size - heap_prefix_size);
73+
mod_heap->heap.init_mem = mod_heap_buf;
74+
mod_heap->heap.init_bytes = heap_size - heap_prefix_size;
7375

7476
return mod_heap;
7577
}

zephyr/lib/alloc.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ extern char _end[], _heap_sentry[];
155155

156156
static struct k_heap sof_heap;
157157

158+
/**
159+
* Checks whether pointer is from a given heap memory.
160+
* @param heap Pointer to a heap.
161+
* @param ptr Pointer to memory being checked.
162+
* @return True if pointer falls into heap memory region, false otherwise.
163+
*/
164+
static bool is_heap_pointer(const struct k_heap *heap, void *ptr)
165+
{
166+
uintptr_t heap_start =
167+
POINTER_TO_UINT(sys_cache_cached_ptr_get(heap->heap.init_mem));
168+
uintptr_t heap_end = heap_start + heap->heap.init_bytes;
169+
170+
if (!is_cached(ptr))
171+
ptr = (__sparse_force void *)sys_cache_cached_ptr_get(ptr);
172+
173+
return ((POINTER_TO_UINT(ptr) >= heap_start) &&
174+
(POINTER_TO_UINT(ptr) < heap_end));
175+
}
176+
158177
#if CONFIG_SOF_USERSPACE_USE_SHARED_HEAP
159178
static struct k_heap shared_buffer_heap;
160179

@@ -628,7 +647,7 @@ EXPORT_SYMBOL(rfree);
628647
void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
629648
size_t alignment)
630649
{
631-
if (flags & SOF_MEM_FLAG_LARGE_BUFFER)
650+
if (flags & (SOF_MEM_FLAG_LARGE_BUFFER | SOF_MEM_FLAG_USER_SHARED_BUFFER))
632651
return rballoc_align(flags, bytes, alignment);
633652

634653
if (!heap)
@@ -642,7 +661,7 @@ void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
642661

643662
void sof_heap_free(struct k_heap *heap, void *addr)
644663
{
645-
if (heap && addr)
664+
if (heap && addr && is_heap_pointer(heap, addr))
646665
heap_free(heap, addr);
647666
else
648667
rfree(addr);

0 commit comments

Comments
 (0)