Skip to content
Open
Show file tree
Hide file tree
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
50 changes: 11 additions & 39 deletions src/audio/buffers/comp_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <rtos/alloc.h>
#include <rtos/cache.h>
#include <sof/lib/vregion.h>
#include <sof/ctx_alloc.h>
#include <sof/list.h>
#include <sof/schedule/dp_schedule.h>
#include <rtos/spinlock.h>
Expand Down Expand Up @@ -158,10 +159,7 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer)

#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, buffer->stream.addr);
else
sof_heap_free(alloc->heap, buffer->stream.addr);
sof_ctx_free(alloc, buffer->stream.addr);
#else
rfree(buffer->stream.addr);
#endif
Expand Down Expand Up @@ -216,10 +214,8 @@ static struct comp_buffer *buffer_alloc_struct(struct mod_alloc_ctx *alloc,

if (!alloc || !alloc->vreg)
buffer = sof_heap_alloc(alloc ? alloc->heap : NULL, flags, sizeof(*buffer), 0);
else if (is_shared)
buffer = vregion_alloc_coherent(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer));
else
buffer = vregion_alloc(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer));
buffer = sof_ctx_alloc(alloc, flags, sizeof(*buffer), 0);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you wanted to remove lines 215-216 too

if (!buffer) {
tr_err(&buffer_tr, "could not alloc structure");
return NULL;
Expand Down Expand Up @@ -265,10 +261,7 @@ struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint3

#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
stream_addr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, size, align);
else
stream_addr = sof_heap_alloc(alloc->heap, flags, size, align);
stream_addr = sof_ctx_alloc(alloc, flags, size, align);
#else
stream_addr = rballoc_align(flags, size, align);
#endif
Expand All @@ -283,10 +276,7 @@ struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint3
tr_err(&buffer_tr, "could not alloc buffer structure");
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, stream_addr);
else
sof_heap_free(alloc->heap, stream_addr);
sof_ctx_free(alloc, stream_addr);
#else
rfree(stream_addr);
#endif
Expand Down Expand Up @@ -319,10 +309,7 @@ struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t prefe
for (size = preferred_size; size >= minimum_size; size -= minimum_size) {
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
stream_addr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, size, align);
else
stream_addr = sof_heap_alloc(alloc->heap, flags, size, align);
stream_addr = sof_ctx_alloc(alloc, flags, size, align);
#else
stream_addr = rballoc_align(flags, size, align);
#endif
Expand All @@ -343,10 +330,7 @@ struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t prefe
tr_err(&buffer_tr, "could not alloc buffer structure");
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, stream_addr);
else
sof_heap_free(alloc->heap, stream_addr);
sof_ctx_free(alloc, stream_addr);
#else
rfree(stream_addr);
#endif
Expand Down Expand Up @@ -387,10 +371,7 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen

#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
new_ptr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, size, alignment);
else
new_ptr = sof_heap_alloc(alloc->heap, buffer->flags, size, alignment);
new_ptr = sof_ctx_alloc(alloc, buffer->flags, size, alignment);
#else
new_ptr = rballoc_align(buffer->flags, size, alignment);
#endif
Expand All @@ -406,10 +387,7 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen
if (new_ptr) {
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, audio_stream_get_addr(&buffer->stream));
else
sof_heap_free(alloc->heap, audio_stream_get_addr(&buffer->stream));
sof_ctx_free(alloc, audio_stream_get_addr(&buffer->stream));
#else
rfree(audio_stream_get_addr(&buffer->stream));
#endif
Expand Down Expand Up @@ -451,10 +429,7 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz
new_size -= minimum_size) {
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
new_ptr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, new_size, alignment);
else
new_ptr = sof_heap_alloc(alloc->heap, buffer->flags, new_size, alignment);
new_ptr = sof_ctx_alloc(alloc, buffer->flags, new_size, alignment);
#else
new_ptr = rballoc_align(buffer->flags, new_size, alignment);
#endif
Expand All @@ -473,10 +448,7 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz
if (new_ptr) {
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, audio_stream_get_addr(&buffer->stream));
else
sof_heap_free(alloc->heap, audio_stream_get_addr(&buffer->stream));
sof_ctx_free(alloc, audio_stream_get_addr(&buffer->stream));
#else
rfree(audio_stream_get_addr(&buffer->stream));
#endif
Expand Down
31 changes: 7 additions & 24 deletions src/audio/buffers/ring_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sof/trace/trace.h>
#include <sof/lib/uuid.h>
#include <sof/lib/vregion.h>
#include <sof/ctx_alloc.h>

