Skip to content

Commit 414e5e5

Browse files
committed
audio: module-adapter: use objpool for allocation containers
Switch to using object pool for module allocation containers. Unlike other use-cases we free all the pool entries when modules are freed. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 335fc5e commit 414e5e5

File tree

2 files changed

+42
-79
lines changed

2 files changed

+42
-79
lines changed

src/audio/module_adapter/module/generic.c

Lines changed: 39 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
#include <rtos/symbol.h>
15+
#include <sof/objpool.h>
1516
#include <sof/audio/module_adapter/module/generic.h>
1617
#include <sof/audio/data_blob.h>
1718
#include <sof/lib/fast-get.h>
@@ -81,10 +82,10 @@ int module_load_config(struct comp_dev *dev, const void *cfg, size_t size)
8182
void mod_resource_init(struct processing_module *mod)
8283
{
8384
struct module_data *md = &mod->priv;
85+
8486
/* Init memory list */
85-
list_init(&md->resources.res_list);
86-
list_init(&md->resources.free_cont_list);
87-
list_init(&md->resources.cont_chunk_list);
87+
list_init(&md->resources.objpool.list);
88+
md->resources.objpool.heap = md->resources.heap;
8889
md->resources.heap_usage = 0;
8990
md->resources.heap_high_water_mark = 0;
9091
}
@@ -141,43 +142,14 @@ int module_init(struct processing_module *mod)
141142
return 0;
142143
}
143144

144-
struct container_chunk {
145-
struct list_item chunk_list;
146-
struct module_resource containers[CONFIG_MODULE_MEMORY_API_CONTAINER_CHUNK_SIZE];
147-
};
148-
149145
static struct module_resource *container_get(struct processing_module *mod)
150146
{
151-
struct module_resources *res = &mod->priv.resources;
152-
struct k_heap *mod_heap = res->heap;
153-
struct module_resource *container;
154-
155-
if (list_is_empty(&res->free_cont_list)) {
156-
struct container_chunk *chunk = sof_heap_alloc(mod_heap, 0, sizeof(*chunk), 0);
157-
int i;
158-
159-
if (!chunk) {
160-
comp_err(mod->dev, "allocating more containers failed");
161-
return NULL;
162-
}
163-
164-
memset(chunk, 0, sizeof(*chunk));
165-
166-
list_item_append(&chunk->chunk_list, &res->cont_chunk_list);
167-
for (i = 0; i < ARRAY_SIZE(chunk->containers); i++)
168-
list_item_append(&chunk->containers[i].list, &res->free_cont_list);
169-
}
170-
171-
container = list_first_item(&res->free_cont_list, struct module_resource, list);
172-
list_item_del(&container->list);
173-
return container;
147+
return objpool_alloc(&mod->priv.resources.objpool, sizeof(struct module_resource), 0);
174148
}
175149

176150
static void container_put(struct processing_module *mod, struct module_resource *container)
177151
{
178-
struct module_resources *res = &mod->priv.resources;
179-
180-
list_item_append(&container->list, &res->free_cont_list);
152+
objpool_free(&mod->priv.resources.objpool, container);
181153
}
182154

183155
#if CONFIG_USERSPACE
@@ -235,7 +207,6 @@ void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignm
235207
container->ptr = ptr;
236208
container->size = size;
237209
container->type = MOD_RES_HEAP;
238-
list_item_prepend(&container->list, &res->res_list);
239210

240211
res->heap_usage += size;
241212
if (res->heap_usage > res->heap_high_water_mark)
@@ -286,7 +257,6 @@ void *z_impl_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t
286257
container->ptr = ptr;
287258
container->size = size;
288259
container->type = MOD_RES_HEAP;
289-
list_item_prepend(&container->list, &res->res_list);
290260

291261
res->heap_usage += size;
292262
if (res->heap_usage > res->heap_high_water_mark)
@@ -325,7 +295,6 @@ struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_modul
325295
container->bhp = bhp;
326296
container->size = 0;
327297
container->type = MOD_RES_BLOB_HANDLER;
328-
list_item_prepend(&container->list, &res->res_list);
329298

330299
return bhp;
331300
}
@@ -362,7 +331,6 @@ const void *z_impl_mod_fast_get(struct processing_module *mod, const void * cons
362331
container->sram_ptr = ptr;
363332
container->size = 0;
364333
container->type = MOD_RES_FAST_GET;
365-
list_item_prepend(&container->list, &res->res_list);
366334

367335
return ptr;
368336
}
@@ -394,6 +362,29 @@ static int free_contents(struct processing_module *mod, struct module_resource *
394362
return -EINVAL;
395363
}
396364

