Skip to content

Commit 1b1e8f1

Browse files
Jyri Sarhakv2019i
authored andcommitted
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 9404d2d commit 1b1e8f1

2 files changed

Lines changed: 116 additions & 37 deletions

File tree

src/audio/module_adapter/module/generic.c

Lines changed: 94 additions & 29 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

@@ -92,7 +93,7 @@ int module_init(struct processing_module *mod)
9293
}
9394

9495
/* Init memory list */
95-
list_init(&md->resources.mem_list);
96+
list_init(&md->resources.res_list);
9697
list_init(&md->resources.free_cont_list);
9798
list_init(&md->resources.cont_chunk_list);
9899
md->resources.heap_usage = 0;
@@ -116,13 +117,13 @@ int module_init(struct processing_module *mod)
116117

117118
struct container_chunk {
118119
struct list_item chunk_list;
119-
struct module_memory containers[CONFIG_MODULE_MEMORY_API_CONTAINER_CHUNK_SIZE];
120+
struct module_resource containers[CONFIG_MODULE_MEMORY_API_CONTAINER_CHUNK_SIZE];
120121
};
121122

122-
static struct module_memory *container_get(struct processing_module *mod)
123+
static struct module_resource *container_get(struct processing_module *mod)
123124
{
124125
struct module_resources *res = &mod->priv.resources;
125-
struct module_memory *container;
126+
struct module_resource *container;
126127

127128
if (list_is_empty(&res->free_cont_list)) {
128129
struct container_chunk *chunk = rzalloc(SOF_MEM_FLAG_USER, sizeof(*chunk));
@@ -135,19 +136,19 @@ static struct module_memory *container_get(struct processing_module *mod)
135136

136137
list_item_append(&chunk->chunk_list, &res->cont_chunk_list);
137138
for (i = 0; i < ARRAY_SIZE(chunk->containers); i++)
138-
list_item_append(&chunk->containers[i].mem_list, &res->free_cont_list);
139+
list_item_append(&chunk->containers[i].list, &res->free_cont_list);
139140
}
140141

141-
container = list_first_item(&res->free_cont_list, struct module_memory, mem_list);
142-
list_item_del(&container->mem_list);
142+
container = list_first_item(&res->free_cont_list, struct module_resource, list);
143+
list_item_del(&container->list);
143144
return container;
144145
}
145146

146-
static void container_put(struct processing_module *mod, struct module_memory *container)
147+
static void container_put(struct processing_module *mod, struct module_resource *container)
147148
{
148149
struct module_resources *res = &mod->priv.resources;
149150

150-
list_item_append(&container->mem_list, &res->free_cont_list);
151+
list_item_append(&container->list, &res->free_cont_list);
151152
}
152153

