diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-02-05 14:27:06 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-02-05 14:27:06 +1100 |
| commit | cabfd4b75ecc733bdf36997606a686c4d2bc277d (patch) | |
| tree | a18e7debe79b3e229a3ee2c029ccbc58fa381673 /src/audio/chunk.cpp | |
| parent | 9eecf78e08e26b488e6a88947611eb89e9870fb0 (diff) | |
| download | tangara-fw-cabfd4b75ecc733bdf36997606a686c4d2bc277d.tar.gz | |
fix pipeline heap corruption and chunk ignores
Diffstat (limited to 'src/audio/chunk.cpp')
| -rw-r--r-- | src/audio/chunk.cpp | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/audio/chunk.cpp b/src/audio/chunk.cpp index abc42bc7..61c0dc2b 100644 --- a/src/audio/chunk.cpp +++ b/src/audio/chunk.cpp @@ -14,10 +14,14 @@ namespace audio { +static const std::size_t kWorkingBufferMultiple = 2; + ChunkReader::ChunkReader(std::size_t chunk_size) : raw_working_buffer_(static_cast<std::byte*>( - heap_caps_malloc(chunk_size * 2, MALLOC_CAP_SPIRAM))), - working_buffer_(raw_working_buffer_, chunk_size * 1.5) {} + heap_caps_malloc(chunk_size * kWorkingBufferMultiple, + MALLOC_CAP_SPIRAM))), + working_buffer_(raw_working_buffer_, + chunk_size * kWorkingBufferMultiple) {} ChunkReader::~ChunkReader() { free(raw_working_buffer_); @@ -29,8 +33,8 @@ auto ChunkReader::HandleNewData(cpp::span<std::byte> data) // Copy the new data onto the front for anything that was left over from the // last portion. Note: this could be optimised for the '0 leftover bytes' // case, which technically shouldn't need a copy. - cpp::span<std::byte> new_data_dest = working_buffer_.subspan(leftover_bytes_); - std::copy(data.begin(), data.end(), new_data_dest.begin()); + std::copy(data.begin(), data.end(), + working_buffer_.begin() + leftover_bytes_); last_data_in_working_buffer_ = working_buffer_.first(leftover_bytes_ + data.size()); leftover_bytes_ = 0; @@ -39,6 +43,11 @@ auto ChunkReader::HandleNewData(cpp::span<std::byte> data) auto ChunkReader::HandleLeftovers(std::size_t bytes_used) -> void { leftover_bytes_ = last_data_in_working_buffer_.size() - bytes_used; + + // Ensure that we don't have more than a chunk of leftever bytes. This is + // bad, because we probably won't have enough data to store the next chunk. + assert(leftover_bytes_ <= working_buffer_.size() / kWorkingBufferMultiple); + if (leftover_bytes_ > 0) { auto data_to_keep = last_data_in_working_buffer_.last(leftover_bytes_); std::copy(data_to_keep.begin(), data_to_keep.end(), |