#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/ring_buffer.h>
Expand Down Expand Up @@ -99,13 +100,8 @@ static void ring_buffer_free(struct sof_audio_buffer *audio_buffer)
struct ring_buffer, audio_buffer);
struct mod_alloc_ctx *alloc = audio_buffer->alloc;

if (alloc->vreg) {
vregion_free(alloc->vreg, (__sparse_force void *)ring_buffer->_data_buffer);
vregion_free(alloc->vreg, ring_buffer);
} else {
sof_heap_free(alloc->heap, (__sparse_force void *)ring_buffer->_data_buffer);
sof_heap_free(alloc->heap, ring_buffer);
}
sof_ctx_free(alloc, (__sparse_force void *)ring_buffer->_data_buffer);
sof_ctx_free(alloc, ring_buffer);
}

static void ring_buffer_reset(struct sof_audio_buffer *audio_buffer)
Expand Down Expand Up @@ -296,17 +292,11 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl
struct ring_buffer *ring_buffer;
struct mod_alloc_ctx *alloc = dev->mod->priv.resources.alloc;
struct k_heap *heap = alloc->heap;
struct vregion *vreg = alloc->vreg;
int memory_flags = (is_shared ? SOF_MEM_FLAG_COHERENT : 0) |
user_get_buffer_memory_region(dev->drv);

/* allocate ring_buffer structure */
if (!vreg)
ring_buffer = sof_heap_alloc(heap, memory_flags, sizeof(*ring_buffer), 0);
else if (is_shared)
ring_buffer = vregion_alloc_coherent(vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*ring_buffer));
else
ring_buffer = vregion_alloc(vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*ring_buffer));
ring_buffer = sof_ctx_alloc(alloc, memory_flags, sizeof(*ring_buffer), 0);
if (!ring_buffer)
return NULL;

Expand Down Expand Up @@ -382,12 +372,8 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl

void *data_buf;

if (vreg)
data_buf = vregion_alloc_align(vreg, VREGION_MEM_TYPE_INTERIM, ring_buffer->data_buffer_size,
PLATFORM_DCACHE_ALIGN);
else
data_buf = sof_heap_alloc(heap, user_get_buffer_memory_region(dev->drv),
ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
data_buf = sof_ctx_alloc(alloc, user_get_buffer_memory_region(dev->drv),
ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);

if (!data_buf)
goto err;
Expand All @@ -402,9 +388,6 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl
return ring_buffer;
err:
tr_err(&ring_buffer_tr, "Ring buffer creation failure");
if (vreg)
vregion_free(vreg, ring_buffer);
else
sof_heap_free(heap, ring_buffer);
sof_ctx_free(alloc, ring_buffer);
return NULL;
}
17 changes: 3 additions & 14 deletions src/audio/module_adapter/module/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <sof/audio/data_blob.h>
#include <sof/lib/fast-get.h>
#include <sof/lib/vregion.h>
#include <sof/ctx_alloc.h>
#include <sof/schedule/dp_schedule.h>
#if CONFIG_IPC_MAJOR_4
#include <ipc4/header.h>
Expand Down Expand Up @@ -252,16 +253,7 @@ void *z_impl_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t
}

/* Allocate memory for module */
void *ptr;