153154
/**
@@ -161,7 +162,7 @@ static void container_put(struct processing_module *mod, struct module_memory *c
161162
*/
162163
void *mod_alloc_align(struct processing_module *mod, uint32_t size, uint32_t alignment)
163164
{
164-
struct module_memory *container = container_get(mod);
165+
struct module_resource *container = container_get(mod);
165166
struct module_resources *res = &mod->priv.resources;
166167
void *ptr;
167168

@@ -189,7 +190,8 @@ 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;
192-
list_item_prepend(&container->mem_list, &res->mem_list);
193+
container->type = MOD_RES_HEAP;
194+
list_item_prepend(&container->list, &res->res_list);
193195

194196
res->heap_usage += size;
195197
if (res->heap_usage > res->heap_high_water_mark)
@@ -233,30 +235,84 @@ 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_resource *container = container_get(mod);
251+
struct comp_data_blob_handler *bhp;
252+
253+
if (!container)
254+
return NULL;
255+
256+
bhp = comp_data_blob_handler_new_ext(mod->dev, false, NULL, NULL);
257+
if (!bhp) {
258+
container_put(mod, container);
259+
return NULL;
260+
}
261+
262+
container->bhp = bhp;
263+
container->size = 0;
264+
container->type = MOD_RES_BLOB_HANDLER;
265+
list_item_prepend(&container->list, &res->res_list);
266+
267+
return bhp;
268+
}
269+
EXPORT_SYMBOL(mod_data_blob_handler_new);
270+
#endif
271+
272+
static int free_contents(struct processing_module *mod, struct module_resource *container)
273+
{
274+
struct module_resources *res = &mod->priv.resources;
275+
276+
switch (container->type) {
277+
case MOD_RES_HEAP:
278+
rfree(container->ptr);
279+
res->heap_usage -= container->size;
280+
return 0;
281+
#if CONFIG_COMP_BLOB
282+
case MOD_RES_BLOB_HANDLER:
283+
comp_data_blob_handler_free(container->bhp);
284+
return 0;
285+
#endif
286+
default:
287+
comp_err(mod->dev, "Unknown resource type: %d", container->type);
288+
}
289+
return -EINVAL;
290+
}
291+
236292
/**
237293
* Frees the memory block removes it from module's book keeping.
238294
* @param mod Pointer to module this memory block was allocated for.
239295
* @param ptr Pointer to the memory block.
240296
*/
241-
int mod_free(struct processing_module *mod, void *ptr)
297+
int mod_free(struct processing_module *mod, const void *ptr)
242298
{
243299
struct module_resources *res = &mod->priv.resources;
244-
struct module_memory *mem;
245-
struct list_item *mem_list;
246-
struct list_item *_mem_list;
300+
struct module_resource *container;
301+
struct list_item *res_list;
302+
struct list_item *_res_list;
247303

248304
if (!ptr)
249305
return 0;
250306

251307
/* Find which container keeps this memory */
252-
list_for_item_safe(mem_list, _mem_list, &res->mem_list) {
253-
mem = container_of(mem_list, struct module_memory, mem_list);
254-
if (mem->ptr == ptr) {
255-
rfree(mem->ptr);
256-
res->heap_usage -= mem->size;
257-
list_item_del(&mem->mem_list);
258-
container_put(mod, mem);
259-
return 0;
308+
list_for_item_safe(res_list, _res_list, &res->res_list) {
309+
container = container_of(res_list, struct module_resource, list);
310+
if (container->ptr == ptr) {
311+
int ret = free_contents(mod, container);
312+
313+
list_item_del(&container->list);
314+
container_put(mod, container);
315+
return ret;
260316
}
261317
}
262318

@@ -267,6 +323,14 @@ int mod_free(struct processing_module *mod, void *ptr)
267323
}
268324
EXPORT_SYMBOL(mod_free);
269325

326+
#if CONFIG_COMP_BLOB
327+
void mod_data_blob_handler_free(struct processing_module *mod, struct comp_data_blob_handler *dbh)
328+
{
329+
mod_free(mod, (void *)dbh);
330+
}
331+
EXPORT_SYMBOL(mod_data_blob_handler_free);
332+
#endif
333+
270334
int module_prepare(struct processing_module *mod,
271335
struct sof_source **sources, int num_of_sources,
272336
struct sof_sink **sinks, int num_of_sinks)
@@ -438,8 +502,8 @@ int module_reset(struct processing_module *mod)
438502
}
439503

440504
/**
441-
* Frees all the memory allocated for this module
442-
* @param mod Pointer to module this memory block was allocated for.
505+
* Frees all the resources registered for this module
506+
* @param mod Pointer to module that should have its resource freed.
443507
*
444508
* This function is called automatically when the module is unloaded.
445509
*/
@@ -450,11 +514,12 @@ void mod_free_all(struct processing_module *mod)
450514
struct list_item *_list;
451515

452516
/* Find which container keeps this memory */
453-
list_for_item_safe(list, _list, &res->mem_list) {
454-
struct module_memory *mem = container_of(list, struct module_memory, mem_list);
517+
list_for_item_safe(list, _list, &res->res_list) {
518+
struct module_resource *container =
519+
container_of(list, struct module_resource, list);
455520

456-
rfree(mem->ptr);
457-
list_item_del(&mem->mem_list);
521+
free_contents(mod, container);
522+
list_item_del(&container->list);
458523
}
459524

460525
list_for_item_safe(list, _list, &res->cont_chunk_list) {

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,37 @@ struct module_param {
116116
};
117117

118118
/**
119-
* \struct module_memory
119+
* \struct module_resources
120120
* \brief module resources block - used for module allocation records
121121
* The allocations are recorded so that they can be automatically freed
122122
* when the module unloads.
123123
*/
124124
struct module_resources {
125-
struct list_item mem_list; /**< Allocad memory containers */
125+
struct list_item res_list; /**< Allocad resource containers */
126126
struct list_item free_cont_list; /**< Unused memory containers */
127127
struct list_item cont_chunk_list; /**< Memory container chunks */
128128
size_t heap_usage;
129129
size_t heap_high_water_mark;
130130
};
131131

132+
enum mod_resource_type {
133+
MOD_RES_UNINITIALIZED = 0,
134+
MOD_RES_HEAP,
135+
MOD_RES_BLOB_HANDLER,
136+
};
137+
132138
/**
133-
* \struct module_memory
139+
* \struct module_resource
134140
* \brief module memory container - used for every memory allocated by module
135141
*/
136-
struct module_memory {
137-
void *ptr; /**< A pointr to particular memory block */
138-
struct list_item mem_list; /**< list of memory allocated by module */
139-
size_t size;
142+
struct module_resource {
143+
union {
144+
void *ptr; /**< Pointer to heap allocated memory */
145+
struct comp_data_blob_handler *bhp; /**< Blob handler ptr */
146+
};
147+
struct list_item list; /**< list element */
148+
size_t size; /**< Size of allocated heap memory, 0 if not from heap */
149+
enum mod_resource_type type; /**< Resource type */
140150
};
141151

142152
/**
@@ -171,7 +181,11 @@ int module_init(struct processing_module *mod);
171181
void *mod_alloc_align(struct processing_module *mod, uint32_t size, uint32_t alignment);
172182
void *mod_alloc(struct processing_module *mod, uint32_t size);
173183
void *mod_zalloc(struct processing_module *mod, uint32_t size);
174-
int mod_free(struct processing_module *mod, void *ptr);
184+
int mod_free(struct processing_module *mod, const void *ptr);
185+
#if CONFIG_COMP_BLOB
186+
struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_module *mod);
187+
void mod_data_blob_handler_free(struct processing_module *mod, struct comp_data_blob_handler *dbh);
188+
#endif
175189
void mod_free_all(struct processing_module *mod);
176190
int module_prepare(struct processing_module *mod,
177191
struct sof_source **sources, int num_of_sources,

0 commit comments

Comments
 (0)