Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/lib/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,12 @@ static void free_block(void *ptr)
int i;
int block;

/* sanity check */
if (ptr == NULL)
return;

heap = get_heap_from_ptr(ptr);
if (heap == NULL)
if (!heap) {
trace_error(TRACE_CLASS_MEM, "invalid heap %p cpu %d",
(uintptr_t)ptr, cpu_get_id());
return;
}

/* find block that ptr belongs to */
for (i = 0; i < heap->blocks; i++) {
Expand All @@ -346,7 +345,8 @@ static void free_block(void *ptr)
}

/* not found */
trace_mem_error("eMF");
trace_error(TRACE_CLASS_MEM, "invalid ptr %p cpu %d",
(uintptr_t)ptr, cpu_get_id());
return;

found:
Expand Down Expand Up @@ -540,8 +540,25 @@ void *rballoc(int zone, uint32_t caps, size_t bytes)

void rfree(void *ptr)
{
struct mm_heap *cpu_heap;
uint32_t flags;

/* sanity check - NULL ptrs are fine */
if (!ptr)
return;

/* use the heap dedicated for the selected core */
cpu_heap = cache_to_uncache(memmap.system + cpu_get_id());

/* panic if pointer is from system heap */
if (ptr >= (void *)cpu_heap->heap &&
ptr <= (void *)cpu_heap->heap + cpu_heap->size) {
trace_error(TRACE_CLASS_MEM, "attempt to free system heap %p cpu %d",
(uintptr_t)ptr, cpu_get_id());
panic(SOF_IPC_PANIC_MEM);
}

/* free the block */
spin_lock_irq(&memmap.lock, flags);
free_block(ptr);
spin_unlock_irq(&memmap.lock, flags);
Expand Down