Skip to content

Commit 361964a

Browse files
author
Jyri Sarha
committed
modules: Add mod_data_blob_handler_new() to module API
Add mod_data_blob_handler_new() to module API. The function is otherwise the same as comp_data_blob_handler_new(), but it takes a module pointer as the first argument, and the blob handler is automatically freed when the module unloads. The handler allocated with mod_data_blob_handler_new() should not be freed with comp_data_blob_handler_free(), mod_data_blob_handler_free() should be used. Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
1 parent 781301c commit 361964a

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

src/audio/module_adapter/module/generic.c

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <rtos/symbol.h>
1515

1616
#include <sof/audio/module_adapter/module/generic.h>
17+
#include <sof/audio/data_blob.h>
1718

1819
LOG_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
}
234236
EXPORT_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
}
268308
EXPORT_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+
270318
int 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

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ struct module_resources {
134134
* \brief module memory container - used for every memory allocated by module
135135
*/
136136
struct module_memory {
137-
void *ptr; /**< A pointr to particular memory block */
137+
void *ptr; /**< A pointer to particular memory block */
138138
struct list_item mem_list; /**< list of memory allocated by module */
139-
size_t size;
139+
size_t size; /**< Size of allocated heap memory, 0 if not from heap */
140+
void (*free)(void *buf); /**< Pointer to free function for non heap allocations */
140141
};
141142

142143
/**
@@ -172,6 +173,10 @@ void *mod_alloc_align(struct processing_module *mod, uint32_t size, uint32_t ali
172173
void *mod_alloc(struct processing_module *mod, uint32_t size);
173174
void *mod_zalloc(struct processing_module *mod, uint32_t size);
174175
int mod_free(struct processing_module *mod, void *ptr);
176+
#if CONFIG_COMP_BLOB
177+
struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_module *mod);
178+
void mod_data_blob_handler_free(struct processing_module *mod, struct comp_data_blob_handler *dbh);
179+
#endif
175180
void mod_free_all(struct processing_module *mod);
176181
int module_prepare(struct processing_module *mod,
177182
struct sof_source **sources, int num_of_sources,

0 commit comments

Comments
 (0)