1414#include <rtos/symbol.h>
1515
1616#include <sof/audio/module_adapter/module/generic.h>
17+ #include <sof/audio/data_blob.h>
1718
1819LOG_MODULE_DECLARE (module_adapter , CONFIG_SOF_LOG_LEVEL );
1920
@@ -189,6 +190,7 @@ void *mod_alloc_align(struct processing_module *mod, uint32_t size, uint32_t ali
189190 /* Store reference to allocated memory */
190191 container -> ptr = ptr ;
191192 container -> size = size ;
193+ container -> free = NULL ;
192194 list_item_prepend (& container -> mem_list , & res -> mem_list );
193195
194196 res -> heap_usage += size ;
@@ -233,6 +235,40 @@ void *mod_zalloc(struct processing_module *mod, uint32_t size)
233235}
234236EXPORT_SYMBOL (mod_zalloc );
235237
238+ /**
239+ * Creates a blob handler and releases it when the module is unloaded
240+ * @param mod Pointer to module this memory block is allocated for.
241+ * @return Pointer to the created data blob handler
242+ *
243+ * Like comp_data_blob_handler_new() but the handler is automatically freed.
244+ */
245+ #if CONFIG_COMP_BLOB
246+ struct comp_data_blob_handler *
247+ mod_data_blob_handler_new (struct processing_module * mod )
248+ {
249+ struct module_resources * res = & mod -> priv .resources ;
250+ struct module_memory * container = container_get (mod );
251+ struct comp_data_blob_handler * dbh ;
252+
253+ if (!container )
254+ return NULL ;
255+
256+ dbh = comp_data_blob_handler_new_ext (mod -> dev , false, NULL , NULL );
257+ if (!dbh ) {
258+ container_put (mod , container );
259+ return NULL ;
260+ }
261+
262+ container -> ptr = dbh ;
263+ container -> size = 0 ;
264+ container -> free = (void (* )(void * ))comp_data_blob_handler_free ;
265+ list_item_prepend (& container -> mem_list , & res -> mem_list );
266+
267+ return dbh ;
268+ }
269+ EXPORT_SYMBOL (mod_data_blob_handler_new );
270+ #endif
271+
236272/**
237273 * Frees the memory block removes it from module's book keeping.
238274 * @param mod Pointer to module this memory block was allocated for.
@@ -252,8 +288,12 @@ int mod_free(struct processing_module *mod, void *ptr)
252288 list_for_item_safe (mem_list , _mem_list , & res -> mem_list ) {
253289 mem = container_of (mem_list , struct module_memory , mem_list );
254290 if (mem -> ptr == ptr ) {
255- rfree (mem -> ptr );
256- res -> heap_usage -= mem -> size ;
291+ if (mem -> free ) {
292+ mem -> free (mem -> ptr );
293+ } else {
294+ rfree (mem -> ptr );
295+ res -> heap_usage -= mem -> size ;
296+ }
257297 list_item_del (& mem -> mem_list );
258298 container_put (mod , mem );
259299 return 0 ;
@@ -267,6 +307,14 @@ int mod_free(struct processing_module *mod, void *ptr)
267307}
268308EXPORT_SYMBOL (mod_free );
269309
310+ #if CONFIG_COMP_BLOB
311+ void mod_data_blob_handler_free (struct processing_module * mod , struct comp_data_blob_handler * dbh )
312+ {
313+ mod_free (mod , (void * )dbh );
314+ }
315+ EXPORT_SYMBOL (mod_data_blob_handler_free );
316+ #endif
317+
270318int module_prepare (struct processing_module * mod ,
271319 struct sof_source * * sources , int num_of_sources ,
272320 struct sof_sink * * sinks , int num_of_sinks )
@@ -453,7 +501,10 @@ void mod_free_all(struct processing_module *mod)
453501 list_for_item_safe (list , _list , & res -> mem_list ) {
454502 struct module_memory * mem = container_of (list , struct module_memory , mem_list );
455503
456- rfree (mem -> ptr );
504+ if (mem -> free )
505+ mem -> free (mem -> ptr );
506+ else
507+ rfree (mem -> ptr );
457508 list_item_del (& mem -> mem_list );
458509 }
459510
0 commit comments