Skip to content

buffer: extend ability to allocate on specific heap to all functions #10719

Open
kv2019i wants to merge 2 commits intothesofproject:mainfrom
kv2019i:202604-rbrealloc
Open

buffer: extend ability to allocate on specific heap to all functions #10719
kv2019i wants to merge 2 commits intothesofproject:mainfrom
kv2019i:202604-rbrealloc

Conversation

@kv2019i
Copy link
Copy Markdown
Collaborator

@kv2019i kv2019i commented Apr 23, 2026

Complete making buffer.h compatible with user-space work.

Tested as part of the bigger #10558

Copy link
Copy Markdown
Contributor

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

Extends heap-specific allocation support for audio buffers to improve Zephyr userspace compatibility by removing rbrealloc*() usage and switching buffer allocations/frees to sof_heap_alloc() / sof_heap_free().

Changes:

  • Removed rbrealloc_align() API and its platform/test implementations.
  • Updated comp_buffer allocation/free paths to use heap-aware sof_heap_alloc() / sof_heap_free().
  • Simplified buffer resize logic to allocate/free explicitly instead of reallocating.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
zephyr/lib/alloc.c Removes Zephyr-side rbrealloc_align() implementation (API cleanup).
zephyr/include/rtos/alloc.h Removes rbrealloc*() declarations from Zephyr allocation API.
posix/include/rtos/alloc.h Removes rbrealloc*() declarations from POSIX allocation API.
src/audio/buffers/comp_buffer.c Switches buffer data allocation/free + resize to heap-aware APIs.
src/lib/alloc.c Removes non-Zephyr rbrealloc_align() implementation.
src/platform/library/lib/alloc.c Removes platform library rbrealloc_align() implementation.
test/cmocka/src/common_mocks.c Removes mock rbrealloc_align() implementation to match API removal.

@@ -341,14 +341,8 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen
if (size == audio_stream_get_size(&buffer->stream))
return 0;

Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

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

In buffer_set_size(), when callers pass alignment==0 (e.g. module_adapter.c and copier_ipcgtw.c do), this now forwards 0 to sof_heap_alloc(). Previously the alignment==0 path used rbrealloc() which implicitly aligned to PLATFORM_DCACHE_ALIGN. To preserve behavior and avoid potentially invalid/insufficient alignment from the underlying heap, map alignment==0 to PLATFORM_DCACHE_ALIGN (or to audio_stream_get_align()/existing stream alignment) before calling sof_heap_alloc().

Suggested change
if (!alignment)
alignment = PLATFORM_DCACHE_ALIGN;

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The same semantics are now implemented by sof_heap_alloc() so 0 can be passed forward to it.

Comment on lines +387 to 392
for (new_size = preferred_size; new_size >= minimum_size;
new_size -= minimum_size) {
new_ptr = sof_heap_alloc(buffer->audio_buffer.heap, buffer->flags, new_size, alignment);
if (new_ptr)
break;
}
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

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

buffer_set_size_range(): the resize loop decrements new_size until it drops below minimum_size. If all allocations fail, new_ptr stays NULL and new_size will end up < minimum_size (often 0), but the function later calls buffer_init_stream(buffer, new_size). That can set an invalid size despite the earlier validation. Consider handling the “no allocation succeeded” case explicitly: for grow requests return -ENOMEM without changing the stream; for shrink requests skip allocating a new block and just set the stream size to the chosen target (>= minimum_size).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in V2.

Comment on lines +389 to +390
new_ptr = sof_heap_alloc(buffer->audio_buffer.heap, buffer->flags, new_size, alignment);
if (new_ptr)
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

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

In buffer_set_size_range(), alignment is forwarded directly to sof_heap_alloc(). When alignment==0, earlier code used rbrealloc() (i.e., PLATFORM_DCACHE_ALIGN via the rbrealloc wrapper). To avoid potentially allocating buffers with weaker-than-expected alignment, consider normalizing alignment==0 to PLATFORM_DCACHE_ALIGN (or the stream’s current alignment) before the allocation loop.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

sof_heap_alloc() implements these semantics for alignment==0, so no need to add a special case for PLATFORM_DCACHE_ALIGN here.

kv2019i added 2 commits April 23, 2026 17:37
Continue the work in commit 9567234 ("buffer: allocate on specific
heap") and add ability to specify the heap to all buffer interface
functions.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
These interfaces are no longer used anywhere, so they can be
safely removed.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
@kv2019i kv2019i requested a review from jsarha April 23, 2026 14:41
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.

2 participants