1212 */
1313
1414#include <rtos/symbol.h>
15- #include <sof/compiler_attributes.h>
16- #include <sof/objpool.h>
1715#include <sof/audio/module_adapter/module/generic.h>
1816#include <sof/audio/data_blob.h>
1917#include <sof/lib/fast-get.h>
@@ -83,10 +81,10 @@ int module_load_config(struct comp_dev *dev, const void *cfg, size_t size)
8381void mod_resource_init (struct processing_module * mod )
8482{
8583 struct module_data * md = & mod -> priv ;
86-
8784 /* Init memory list */
88- list_init (& md -> resources .objpool .list );
89- md -> resources .objpool .heap = md -> resources .heap ;
85+ list_init (& md -> resources .res_list );
86+ list_init (& md -> resources .free_cont_list );
87+ list_init (& md -> resources .cont_chunk_list );
9088 md -> resources .heap_usage = 0 ;
9189 md -> resources .heap_high_water_mark = 0 ;
9290}
@@ -143,14 +141,43 @@ int module_init(struct processing_module *mod)
143141 return 0 ;
144142}
145143
144+ struct container_chunk {
145+ struct list_item chunk_list ;
146+ struct module_resource containers [CONFIG_MODULE_MEMORY_API_CONTAINER_CHUNK_SIZE ];
147+ };
148+
146149static struct module_resource * container_get (struct processing_module * mod )
147150{
148- return objpool_alloc (& mod -> priv .resources .objpool , sizeof (struct module_resource ), 0 );
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 ;
149174}
150175
151176static void container_put (struct processing_module * mod , struct module_resource * container )
152177{
153- objpool_free (& mod -> priv .resources .objpool , container );
178+ struct module_resources * res = & mod -> priv .resources ;
179+
180+ list_item_append (& container -> list , & res -> free_cont_list );
154181}
155182
156183#if CONFIG_USERSPACE
@@ -208,6 +235,7 @@ void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignm
208235 container -> ptr = ptr ;
209236 container -> size = size ;
210237 container -> type = MOD_RES_HEAP ;
238+ list_item_prepend (& container -> list , & res -> res_list );
211239
212240 res -> heap_usage += size ;
213241 if (res -> heap_usage > res -> heap_high_water_mark )
@@ -258,6 +286,7 @@ void *z_impl_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t
258286 container -> ptr = ptr ;
259287 container -> size = size ;
260288 container -> type = MOD_RES_HEAP ;
289+ list_item_prepend (& container -> list , & res -> res_list );
261290
262291 res -> heap_usage += size ;
263292 if (res -> heap_usage > res -> heap_high_water_mark )
@@ -277,7 +306,7 @@ EXPORT_SYMBOL(z_impl_mod_alloc_ext);
277306#if CONFIG_COMP_BLOB
278307struct comp_data_blob_handler * mod_data_blob_handler_new (struct processing_module * mod )
279308{
280- struct module_resources * __maybe_unused res = & mod -> priv .resources ;
309+ struct module_resources * res = & mod -> priv .resources ;
281310 struct comp_data_blob_handler * bhp ;
282311 struct module_resource * container ;
283312
@@ -296,6 +325,7 @@ struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_modul
296325 container -> bhp = bhp ;
297326 container -> size = 0 ;
298327 container -> type = MOD_RES_BLOB_HANDLER ;
328+ list_item_prepend (& container -> list , & res -> res_list );
299329
300330 return bhp ;
301331}
@@ -332,6 +362,7 @@ const void *z_impl_mod_fast_get(struct processing_module *mod, const void * cons
332362 container -> sram_ptr = ptr ;
333363 container -> size = 0 ;
334364 container -> type = MOD_RES_FAST_GET ;
365+ list_item_prepend (& container -> list , & res -> res_list );
335366
336367 return ptr ;
337368}
@@ -363,29 +394,6 @@ static int free_contents(struct processing_module *mod, struct module_resource *
363394 return - EINVAL ;
364395}
365396
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-
389397/**
390398 * Frees the memory block removes it from module's book keeping.
391399 * @param mod Pointer to module this memory block was allocated for.
@@ -394,19 +402,28 @@ static bool mod_res_free(void *data, void *arg)
394402int z_impl_mod_free (struct processing_module * mod , const void * ptr )
395403{
396404 struct module_resources * res = & mod -> priv .resources ;
405+ struct module_resource * container ;
406+ struct list_item * res_list ;
397407
398408 MEM_API_CHECK_THREAD (res );
399409 if (!ptr )
400410 return 0 ;
401411
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 );
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+ }
405423
406- if (ret < 0 )
407- comp_err (mod -> dev , "error: could not find memory pointed by %p" , ptr );
424+ comp_err (mod -> dev , "error: could not find memory pointed by %p" , ptr );
408425
409- return ret ;
426+ return - EINVAL ;
410427}
411428EXPORT_SYMBOL (z_impl_mod_free );
412429
@@ -667,14 +684,32 @@ int module_reset(struct processing_module *mod)
667684void mod_free_all (struct processing_module * mod )
668685{
669686 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 ;
670690
671691 MEM_API_CHECK_THREAD (res );
672-
673692 /* Free all contents found in used containers */
674- struct mod_res_cb_arg cb_arg = {mod , NULL };
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+ }
675699
676- objpool_iterate (& res -> objpool , mod_res_free , & cb_arg );
677- objpool_prune (& res -> objpool );
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 );
709+
710+ list_item_del (& chunk -> chunk_list );
711+ sof_heap_free (mod_heap , chunk );
712+ }
678713
679714 /* Make sure resource lists and accounting are reset */
680715 mod_resource_init (mod );
0 commit comments