-
Notifications
You must be signed in to change notification settings - Fork 349
module: memory: partition out memory APIs from generic.c #10426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors module memory management by extracting memory-related APIs from generic.c into two new dedicated files: memory-common.c and memory-heap.c. This reorganization improves code modularity and facilitates future abstractions for module memory management and userspace integration.
Key changes:
- Extracted heap-related memory operations into
memory-heap.c - Moved common memory initialization and wrapper functions to
memory-common.c - Updated build configurations to include the new source files
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/audio/module_adapter/module/memory-heap.c |
New file containing heap allocation/deallocation functions previously in generic.c |
src/audio/module_adapter/module/memory-common.c |
New file containing module initialization and memory API wrappers |
src/audio/module_adapter/module/generic.c |
Removed memory management code that was moved to new files |
src/audio/module_adapter/module_adapter.c |
Removed module_adapter_mem_alloc and module_adapter_mem_free functions (moved to memory-heap.c) |
src/include/sof/audio/module_adapter/module/generic.h |
Added declarations for newly exposed functions |
src/audio/module_adapter/CMakeLists.txt |
Updated to compile new memory source files |
test/cmocka/src/audio/*/CMakeLists.txt |
Updated test build configurations to link new memory source files |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Author: Marcin Rajwa <[email protected]> | ||
|
|
||
| /* | ||
| * \file generic.c |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file header comment incorrectly references 'generic.c'. This should be updated to 'memory-heap.c' to accurately reflect the current filename.
| * \file generic.c | |
| * \file memory-heap.c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
| // Author: Marcin Rajwa <[email protected]> | ||
|
|
||
| /* | ||
| * \file generic.c |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file header comment incorrectly references 'generic.c'. This should be updated to 'memory-common.c' to accurately reflect the current filename.
| * \file generic.c | |
| * \file memory-common.c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
| void *mod_heap_buf = mod_heap_mem + heap_prefix_size; | ||
|
|
||
| k_heap_init(mod_heap, mod_heap_buf, heap_size - heap_prefix_size); | ||
|
|
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module_adapter_dp_heap_new function is missing the Zephyr-specific initialization code that was present in the original implementation (lines setting mod_heap->heap.init_mem and mod_heap->heap.init_bytes under #ifdef __ZEPHYR__). This code should be restored after line 405 to maintain compatibility with Zephyr builds.
| #ifdef __ZEPHYR__ | |
| mod_heap->heap.init_mem = mod_heap_buf; | |
| mod_heap->heap.init_bytes = heap_size - heap_prefix_size; | |
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is - fixed.
| // Author: Marcin Rajwa <[email protected]> | ||
|
|
||
| /* | ||
| * \file generic.c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems valid.
|
I can understand extracting functions related to memory allocation ( |
|
|
||
| LOG_MODULE_DECLARE(module_adapter, CONFIG_SOF_LOG_LEVEL); | ||
|
|
||
| int module_init(struct processing_module *mod) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function does not appear to be related to memory. Can you explain why it was moved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one doesn't do any memory allocations directly, they are done via other calls so its quite generic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lgirdwood I agree with @softwarecki on this - even though this function does call mod_resource_init() and sets mod->priv.resources.rsrc_mngr its main function is to call interface->init() so it should stay together with other functions, calling other methods in struct module_interface like module_process_sink_src(), module_reset() etc. - in generic.c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to avoid duplication of this function, i.e. the same code can be used with with both heap and virtual region
| * Like comp_data_blob_handler_new() but the handler is automatically freed. | ||
| */ | ||
| #if CONFIG_COMP_BLOB | ||
| struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_module *mod) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems this function has nothing to do with heap and fits better under the memory module. I assume the reason for placing it here is that it uses the static container_get and therefore must stay in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does - virtual regions wont need to track and will allocate differently.
| } | ||
| EXPORT_SYMBOL(mod_free_all); | ||
|
|
||
| int module_free(struct processing_module *mod) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function does not appear to be related to memory. Can you explain why it was moved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does some memory freeing, not quite the opposite of init() which is common.
| void *mod_heap_buf = mod_heap_mem + heap_prefix_size; | ||
|
|
||
| k_heap_init(mod_heap, mod_heap_buf, heap_size - heap_prefix_size); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems valid.
| return mod_heap; | ||
| } | ||
|
|
||
| struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems this function has nothing to do with heap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does many allocations and creates a heap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The heap is created by function module_adapter_dp_heap_new. The file name suggests that functions operating on the heap should be placed here. This function only uses the allocator and does not create it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and none of this is needed for regions, it is heap only based logic.
| return NULL; | ||
| } | ||
|
|
||
| void module_adapter_mem_free(struct processing_module *mod) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above, we are freeing memory here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function only calls the memory release routine. It does not operate on the heap directly but simply use sof_heap_free. Based on this logic, we could move here any function that invokes any function related to memory allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we wont always be using a heap.
Move all the module memory APIs into common and heap specific files with no other changes. This is to enable easier abstraction and partitioning around memory for modules and userspace going forward. Signed-off-by: Liam Girdwood <[email protected]>
8ebc3c4 to
e5ca01c
Compare
softwarecki
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that these two functions are the only places in module_adapter where sof_heap_alloc and sof_heap_free are called. I assume this was the criterion for moving them. Does the plan include forking these new files later to use the new allocator you are working on? If so, feel free to admit it, I will not mind at all.
| return NULL; | ||
| } | ||
|
|
||
| void module_adapter_mem_free(struct processing_module *mod) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function only calls the memory release routine. It does not operate on the heap directly but simply use sof_heap_free. Based on this logic, we could move here any function that invokes any function related to memory allocation.
| return mod_heap; | ||
| } | ||
|
|
||
| struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The heap is created by function module_adapter_dp_heap_new. The file name suggests that functions operating on the heap should be placed here. This function only uses the allocator and does not create it.
Yes your right, so anywhere we use the sof_heap_alloc()/free() is where I'm splitting today. However, the vregion is partitioned to include zephyr heaps for interim usage so it may well be as we integrate more code from memory-heap.c to memory-common.c as we go, its just we can easily do this today without breaking too much (and we depend on kernel patches too). Hence the need for a memory-region.c in a future PR which we can then incrementally move common code in memory-common.c and keep the heap/region specific code apart where needed. |
Move all the module memory APIs into common and heap specific files with no other changes.
This is to enable easier abstraction and partitioning around memory for modules and userspace going forward.