Skip to content

Commit 9c1b3aa

Browse files
committed
module-adapter: allocate control objects on an own heap
We want to be able to serve all module memory allocations from a private heap. This commit creates such a heap and moved struct comp_dev and struct processing_module to it. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent a8aef6e commit 9c1b3aa

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

src/audio/module_adapter/module_adapter.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,47 @@ struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
7474
return NULL;
7575
}
7676

77-
dev = comp_alloc(drv, sizeof(*dev));
78-
if (!dev) {
79-
comp_cl_err(drv, "module_adapter_new(), failed to allocate memory for comp_dev");
77+
#define HEAP_SIZE (8 * 1024)
78+
int flags = config->proc_domain == COMP_PROCESSING_DOMAIN_DP ?
79+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT : SOF_MEM_FLAG_USER;
80+
uint8_t *mod_heap_mem = rballoc_align(flags, HEAP_SIZE, 4096);
81+
82+
if (!mod_heap_mem)
8083
return NULL;
81-
}
82-
dev->ipc_config = *config;
84+
85+
struct k_heap *mod_heap = (struct k_heap *)mod_heap_mem;
86+
const size_t heap_pref_size = ALIGN_UP(sizeof(*mod_heap), 16);
87+
void *mod_heap_buf = mod_heap_mem + heap_pref_size;
88+
89+
k_heap_init(mod_heap, mod_heap_buf, HEAP_SIZE - heap_pref_size);
8390

8491
/* allocate module information.
8592
* for DP shared modules this struct must be accessible from all cores
8693
* Unfortunately at this point there's no information of components the module
8794
* will be bound to. So we need to allocate shared memory for each DP module
8895
* To be removed when pipeline 2.0 is ready
8996
*/
90-
int flags = config->proc_domain == COMP_PROCESSING_DOMAIN_DP ?
91-
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT : SOF_MEM_FLAG_USER;
9297

93-
mod = rzalloc(flags, sizeof(*mod));
98+
mod = k_heap_alloc(mod_heap, sizeof(*mod), K_NO_WAIT);
9499
if (!mod) {
95-
comp_err(dev, "module_adapter_new(), failed to allocate memory for module");
100+
comp_cl_err(drv, "module_adapter_new(), failed to allocate memory for module");
101+
goto emod;
102+
}
103+
104+
memset(mod, 0, sizeof(*mod));
105+
mod->priv.resources.heap = mod_heap;
106+
mod->priv.resources.heap_mem = mod_heap_mem;
107+
108+
dev = k_heap_alloc(mod_heap, sizeof(*dev), K_NO_WAIT);
109+
if (!dev) {
110+
comp_cl_err(drv, "module_adapter_new(), failed to allocate memory for comp_dev");
96111
goto err;
97112
}
98113

114+
memset(dev, 0, sizeof(*dev));
115+
comp_init(drv, dev, sizeof(*dev));
116+
dev->ipc_config = *config;
117+
99118
dst = &mod->priv.cfg;
100119

101120
module_set_private_data(mod, mod_priv);
@@ -159,13 +178,17 @@ struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
159178

160179
comp_dbg(dev, "module_adapter_new() done");
161180
return dev;
181+
162182
err:
163183
#if CONFIG_IPC_MAJOR_4
164184
if (mod)
165185
rfree(mod->priv.cfg.input_pins);
166186
#endif
167-
rfree(mod);
168-
rfree(dev);
187+
k_heap_free(mod_heap, dev);
188+
k_heap_free(mod_heap, mod);
189+
emod:
190+
rfree(mod_heap_mem);
191+
169192
return NULL;
170193
}
171194
EXPORT_SYMBOL(module_adapter_new);
@@ -1284,8 +1307,12 @@ void module_adapter_free(struct comp_dev *dev)
12841307
rfree(mod->priv.cfg.input_pins);
12851308
#endif
12861309

1287-
rfree(mod);
1288-
rfree(dev);
1310+
struct k_heap *mod_heap = mod->priv.resources.heap;
1311+
void *mem = mod->priv.resources.heap_mem;
1312+
1313+
k_heap_free(mod_heap, mod);
1314+
k_heap_free(mod_heap, dev);
1315+
rfree(mem);
12891316
}
12901317
EXPORT_SYMBOL(module_adapter_free);
12911318

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ struct module_resources {
132132
struct list_item cont_chunk_list; /**< Memory container chunks */
133133
size_t heap_usage;
134134
size_t heap_high_water_mark;
135+
struct k_heap *heap;
136+
void *heap_mem;
135137
#if CONFIG_MODULE_MEMORY_API_DEBUG && defined(__ZEPHYR__)
136138
k_tid_t rsrc_mngr;
137139
#endif

0 commit comments

Comments
 (0)