Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 44 additions & 34 deletions src/audio/module_adapter/module/cadence.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static int cadence_codec_post_init(struct processing_module *mod)
return ret;
}
/* Allocate space for codec object */
cd->self = rballoc(SOF_MEM_FLAG_USER, obj_size);
cd->self = mod_balloc(mod, obj_size);
if (!cd->self) {
comp_err(dev, "failed to allocate space for lib object");
return -ENOMEM;
Expand All @@ -247,7 +247,7 @@ static int cadence_codec_post_init(struct processing_module *mod)
API_CALL(cd, XA_API_CMD_INIT, XA_CMD_TYPE_INIT_API_PRE_CONFIG_PARAMS,
NULL, ret);
if (ret != LIB_NO_ERROR) {
rfree(cd->self);
mod_free(mod, cd->self);
return ret;
}

Expand All @@ -268,7 +268,7 @@ static int cadence_codec_init(struct processing_module *mod)

comp_dbg(dev, "cadence_codec_init() start");

cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(struct cadence_codec_data));
cd = mod_zalloc(mod, sizeof(struct cadence_codec_data));
if (!cd) {
comp_err(dev, "failed to allocate memory for cadence codec data");
return -ENOMEM;
Expand All @@ -284,17 +284,15 @@ static int cadence_codec_init(struct processing_module *mod)
cfg = (const struct ipc4_cadence_module_cfg *)codec->cfg.init_data;

/* allocate memory for set up config */
setup_cfg->data = rmalloc(SOF_MEM_FLAG_USER,
cfg->param_size);
setup_cfg->data = mod_alloc(mod, cfg->param_size);
if (!setup_cfg->data) {
comp_err(dev, "failed to alloc setup config");
ret = -ENOMEM;
goto free;
}

/* allocate memory for runtime set up config */
codec->cfg.data = rmalloc(SOF_MEM_FLAG_USER,
cfg->param_size);
codec->cfg.data = mod_alloc(mod, cfg->param_size);
if (!codec->cfg.data) {
comp_err(dev, "failed to alloc runtime setup config");
ret = -ENOMEM;
Expand Down Expand Up @@ -326,11 +324,11 @@ static int cadence_codec_init(struct processing_module *mod)
return 0;

free_cfg2:
rfree(codec->cfg.data);
mod_free(mod, codec->cfg.data);
free_cfg:
rfree(setup_cfg->data);
mod_free(mod, setup_cfg->data);
free:
rfree(cd);
mod_free(mod, cd);
return ret;
}

Expand All @@ -345,7 +343,7 @@ static int cadence_codec_init(struct processing_module *mod)

comp_dbg(dev, "cadence_codec_init() start");

cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(struct cadence_codec_data));
cd = mod_zalloc(mod, sizeof(struct cadence_codec_data));
if (!cd) {
comp_err(dev, "failed to allocate memory for cadence codec data");
return -ENOMEM;
Expand All @@ -359,8 +357,7 @@ static int cadence_codec_init(struct processing_module *mod)
setup_cfg = &cd->setup_cfg;

/* allocate memory for set up config */
setup_cfg->data = rmalloc(SOF_MEM_FLAG_USER,
codec->cfg.size);
setup_cfg->data = mod_alloc(mod, codec->cfg.size);
if (!setup_cfg->data) {
comp_err(dev, "failed to alloc setup config");
ret = -ENOMEM;
Expand All @@ -383,9 +380,9 @@ static int cadence_codec_init(struct processing_module *mod)
return 0;

free_cfg:
rfree(setup_cfg->data);
mod_free(mod, setup_cfg->data);
free:
rfree(cd);
mod_free(mod, cd);
return ret;
}

Expand Down Expand Up @@ -465,6 +462,20 @@ static int cadence_codec_apply_config(struct processing_module *mod)
return 0;
}

