Skip to content

Commit

Permalink
Audio: MDRC: Restructure MDRC for effective memory allocation
Browse files Browse the repository at this point in the history
This check-in attempts to decrease memory allocation overhead,
enhance cache efficiency through data locality, and lessen heap
fragmentation. Within the MDRC component, combine memory
allocations for the crossover, emphasis, and deemphasis filter
coefficients into a single block.

By streamlining memory management, the update lowers the possibility
of memory leaks.

Signed-off-by: Shriram Shastry <[email protected]>
  • Loading branch information
ShriramShastry committed Jun 5, 2024
1 parent 2f3f877 commit a8a3a75
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/audio/multiband_drc/multiband_drc.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,43 +128,62 @@ static int multiband_drc_init_coef(struct processing_module *mod, int16_t nch, u
comp_info(dev, "multiband_drc_init_coef(), initializing %i-way crossover",
config->num_bands);

/* Allocate memory for all coefficient arrays in a single block */
/* For crossover, emphasis, and deemphasis */
size_t total_coefficients_size = sizeof(struct sof_eq_iir_biquad) *
num_bands * nch * 3;

struct sof_eq_iir_biquad *coefficients_block =
rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
total_coefficients_size);

if (!coefficients_block) {
comp_err(dev, "multiband_drc_init_coef(), failed to allocate memory for coefficients");
return -ENOMEM;
}

/* Assign allocated memory blocks */
crossover = coefficients_block;
emphasis = coefficients_block + num_bands * nch;
deemphasis = emphasis + num_bands * nch;

/* Crossover: collect the coef array and assign it to every channel */
crossover = config->crossover_coef;
for (ch = 0; ch < nch; ch++) {
ret = crossover_init_coef_ch(crossover, &state->crossover[ch],
config->num_bands);
/* Free all previously allocated blocks in case of an error */
if (ret < 0) {
comp_err(dev,
"multiband_drc_init_coef(), could not assign coeffs to ch %d", ch);
rfree(coefficients_block);
goto err;
}
}

comp_info(dev, "multiband_drc_init_coef(), initializing emphasis_eq");

/* Emphasis: collect the coef array and assign it to every channel */
emphasis = config->emp_coef;
for (ch = 0; ch < nch; ch++) {
ret = multiband_drc_eq_init_coef_ch(emphasis, &state->emphasis[ch]);
/* Free all previously allocated blocks in case of an error */
if (ret < 0) {
comp_err(dev, "multiband_drc_init_coef(), could not assign coeffs to ch %d",
ch);
rfree(coefficients_block);
goto err;
}
}

comp_info(dev, "multiband_drc_init_coef(), initializing deemphasis_eq");

/* Deemphasis: collect the coef array and assign it to every channel */
deemphasis = config->deemp_coef;
for (ch = 0; ch < nch; ch++) {
ret = multiband_drc_eq_init_coef_ch(deemphasis, &state->deemphasis[ch]);
/* Free all previously allocated blocks in case of an error */
if (ret < 0) {
comp_err(dev, "multiband_drc_init_coef(), could not assign coeffs to ch %d",
ch);
rfree(coefficients_block);
goto err;
}
}
Expand All @@ -177,6 +196,7 @@ static int multiband_drc_init_coef(struct processing_module *mod, int16_t nch, u
if (ret < 0) {
comp_err(dev,
"multiband_drc_init_coef(), could not init pre delay buffers");
rfree(coefficients_block);
goto err;
}

Expand All @@ -191,6 +211,10 @@ static int multiband_drc_init_coef(struct processing_module *mod, int16_t nch, u
return 0;

err:
/* Free allocated memory block on error */
if (coefficients_block)
rfree(coefficients_block);

multiband_drc_reset_state(state);
return ret;
}
Expand Down Expand Up @@ -220,7 +244,6 @@ static int multiband_drc_init(struct processing_module *mod)
struct module_data *md = &mod->priv;
struct comp_dev *dev = mod->dev;
struct module_config *cfg = &md->cfg;
struct multiband_drc_comp_data *cd;
size_t bs = cfg->size;
int ret;

Expand All @@ -235,9 +258,13 @@ static int multiband_drc_init(struct processing_module *mod)
return -EINVAL;
}

cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
if (!cd)
/* Memory allocation for multiband_drc_comp_data */
struct multiband_drc_comp_data *cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0,
SOF_MEM_CAPS_RAM, sizeof(*cd));
if (!cd) {
comp_err(dev, "multiband_drc_init(), allocation for multiband_drc_comp_data failed");
return -ENOMEM;
}

md->private = cd;
cd->multiband_drc_func = NULL;
Expand Down

0 comments on commit a8a3a75

Please sign in to comment.