365+
struct mod_res_cb_arg {
366+
struct processing_module *mod;
367+
const void *ptr;
368+
};
369+
370+
static bool mod_res_free(void *data, void *arg)
371+
{
372+
struct mod_res_cb_arg *cb_arg = arg;
373+
struct module_resource *container = data;
374+
375+
if (cb_arg->ptr && container->ptr != cb_arg->ptr)
376+
return false;
377+
378+
int ret = free_contents(cb_arg->mod, container);
379+
380+
if (ret < 0)
381+
comp_err(cb_arg->mod->dev, "Cannot free allocation %p", cb_arg->ptr);
382+
383+
container_put(cb_arg->mod, container);
384+
385+
return true;
386+
}
387+
397388
/**
398389
* Frees the memory block removes it from module's book keeping.
399390
* @param mod Pointer to module this memory block was allocated for.
@@ -402,28 +393,19 @@ static int free_contents(struct processing_module *mod, struct module_resource *
402393
int z_impl_mod_free(struct processing_module *mod, const void *ptr)
403394
{
404395
struct module_resources *res = &mod->priv.resources;
405-
struct module_resource *container;
406-
struct list_item *res_list;
407396

408397
MEM_API_CHECK_THREAD(res);
409398
if (!ptr)
410399
return 0;
411400

412-
/* Find which container keeps this memory */
413-
list_for_item(res_list, &res->res_list) {
414-
container = container_of(res_list, struct module_resource, list);
415-
if (container->ptr == ptr) {
416-
int ret = free_contents(mod, container);
417-
418-
list_item_del(&container->list);
419-
container_put(mod, container);
420-
return ret;
421-
}
422-
}
401+
/* Find which container holds this memory */
402+
struct mod_res_cb_arg cb_arg = {mod, ptr};
403+
int ret = objpool_iterate(&mod->priv.resources.objpool, mod_res_free, &cb_arg);
423404

424-
comp_err(mod->dev, "error: could not find memory pointed by %p", ptr);
405+
if (ret < 0)
406+
comp_err(mod->dev, "error: could not find memory pointed by %p", ptr);
425407

426-
return -EINVAL;
408+
return ret;
427409
}
428410
EXPORT_SYMBOL(z_impl_mod_free);
429411

@@ -684,32 +666,14 @@ int module_reset(struct processing_module *mod)
684666
void mod_free_all(struct processing_module *mod)
685667
{
686668
struct module_resources *res = &mod->priv.resources;
687-
struct k_heap *mod_heap = res->heap;
688-
struct list_item *list;
689-
struct list_item *_list;
690669

691670
MEM_API_CHECK_THREAD(res);
692-
/* Free all contents found in used containers */
693-
list_for_item(list, &res->res_list) {
694-
struct module_resource *container =
695-
container_of(list, struct module_resource, list);
696-
697-
free_contents(mod, container);
698-
}
699671

700-
/*
701-
* We do not need to remove the containers from res_list in
702-
* the loop above or go through free_cont_list as all the
703-
* containers are anyway freed in the loop below, and the list
704-
* heads are reinitialized when mod_resource_init() is called.
705-
*/
706-
list_for_item_safe(list, _list, &res->cont_chunk_list) {
707-
struct container_chunk *chunk =
708-
container_of(list, struct container_chunk, chunk_list);
672+
/* Free all contents found in used containers */
673+
struct mod_res_cb_arg cb_arg = {mod, NULL};
709674

710-
list_item_del(&chunk->chunk_list);
711-
sof_heap_free(mod_heap, chunk);
712-
}
675+
objpool_iterate(&res->objpool, mod_res_free, &cb_arg);
676+
objpool_prune(&res->objpool);
713677

714678
/* Make sure resource lists and accounting are reset */
715679
mod_resource_init(mod);

src/include/sof/audio/module_adapter/module/generic.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
#ifndef __SOF_AUDIO_MODULE_GENERIC__
1414
#define __SOF_AUDIO_MODULE_GENERIC__
1515

16-
#include <sof/audio/component.h>
16+
#include <sof/objpool.h>
1717
#include <sof/ut.h>
18+
#include <sof/audio/component.h>
1819
#include <sof/audio/sink_api.h>
1920
#include <sof/audio/source_api.h>
2021
#include "module_interface.h"
@@ -128,9 +129,7 @@ struct module_param {
128129
* when the module unloads.
129130
*/
130131
struct module_resources {
131-
struct list_item res_list; /**< Allocad resource containers */
132-
struct list_item free_cont_list; /**< Unused memory containers */
133-
struct list_item cont_chunk_list; /**< Memory container chunks */
132+
struct objpool_head objpool;
134133
size_t heap_usage;
135134
size_t heap_high_water_mark;
136135
struct k_heap *heap;

0 commit comments

Comments
 (0)