Skip to content

Commit d6d6854

Browse files
Audio: MDRC: Restructure Multiband DRC for more effective memory allocation
The coefficient allocation for the crossover, emphasis, and deemphasis filters is combined into a single contiguous memory block with this check-in. Benefits include decreased heap fragmentation risk, better cache performance because of data locality, and less memory allocation overhead. A significant reduction in the complexity of memory management logic makes the code less prone to memory leaks and easier to maintain. Signed-off-by: Shriram Shastry <[email protected]>
1 parent 2f3f877 commit d6d6854

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/audio/multiband_drc/multiband_drc.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,43 +128,62 @@ static int multiband_drc_init_coef(struct processing_module *mod, int16_t nch, u
128128
comp_info(dev, "multiband_drc_init_coef(), initializing %i-way crossover",
129129
config->num_bands);
130130

131+
/* Allocate memory for all coefficient arrays in a single block */
132+
/* For crossover, emphasis, and deemphasis */
133+
size_t total_coefficients_size = sizeof(struct sof_eq_iir_biquad) *
134+
num_bands * nch * 3;
135+
136+
struct sof_eq_iir_biquad *coefficients_block =
137+
rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
138+
total_coefficients_size);
139+
140+
if (!coefficients_block) {
141+
comp_err(dev, "multiband_drc_init_coef(), failed to allocate memory for coefficients");
142+
return -ENOMEM;
143+
}
144+
145+
/* Assign allocated memory blocks */
146+
crossover = coefficients_block;
147+
emphasis = coefficients_block + num_bands * nch;
148+
deemphasis = emphasis + num_bands * nch;
149+
131150
/* Crossover: collect the coef array and assign it to every channel */
132-
crossover = config->crossover_coef;
133151
for (ch = 0; ch < nch; ch++) {
134152
ret = crossover_init_coef_ch(crossover, &state->crossover[ch],
135153
config->num_bands);
136154
/* Free all previously allocated blocks in case of an error */
137155
if (ret < 0) {
138156
comp_err(dev,
139157
"multiband_drc_init_coef(), could not assign coeffs to ch %d", ch);
158+
rfree(coefficients_block);
140159
goto err;
141160
}
142161
}
143162

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

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

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

160179
/* Deemphasis: collect the coef array and assign it to every channel */
161-
deemphasis = config->deemp_coef;
162180
for (ch = 0; ch < nch; ch++) {
163181
ret = multiband_drc_eq_init_coef_ch(deemphasis, &state->deemphasis[ch]);
164182
/* Free all previously allocated blocks in case of an error */
165183
if (ret < 0) {
166184
comp_err(dev, "multiband_drc_init_coef(), could not assign coeffs to ch %d",
167185
ch);
186+
rfree(coefficients_block);
168187
goto err;
169188
}
170189
}
@@ -177,6 +196,7 @@ static int multiband_drc_init_coef(struct processing_module *mod, int16_t nch, u
177196
if (ret < 0) {
178197
comp_err(dev,
179198
"multiband_drc_init_coef(), could not init pre delay buffers");
199+
rfree(coefficients_block);
180200
goto err;
181201
}
182202

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

193213
err:
214+
/* Free allocated memory block on error */
215+
if (coefficients_block)
216+
rfree(coefficients_block);
217+
194218
multiband_drc_reset_state(state);
195219
return ret;
196220
}
@@ -220,7 +244,6 @@ static int multiband_drc_init(struct processing_module *mod)
220244
struct module_data *md = &mod->priv;
221245
struct comp_dev *dev = mod->dev;
222246
struct module_config *cfg = &md->cfg;
223-
struct multiband_drc_comp_data *cd;
224247
size_t bs = cfg->size;
225248
int ret;
226249

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

238-
cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
239-
if (!cd)
261+
/* Memory allocation for multiband_drc_comp_data */
262+
struct multiband_drc_comp_data *cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0,
263+
SOF_MEM_CAPS_RAM, sizeof(*cd));
264+
if (!cd) {
265+
comp_err(dev, "multiband_drc_init(), allocation for multiband_drc_comp_data failed");
240266
return -ENOMEM;
267+
}
241268

242269
md->private = cd;
243270
cd->multiband_drc_func = NULL;

0 commit comments

Comments
 (0)