if (!res->alloc->vreg)
ptr = sof_heap_alloc(res->alloc->heap, flags, size, alignment);
else if (flags & SOF_MEM_FLAG_COHERENT)
ptr = vregion_alloc_coherent_align(res->alloc->vreg, VREGION_MEM_TYPE_INTERIM,
size, alignment);
else
ptr = vregion_alloc_align(res->alloc->vreg, VREGION_MEM_TYPE_INTERIM,
size, alignment);
void *ptr = sof_ctx_alloc(res->alloc, flags, size, alignment);

if (!ptr) {
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
Expand Down Expand Up @@ -362,10 +354,7 @@ static int free_contents(struct processing_module *mod, struct module_resource *

switch (container->type) {
case MOD_RES_HEAP:
if (res->alloc->vreg)
vregion_free(res->alloc->vreg, container->ptr);
else
sof_heap_free(res->alloc->heap, container->ptr);
sof_ctx_free(res->alloc, container->ptr);
res->heap_usage -= container->size;
return 0;
#if CONFIG_COMP_BLOB
Expand Down
68 changes: 68 additions & 0 deletions src/include/sof/ctx_alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright(c) 2026 Intel Corporation. All rights reserved.
*/

#ifndef __SOF_CTX_ALLOC_H__
#define __SOF_CTX_ALLOC_H__

#include <sof/audio/component.h>
#include <sof/lib/vregion.h>
#include <rtos/alloc.h>
#include <stddef.h>
#include <stdint.h>

/**
* Allocate memory from a mod_alloc_ctx context.
*
* When the context has a vregion, allocates from the vregion interim
* partition. Coherent memory is used when SOF_MEM_FLAG_COHERENT is set
* in flags. Falls back to sof_heap_alloc() otherwise.
*
* @param ctx Allocation context (heap + optional vregion).
* @param flags Allocation flags (SOF_MEM_FLAG_*).
* @param size Size in bytes.
* @param alignment Required alignment in bytes.
* @return Pointer to allocated memory or NULL on failure.
*/
static inline void *sof_ctx_alloc(struct mod_alloc_ctx *ctx, uint32_t flags,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsarha @lyakh @lgirdwood My head spins a bit with the naming. We have been adding layers to heap allocation recently and it's starts to be very hard to follow:

  • rtos/alloc.h: rmalloc/rzalloc() and the new sof_heap_alloc()
  • sof/lib/vregion.h: vregion_alloc()
  • sof/lib/ctx_alloc.h: sof_ctx_alloc()

I wonder if we could come up with a better name for ctx_alloc.h? The main point seems to be capability to group heap allocs to region, so maybe:

  • sof_region_alloc() ? this would use vregion.h if available and fallback to sof_heap_alloc() otherwise

size_t size, size_t alignment)
{
if (!ctx || !ctx->vreg)
return sof_heap_alloc(ctx ? ctx->heap : NULL, flags, size, alignment);

if (flags & SOF_MEM_FLAG_COHERENT)
return vregion_alloc_coherent_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM,
size, alignment);

Comment on lines +34 to +37
return vregion_alloc_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM, size, alignment);
}

/**
* Allocate zero-initialized memory from a mod_alloc_ctx context.
* @param ctx Allocation context.
* @param size Size in bytes.
* @return Pointer to allocated memory or NULL on failure.
*/
static inline void *sof_ctx_zalloc(struct mod_alloc_ctx *ctx, size_t size)
{
return sof_ctx_alloc(ctx, 0, size, 0);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems rather wrong and incomplete. If you do want such a wrapper, you'd want it with flags and alignment too. And memset() of course

}
Comment on lines +41 to +50

/**
* Free memory allocated from a mod_alloc_ctx context.
* @param ctx Allocation context.
* @param ptr Pointer to free.
*/
static inline void sof_ctx_free(struct mod_alloc_ctx *ctx, void *ptr)
{
if (!ptr)
return;

if (ctx && ctx->vreg)
vregion_free(ctx->vreg, ptr);
else
sof_heap_free(ctx ? ctx->heap : NULL, ptr);
}

#endif /* __SOF_CTX_ALLOC_H__ */
Loading