Skip to content

Commit 06ea07b

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 5e815b5 commit 06ea07b

4 files changed

Lines changed: 22 additions & 13 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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,9 @@ EXPORT_SYMBOL(rfree);
618618
void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
619619
size_t alignment)
620620
{
621+
if (flags & SOF_MEM_FLAG_LARGE_BUFFER)
622+
return rballoc_align(flags, bytes, alignment);
623+
621624
if (!heap)
622625
heap = &sof_heap;
623626

@@ -629,10 +632,10 @@ void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
629632

630633
void sof_heap_free(struct k_heap *heap, void *addr)
631634
{
632-
if (!heap)
633-
heap = &sof_heap;
634-
635-
heap_free(heap, addr);
635+
if (heap && addr)
636+
heap_free(heap, addr);
637+
else
638+
rfree(addr);
636639
}
637640

638641
static int heap_init(void)

0 commit comments

Comments
 (0)