Skip to content

Commit 275226e

Browse files
committed
module-adapter: move mod_alloc() allocations to the module heap
Move mod_alloc() allocations, including the container pool, to the module local heap. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 53128eb commit 275226e

4 files changed

Lines changed: 33 additions & 9 deletions

File tree

posix/include/rtos/alloc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#define SOF_MEM_FLAG_USER BIT(8)
5555
/** \brief Indicates that if we should return shared user memory address. */
5656
#define SOF_MEM_FLAG_USER_SHARED_BUFFER BIT(9)
57+
/** \brief Use allocation method for large buffers. */
58+
#define SOF_MEM_FLAG_LARGE_BUFFER BIT(10)
5759

5860
/** @} */
5961

src/audio/module_adapter/module/generic.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,20 @@ struct container_chunk {
137137
static struct module_resource *container_get(struct processing_module *mod)
138138
{
139139
struct module_resources *res = &mod->priv.resources;
140+
struct k_heap *mod_heap = res->heap;
140141
struct module_resource *container;
141142

142143
if (list_is_empty(&res->free_cont_list)) {
143-
struct container_chunk *chunk = rzalloc(SOF_MEM_FLAG_USER, sizeof(*chunk));
144+
struct container_chunk *chunk = sof_heap_alloc(mod_heap, 0, sizeof(*chunk), 0);
144145
int i;
145146

146147
if (!chunk) {
147148
comp_err(mod->dev, "allocating more containers failed");
148149
return NULL;
149150
}
150151

152+
memset(chunk, 0, sizeof(*chunk));
153+
151154
list_item_append(&chunk->chunk_list, &res->cont_chunk_list);
152155
for (i = 0; i < ARRAY_SIZE(chunk->containers); i++)
153156
list_item_append(&chunk->containers[i].list, &res->free_cont_list);
@@ -180,7 +183,6 @@ void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignm
180183
{
181184
struct module_resources *res = &mod->priv.resources;
182185
struct module_resource *container;
183-
void *ptr;
184186

185187
MEM_API_CHECK_THREAD(res);
186188

@@ -195,7 +197,8 @@ void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignm
195197
}
196198

197199
/* Allocate buffer memory for module */
198-
ptr = rballoc_align(SOF_MEM_FLAG_USER, size, alignment);
200+
void *ptr = sof_heap_alloc(res->heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_LARGE_BUFFER,
201+
size, alignment);
199202

200203
if (!ptr) {
201204
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
@@ -231,7 +234,6 @@ void *mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t size,
231234
{
232235
struct module_resources *res = &mod->priv.resources;
233236
struct module_resource *container;
234-
void *ptr;
235237

236238
MEM_API_CHECK_THREAD(res);
237239

@@ -246,7 +248,7 @@ void *mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t size,
246248
}
247249

248250
/* Allocate memory for module */
249-
ptr = rmalloc_align(flags, size, alignment);
251+
void *ptr = sof_heap_alloc(res->heap, flags, size, alignment);
250252

251253
if (!ptr) {
252254
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
@@ -276,8 +278,7 @@ EXPORT_SYMBOL(mod_alloc_ext);
276278
* Like comp_data_blob_handler_new() but the handler is automatically freed.
277279
*/
278280
#if CONFIG_COMP_BLOB
279-
struct comp_data_blob_handler *
280-
mod_data_blob_handler_new(struct processing_module *mod)
281+
struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_module *mod)
281282
{
282283
struct module_resources *res = &mod->priv.resources;
283284
struct comp_data_blob_handler *bhp;
@@ -347,7 +348,7 @@ static int free_contents(struct processing_module *mod, struct module_resource *
347348

348349
switch (container->type) {
349350
case MOD_RES_HEAP:
350-
rfree(container->ptr);
351+
sof_heap_free(res->heap, container->ptr);
351352
res->heap_usage -= container->size;
352353
return 0;
353354
#if CONFIG_COMP_BLOB
@@ -588,6 +589,7 @@ int module_reset(struct processing_module *mod)
588589
void mod_free_all(struct processing_module *mod)
589590
{
590591
struct module_resources *res = &mod->priv.resources;
592+
struct k_heap *mod_heap = res->heap;
591593
struct list_item *list;
592594
struct list_item *_list;
593595

@@ -611,7 +613,7 @@ void mod_free_all(struct processing_module *mod)
611613
container_of(list, struct container_chunk, chunk_list);
612614

613615
list_item_del(&chunk->chunk_list);
614-
rfree(chunk);
616+
sof_heap_free(mod_heap, chunk);
615617
}
616618

617619
/* Make sure resource lists and accounting are reset */

zephyr/include/rtos/alloc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#define SOF_MEM_FLAG_USER BIT(8)
4646
/** \brief Indicates that if we should return shared user memory address. */
4747
#define SOF_MEM_FLAG_USER_SHARED_BUFFER BIT(9)
48+
/** \brief Use allocation method for large buffers. */
49+
#define SOF_MEM_FLAG_LARGE_BUFFER BIT(10)
4850

4951
/** @} */
5052

zephyr/lib/alloc.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ size_t get_shared_buffer_heap_size(void)
183183
{
184184
return ROUND_DOWN(SHARED_BUFFER_HEAP_MEM_SIZE, HOST_PAGE_SIZE);
185185
}
186+
#else
187+
static bool is_shared_buffer_heap_pointer(void *ptr)
188+
{
189+
return false;
190+
}
186191
#endif /* CONFIG_SOF_USERSPACE_USE_SHARED_HEAP */
187192

188193
#if CONFIG_L3_HEAP
@@ -375,6 +380,11 @@ static int virtual_heap_init(void)
375380

376381
SYS_INIT(virtual_heap_init, POST_KERNEL, 1);
377382

383+
#else
384+
static bool is_virtual_heap_pointer(void *ptr)
385+
{
386+
return false;
387+
}
378388
#endif /* CONFIG_VIRTUAL_HEAP */
379389

380390
static void *heap_alloc_aligned(struct k_heap *h, size_t min_align, size_t bytes)
@@ -618,6 +628,9 @@ EXPORT_SYMBOL(rfree);
618628
void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
619629
size_t alignment)
620630
{
631+
if (flags & SOF_MEM_FLAG_LARGE_BUFFER)
632+
return rballoc_align(flags, bytes, alignment);
633+
621634
if (!heap)
622635
heap = &sof_heap;
623636

@@ -629,6 +642,11 @@ void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
629642

630643
void sof_heap_free(struct k_heap *heap, void *addr)
631644
{
645+
if (is_virtual_heap_pointer(addr) || is_shared_buffer_heap_pointer(addr)) {
646+
rfree(addr);
647+
return;
648+
}
649+
632650
if (!heap)
633651
heap = &sof_heap;
634652

0 commit comments

Comments
 (0)