Skip to content

Conversation

@lgirdwood
Copy link
Member

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.

Copilot AI review requested due to automatic review settings December 10, 2025 17:29
Copy link

Copilot AI left a 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
Copy link

Copilot AI Dec 10, 2025

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.

Suggested change
* \file generic.c
* \file memory-heap.c

Copilot uses AI. Check for mistakes.
Copy link
Member Author

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
Copy link

Copilot AI Dec 10, 2025

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.

Suggested change
* \file generic.c
* \file memory-common.c

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems valid.

Copy link
Member Author

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);

Copy link

Copilot AI Dec 10, 2025

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.

Suggested change
#ifdef __ZEPHYR__
mod_heap->heap.init_mem = mod_heap_buf;
mod_heap->heap.init_bytes = heap_size - heap_prefix_size;
#endif

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems valid.

Copy link
Member Author

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems valid.

@softwarecki
Copy link
Collaborator

I can understand extracting functions related to memory allocation (mod_alloc, mod_free, etc.) into a separate file. However, the rest of the changes do not make sense in my opinion. Moving functions like module_init, module_free, module_adapter_mem_alloc, and module_adapter_mem_free seems unnecessary and introduces more confusion. Can you provide a clear justification for these relocations? Could you share what the next steps are in this refactoring? It would help clarify the overall direction.


LOG_MODULE_DECLARE(module_adapter, CONFIG_SOF_LOG_LEVEL);

int module_init(struct processing_module *mod)
Copy link
Collaborator

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?

Copy link
Member Author

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.

Copy link
Collaborator

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

Copy link
Member Author

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)
Copy link
Collaborator

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.

Copy link
Member Author

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)
Copy link
Collaborator

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?

Copy link
Member Author

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);

Copy link
Collaborator

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,
Copy link
Collaborator

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

Copy link
Member Author

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.

Copy link
Collaborator

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.

Copy link
Member Author

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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

Copy link
Member Author

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.

Copy link
Collaborator

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.

Copy link
Member Author

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]>
Copy link
Collaborator

@softwarecki softwarecki left a 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)
Copy link
Collaborator

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,
Copy link
Collaborator

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.

@lgirdwood
Copy link
Member Author

lgirdwood commented Dec 12, 2025

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants