Mod alloc ctx wrapper#10802
Open
jsarha wants to merge 4 commits into
Open
Conversation
added 4 commits
May 21, 2026 18:20
Add a small header with inline wrapper functions that abstract the vregion vs heap allocation decision based on a mod_alloc_ctx context: - sof_ctx_alloc(): full allocation with flags and alignment, uses vregion_alloc_coherent_align() or vregion_alloc_align() when a vregion is available, falls back to sof_heap_alloc() otherwise. - sof_ctx_zalloc(): simplified zero-flag, zero-alignment shorthand. - sof_ctx_free(): matching free function. These replace the repeated if/else pattern found throughout the buffer and module allocator code. Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
Replace the open-coded vregion vs heap allocation and free patterns in z_impl_mod_alloc_ext() and free_contents() with the new sof_ctx_alloc() and sof_ctx_free() wrappers. Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
Replace open-coded vregion vs heap allocation and free patterns in buffer_alloc(), buffer_alloc_range(), buffer_alloc_struct(), buffer_free(), buffer_set_size(), and buffer_set_size_range() with the new sof_ctx_alloc() and sof_ctx_free() wrappers. Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
Replace open-coded vregion vs heap allocation and free patterns in ring_buffer_free() and ring_buffer_create() with the new sof_ctx_alloc() and sof_ctx_free() wrappers. Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a small allocation/free wrapper around struct mod_alloc_ctx to centralize the repeated “heap vs vregion” allocation pattern, and updates several call sites to use it.
Changes:
- Added
sof_ctx_alloc()/sof_ctx_free()helpers for allocating/freeing from amod_alloc_ctx(heap + optional vregion). - Replaced repeated
if (!alloc->vreg) ... else ...allocation/free logic in module adapter generic allocations, ring buffer, and comp buffer code. - Added a
sof_ctx_zalloc()helper (but it currently does not zero-initialize memory; see comments).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/include/sof/ctx_alloc.h | Adds the new mod_alloc_ctx allocation/free helper API. |
| src/audio/module_adapter/module/generic.c | Switches module allocation/free paths to the new ctx helpers. |
| src/audio/buffers/ring_buffer.c | Uses ctx helpers for ring buffer struct/data allocations and frees. |
| src/audio/buffers/comp_buffer.c | Uses ctx helpers for userspace-LL buffer stream allocations and frees. |
Comment on lines
+41
to
+50
| /** | ||
| * Allocate zero-initialized memory from a mod_alloc_ctx context. | ||
| * @param ctx Allocation context. | ||
| * @param size Size in bytes. | ||
| * @return Pointer to allocated memory or NULL on failure. | ||
| */ | ||
| static inline void *sof_ctx_zalloc(struct mod_alloc_ctx *ctx, size_t size) | ||
| { | ||
| return sof_ctx_alloc(ctx, 0, size, 0); | ||
| } |
Comment on lines
+34
to
+37
| if (flags & SOF_MEM_FLAG_COHERENT) | ||
| return vregion_alloc_coherent_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM, | ||
| size, alignment); | ||
|
|
lyakh
requested changes
May 22, 2026
| */ | ||
| static inline void *sof_ctx_zalloc(struct mod_alloc_ctx *ctx, size_t size) | ||
| { | ||
| return sof_ctx_alloc(ctx, 0, size, 0); |
Collaborator
There was a problem hiding this comment.
this seems rather wrong and incomplete. If you do want such a wrapper, you'd want it with flags and alignment too. And memset() of course
| buffer = vregion_alloc_coherent(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer)); | ||
| else | ||
| buffer = vregion_alloc(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer)); | ||
| buffer = sof_ctx_alloc(alloc, flags, sizeof(*buffer), 0); |
Collaborator
There was a problem hiding this comment.
I think you wanted to remove lines 215-216 too
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It started to look like the "if (!res->alloc->vreg)" -pattern will fill up the universe, so thought we'd better do something about it.
I've tested this code on top of main, and I've also tested it as a base for our current ll-userspace feature thread. The both appear to work Ok.