-
Notifications
You must be signed in to change notification settings - Fork 349
zephyr: buffer: host: dai: Introduction of functions that allocate a buffer from a range #9145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
72ee157
buffer: Rename buffer_init to buffer_init_stream
softwarecki 303fd7e
buffer: Extract buffer structure initialization from buffer_alloc
softwarecki e60ca9d
buffer: Add buffer range allocation functions
softwarecki 4f08e77
zephyr: dai: Use the buffer_alloc_range function to allocate a buffer
softwarecki 7c63508
zephyr: host: Use the buffer_alloc_range function to allocate a buffer
softwarecki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,45 +32,31 @@ DECLARE_SOF_RT_UUID("buffer", buffer_uuid, 0x42544c92, 0x8e92, 0x4e41, | |
| 0xb6, 0x79, 0x34, 0x51, 0x9f, 0x1c, 0x1d, 0x28); | ||
| DECLARE_TR_CTX(buffer_tr, SOF_UUID(buffer_uuid), LOG_LEVEL_INFO); | ||
|
|
||
| struct comp_buffer *buffer_alloc(uint32_t size, uint32_t caps, uint32_t flags, uint32_t align, | ||
| bool is_shared) | ||
| static struct comp_buffer *buffer_alloc_struct(void *stream_addr, size_t size, uint32_t caps, | ||
| uint32_t flags, bool is_shared) | ||
| { | ||
| struct comp_buffer *buffer; | ||
| void *stream_addr; | ||
|
|
||
| tr_dbg(&buffer_tr, "buffer_alloc()"); | ||
|
|
||
| /* validate request */ | ||
| if (size == 0) { | ||
| tr_err(&buffer_tr, "buffer_alloc(): new size = %u is invalid", | ||
| size); | ||
| return NULL; | ||
| } | ||
| tr_dbg(&buffer_tr, "buffer_alloc_struct()"); | ||
|
|
||
| /* allocate new buffer */ | ||
| enum mem_zone zone = is_shared ? SOF_MEM_ZONE_RUNTIME_SHARED : SOF_MEM_ZONE_RUNTIME; | ||
|
|
||
| buffer = rzalloc(zone, 0, SOF_MEM_CAPS_RAM, sizeof(*buffer)); | ||
|
|
||
| if (!buffer) { | ||
| tr_err(&buffer_tr, "buffer_alloc(): could not alloc structure"); | ||
| tr_err(&buffer_tr, "buffer_alloc_struct(): could not alloc structure"); | ||
| return NULL; | ||
| } | ||
|
|
||
| CORE_CHECK_STRUCT_INIT(buffer, is_shared); | ||
|
|
||
| buffer->is_shared = is_shared; | ||
| stream_addr = rballoc_align(0, caps, size, align); | ||
| if (!stream_addr) { | ||
| rfree(buffer); | ||
| tr_err(&buffer_tr, "buffer_alloc(): could not alloc size = %u bytes of type = %u", | ||
| size, caps); | ||
| return NULL; | ||
| } | ||
| buffer->caps = caps; | ||
|
|
||
| /* From here no more uncached access to the buffer object, except its list headers */ | ||
| audio_stream_set_addr(&buffer->stream, stream_addr); | ||
| buffer_init(buffer, size, caps); | ||
| buffer_init_stream(buffer, size); | ||
|
|
||
| audio_stream_set_underrun(&buffer->stream, !!(flags & SOF_BUF_UNDERRUN_PERMITTED)); | ||
| audio_stream_set_overrun(&buffer->stream, !!(flags & SOF_BUF_OVERRUN_PERMITTED)); | ||
|
|
@@ -81,6 +67,36 @@ struct comp_buffer *buffer_alloc(uint32_t size, uint32_t caps, uint32_t flags, u | |
| return buffer; | ||
| } | ||
|
|
||
| struct comp_buffer *buffer_alloc(size_t size, uint32_t caps, uint32_t flags, uint32_t align, | ||
| bool is_shared) | ||
| { | ||
| struct comp_buffer *buffer; | ||
| void *stream_addr; | ||
|
|
||
| tr_dbg(&buffer_tr, "buffer_alloc()"); | ||
|
|
||
| /* validate request */ | ||
| if (size == 0) { | ||
| tr_err(&buffer_tr, "buffer_alloc(): new size = %zu is invalid", size); | ||
| return NULL; | ||
| } | ||
|
|
||
| stream_addr = rballoc_align(0, caps, size, align); | ||
| if (!stream_addr) { | ||
| tr_err(&buffer_tr, "buffer_alloc(): could not alloc size = %zu bytes of type = %u", | ||
| size, caps); | ||
| return NULL; | ||
| } | ||
|
|
||
| buffer = buffer_alloc_struct(stream_addr, size, caps, flags, is_shared); | ||
| if (!buffer) { | ||
| tr_err(&buffer_tr, "buffer_alloc(): could not alloc buffer structure"); | ||
| rfree(stream_addr); | ||
| } | ||
|
|
||
| return buffer; | ||
| } | ||
|
|
||
| #if CONFIG_ZEPHYR_DP_SCHEDULER | ||
| int buffer_create_shadow_dp_queue(struct comp_buffer *buffer, bool at_input) | ||
| { | ||
|
|
@@ -148,6 +164,49 @@ int buffer_sync_shadow_dp_queue(struct comp_buffer *buffer, size_t limit) | |
| } | ||
| #endif /* CONFIG_ZEPHYR_DP_SCHEDULER */ | ||
|
|
||
| struct comp_buffer *buffer_alloc_range(size_t preferred_size, size_t minimum_size, uint32_t caps, | ||
| uint32_t flags, uint32_t align, bool is_shared) | ||
| { | ||
| struct comp_buffer *buffer; | ||
| size_t size; | ||
| void *stream_addr = NULL; | ||
|
|
||
| tr_dbg(&buffer_tr, "buffer_alloc_range(): %zu -- %zu bytes", minimum_size, preferred_size); | ||
|
|
||
| /* validate request */ | ||
| if (minimum_size == 0 || preferred_size < minimum_size) { | ||
| tr_err(&buffer_tr, "buffer_alloc_range(): new size range %zu -- %zu is invalid", | ||
| minimum_size, preferred_size); | ||
| return NULL; | ||
| } | ||
|
|
||
| /* Align preferred size to a multiple of the minimum size */ | ||
| if (preferred_size % minimum_size) | ||
| preferred_size += minimum_size - preferred_size % minimum_size; | ||
|
|
||
| for (size = preferred_size; size >= minimum_size; size -= minimum_size) { | ||
| stream_addr = rballoc_align(0, caps, size, align); | ||
| if (stream_addr) | ||
| break; | ||
| } | ||
|
|
||
| tr_dbg(&buffer_tr, "buffer_alloc_range(): allocated %zu bytes", size); | ||
|
|
||
| if (!stream_addr) { | ||
| tr_err(&buffer_tr, "buffer_alloc_range(): could not alloc size = %zu bytes of type = %u", | ||
| minimum_size, caps); | ||
| return NULL; | ||
| } | ||
|
|
||
| buffer = buffer_alloc_struct(stream_addr, size, caps, flags, is_shared); | ||
| if (!buffer) { | ||
| tr_err(&buffer_tr, "buffer_alloc_range(): could not alloc buffer structure"); | ||
| rfree(stream_addr); | ||
| } | ||
|
|
||
| return buffer; | ||
| } | ||
|
|
||
| void buffer_zero(struct comp_buffer *buffer) | ||
| { | ||
| buf_dbg(buffer, "stream_zero()"); | ||
|
|
@@ -193,7 +252,64 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen | |
| if (new_ptr) | ||
| buffer->stream.addr = new_ptr; | ||
|
|
||
| buffer_init(buffer, size, buffer->caps); | ||
| buffer_init_stream(buffer, size); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, size_t minimum_size, | ||
| uint32_t alignment) | ||
| { | ||
| void *ptr = audio_stream_get_addr(&buffer->stream); | ||
| const size_t actual_size = audio_stream_get_size(&buffer->stream); | ||
| void *new_ptr = NULL; | ||
| size_t new_size; | ||
|
|
||
| CORE_CHECK_STRUCT(buffer); | ||
|
|
||
| /* validate request */ | ||
| if (minimum_size == 0 || preferred_size < minimum_size) { | ||
| buf_err(buffer, "resize size range %zu -- %zu is invalid", minimum_size, | ||
| preferred_size); | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| /* Align preferred size to a multiple of the minimum size */ | ||
| if (preferred_size % minimum_size) | ||
| preferred_size += minimum_size - preferred_size % minimum_size; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in fact wondering, whether it would be better to align down? Doesn't seem important though |
||
|
|
||
| if (preferred_size == actual_size) | ||
| return 0; | ||
|
|
||
| if (!alignment) { | ||
| for (new_size = preferred_size; new_size >= minimum_size; | ||
| new_size -= minimum_size) { | ||
| new_ptr = rbrealloc(ptr, SOF_MEM_FLAG_NO_COPY, buffer->caps, new_size, | ||
| actual_size); | ||
| if (new_ptr) | ||
| break; | ||
| } | ||
| } else { | ||
| for (new_size = preferred_size; new_size >= minimum_size; | ||
| new_size -= minimum_size) { | ||
| new_ptr = rbrealloc_align(ptr, SOF_MEM_FLAG_NO_COPY, buffer->caps, new_size, | ||
| actual_size, alignment); | ||
| if (new_ptr) | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| /* we couldn't allocate bigger chunk */ | ||
| if (!new_ptr && new_size > actual_size) { | ||
| buf_err(buffer, "resize can't alloc %zu bytes type %u", new_size, buffer->caps); | ||
| return -ENOMEM; | ||
| } | ||
|
|
||
| /* use bigger chunk, else just use the old chunk but set smaller */ | ||
| if (new_ptr) | ||
| buffer->stream.addr = new_ptr; | ||
|
|
||
| buffer_init_stream(buffer, new_size); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.