static void free_memory_tables(struct processing_module *mod)
{
struct cadence_codec_data *cd = module_get_private_data(mod);
int i;

if (cd->mem_to_be_freed)
for (i = 0; i < cd->mem_to_be_freed_len; i++)
mod_free(mod, cd->mem_to_be_freed[i]);

mod_free(mod, cd->mem_to_be_freed);
cd->mem_to_be_freed = NULL;
cd->mem_to_be_freed_len = 0;
}

static int init_memory_tables(struct processing_module *mod)
{
int ret, no_mem_tables, i, mem_type, mem_size, mem_alignment;
Expand Down Expand Up @@ -493,6 +504,11 @@ static int init_memory_tables(struct processing_module *mod)
return ret;
}

cd->mem_to_be_freed = mod_zalloc(mod, no_mem_tables * sizeof(*cd->mem_to_be_freed));
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory allocation for mem_to_be_freed occurs after memory tables are allocated, but if this allocation fails, the already-allocated memory tables will leak. This allocation should occur before the memory table loop, or the error path should be updated to free any already-allocated tables.

Copilot uses AI. Check for mistakes.
if (!cd->mem_to_be_freed)
return -ENOMEM;
cd->mem_to_be_freed_len = no_mem_tables;

/* Initialize each memory table */
for (i = 0; i < no_mem_tables; i++) {
/* Get type of memory - it specifies how the memory will be used */
Expand Down Expand Up @@ -526,6 +542,7 @@ static int init_memory_tables(struct processing_module *mod)
ret = -EINVAL;
goto err;
}
cd->mem_to_be_freed[i] = ptr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could call the array just mem_table or similar, but not so important

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

/* Finally, provide this memory for codec */
API_CALL(cd, XA_API_CMD_SET_MEM_PTR, i, ptr, ret);
if (ret != LIB_NO_ERROR) {
Expand Down Expand Up @@ -562,14 +579,8 @@ static int init_memory_tables(struct processing_module *mod)

return 0;
err:
if (scratch)
mod_free(mod, scratch);
if (persistent)
mod_free(mod, persistent);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like these two mod_free() calls were redundant: scratch and persistent are copies of ptr on the iteration, that failed, but this is called during the prepare() step, so then clean up would be performed and all module allocated memory would be freed anyway

if (codec->mpd.in_buff)
mod_free(mod, codec->mpd.in_buff);
if (codec->mpd.out_buff)
mod_free(mod, codec->mpd.out_buff);
free_memory_tables(mod);

return ret;
}

Expand Down Expand Up @@ -842,12 +853,8 @@ static int cadence_codec_reset(struct processing_module *mod)
struct cadence_codec_data *cd = codec->private;
int ret;

/*
* Current CADENCE API doesn't support reset of codec's runtime parameters.
* So, free all memory associated with runtime params. These will be reallocated during
* prepare.
*/
mod_free_all(mod);
free_memory_tables(mod);
mod_free(mod, cd->mem_tabs);

/* reset to default params */
API_CALL(cd, XA_API_CMD_INIT, XA_CMD_TYPE_INIT_API_PRE_CONFIG_PARAMS, NULL, ret);
Expand All @@ -856,7 +863,7 @@ static int cadence_codec_reset(struct processing_module *mod)

codec->mpd.init_done = 0;

rfree(cd->self);
mod_free(mod, cd->self);
cd->self = NULL;

return ret;
Expand All @@ -866,10 +873,13 @@ static int cadence_codec_free(struct processing_module *mod)
{
struct cadence_codec_data *cd = module_get_private_data(mod);

rfree(cd->setup_cfg.data);
mod_free_all(mod);
rfree(cd->self);
rfree(cd);
mod_free(mod, cd->setup_cfg.data);

free_memory_tables(mod);
mod_free(mod, cd->mem_tabs);

mod_free(mod, cd->self);
mod_free(mod, cd);
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/include/sof/audio/module_adapter/module/cadence.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ struct cadence_codec_data {
void *self;
xa_codec_func_t *api;
void *mem_tabs;
size_t mem_to_be_freed_len;
void **mem_to_be_freed;
uint32_t api_id;
struct module_config setup_cfg;
};
Expand Down
Loading