1212 */
1313
1414#include <rtos/symbol.h>
15+ #include <sof/compiler_attributes.h>
16+ #include <sof/objpool.h>
1517#include <sof/audio/module_adapter/module/generic.h>
1618#include <sof/audio/data_blob.h>
1719#include <sof/lib/fast-get.h>
@@ -81,10 +83,10 @@ int module_load_config(struct comp_dev *dev, const void *cfg, size_t size)
8183void mod_resource_init (struct processing_module * mod )
8284{
8385 struct module_data * md = & mod -> priv ;
86+
8487 /* 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 );
88+ list_init (& md -> resources .objpool .list );
89+ md -> resources .objpool .heap = md -> resources .heap ;
8890 md -> resources .heap_usage = 0 ;
8991 md -> resources .heap_high_water_mark = 0 ;
9092}
@@ -141,43 +143,14 @@ int module_init(struct processing_module *mod)
141143 return 0 ;
142144}
143145
144- struct container_chunk {
145- struct list_item chunk_list ;
146- struct module_resource containers [CONFIG_MODULE_MEMORY_API_CONTAINER_CHUNK_SIZE ];
147- };
148-
149146static struct module_resource * container_get (struct processing_module * mod )
150147{
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 ;
148+ return objpool_alloc (& mod -> priv .resources .objpool , sizeof (struct module_resource ), 0 );
174149}
175150
176151static void container_put (struct processing_module * mod , struct module_resource * container )
177152{
178- struct module_resources * res = & mod -> priv .resources ;
179-
180- list_item_append (& container -> list , & res -> free_cont_list );
153+ objpool_free (& mod -> priv .resources .objpool , container );
181154}
182155
183156#if CONFIG_USERSPACE
@@ -235,7 +208,6 @@ void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignm
235208 container -> ptr = ptr ;
236209 container -> size = size ;
237210 container -> type = MOD_RES_HEAP ;
238- list_item_prepend (& container -> list , & res -> res_list );
239211
240212 res -> heap_usage += size ;
241213 if (res -> heap_usage > res -> heap_high_water_mark )
@@ -286,7 +258,6 @@ void *z_impl_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t
286258 container -> ptr = ptr ;
287259 container -> size = size ;
288260 container -> type = MOD_RES_HEAP ;
289- list_item_prepend (& container -> list , & res -> res_list );
290261
291262 res -> heap_usage += size ;
292263 if (res -> heap_usage > res -> heap_high_water_mark )
@@ -306,7 +277,7 @@ EXPORT_SYMBOL(z_impl_mod_alloc_ext);
306277#if CONFIG_COMP_BLOB
307278struct comp_data_blob_handler * mod_data_blob_handler_new (struct processing_module * mod )
308279{
309- struct module_resources * res = & mod -> priv .resources ;
280+ struct module_resources * __maybe_unused res = & mod -> priv .resources ;
310281 struct comp_data_blob_handler * bhp ;
311282 struct module_resource * container ;
312283
@@ -325,7 +296,6 @@ struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_modul
325296 container -> bhp = bhp ;
326297 container -> size = 0 ;
327298 container -> type = MOD_RES_BLOB_HANDLER ;
328- list_item_prepend (& container -> list , & res -> res_list );
329299
330300 return bhp ;
331301}
@@ -362,7 +332,6 @@ const void *z_impl_mod_fast_get(struct processing_module *mod, const void * cons
362332 container -> sram_ptr = ptr ;
363333 container -> size = 0 ;
364334 container -> type = MOD_RES_FAST_GET ;
365- list_item_prepend (& container -> list , & res -> res_list );
366335
367336 return ptr ;
368337}
@@ -394,6 +363,29 @@ static int free_contents(struct processing_module *mod, struct module_resource *
394363 return - EINVAL ;
395364}
396365
366+ struct mod_res_cb_arg {
367+ struct processing_module * mod ;
368+ const void * ptr ;
369+ };
370+
371+ static bool mod_res_free (void * data , void * arg )
372+ {
373+ struct mod_res_cb_arg * cb_arg = arg ;
374+ struct module_resource * container = data ;
375+
376+ if (cb_arg -> ptr && container -> ptr != cb_arg -> ptr )
377+ return false;
378+
379+ int ret = free_contents (cb_arg -> mod , container );
380+
381+ if (ret < 0 )
382+ comp_err (cb_arg -> mod -> dev , "Cannot free allocation %p" , cb_arg -> ptr );
383+
384+ container_put (cb_arg -> mod , container );
385+
386+ return true;
387+ }
388+
397389/**
398390 * Frees the memory block removes it from module's book keeping.
399391 * @param mod Pointer to module this memory block was allocated for.
@@ -402,28 +394,19 @@ static int free_contents(struct processing_module *mod, struct module_resource *
402394int z_impl_mod_free (struct processing_module * mod , const void * ptr )
403395{
404396 struct module_resources * res = & mod -> priv .resources ;
405- struct module_resource * container ;
406- struct list_item * res_list ;
407397
408398 MEM_API_CHECK_THREAD (res );
409399 if (!ptr )
410400 return 0 ;
411401
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- }
402+ /* Find which container holds this memory */
403+ struct mod_res_cb_arg cb_arg = {mod , ptr };
404+ int ret = objpool_iterate (& res -> objpool , mod_res_free , & cb_arg );
423405
424- comp_err (mod -> dev , "error: could not find memory pointed by %p" , ptr );
406+ if (ret < 0 )
407+ comp_err (mod -> dev , "error: could not find memory pointed by %p" , ptr );
425408
426- return - EINVAL ;
409+ return ret ;
427410}
428411EXPORT_SYMBOL (z_impl_mod_free );
429412
@@ -684,32 +667,14 @@ int module_reset(struct processing_module *mod)
684667void mod_free_all (struct processing_module * mod )
685668{
686669 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 ;
690670
691671 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- }
699672
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 );
673+ /* Free all contents found in used containers */
674+ struct mod_res_cb_arg cb_arg = {mod , NULL };
709675
710- list_item_del (& chunk -> chunk_list );
711- sof_heap_free (mod_heap , chunk );
712- }
676+ objpool_iterate (& res -> objpool , mod_res_free , & cb_arg );
677+ objpool_prune (& res -> objpool );
713678
714679 /* Make sure resource lists and accounting are reset */
715680 mod_resource_init (mod );
0 commit comments