diff options
-rw-r--r-- | include/cru/common/io/BufferStream.h | 4 | ||||
-rw-r--r-- | src/common/Buffer.cpp | 10 | ||||
-rw-r--r-- | src/common/io/AutoReadStream.cpp | 1 | ||||
-rw-r--r-- | src/common/io/BufferStream.cpp | 2 |
4 files changed, 15 insertions, 2 deletions
diff --git a/include/cru/common/io/BufferStream.h b/include/cru/common/io/BufferStream.h index d5d165cb..16ba999b 100644 --- a/include/cru/common/io/BufferStream.h +++ b/include/cru/common/io/BufferStream.h @@ -46,7 +46,9 @@ struct BufferStreamOptions { return block_size <= 0 ? kDefaultBlockSize : block_size; } - Index GetMaxBlockCount() const { return total_size_limit / block_size; } + Index GetMaxBlockCount() const { + return total_size_limit / GetBlockSizeOrDefault(); + } }; /** diff --git a/src/common/Buffer.cpp b/src/common/Buffer.cpp index af0871ca..e37fddc7 100644 --- a/src/common/Buffer.cpp +++ b/src/common/Buffer.cpp @@ -58,6 +58,8 @@ void Buffer::AssignBytes(Index dst_offset, std::byte* src, Index src_offset, } void Buffer::ResizeBuffer(Index new_size, bool preserve_used) { + CheckSize(new_size); + if (new_size == 0) { Delete_(); ptr_ = nullptr; @@ -83,6 +85,8 @@ void Buffer::ResizeBuffer(Index new_size, bool preserve_used) { Index Buffer::PushFront(const std::byte* other, Index other_size, bool use_memmove) { + CheckSize(other_size); + auto copy_size = std::min(used_begin_, other_size); if (copy_size) { @@ -105,6 +109,8 @@ bool Buffer::PushBack(std::byte b) { Index Buffer::PushBack(const std::byte* other, Index other_size, bool use_memmove) { + CheckSize(other_size); + auto copy_size = std::min(size_ - used_end_, other_size); if (copy_size) { @@ -117,12 +123,16 @@ Index Buffer::PushBack(const std::byte* other, Index other_size, } Index Buffer::PopFront(Index size) { + CheckSize(size); + auto move = std::min(used_begin_, size); used_begin_ -= move; return move; } Index Buffer::PopFront(std::byte* buffer, Index size, bool use_memmove) { + CheckSize(size); + auto pop_size = std::min(GetUsedSize(), size); if (pop_size) { diff --git a/src/common/io/AutoReadStream.cpp b/src/common/io/AutoReadStream.cpp index edb77a47..bc43fa81 100644 --- a/src/common/io/AutoReadStream.cpp +++ b/src/common/io/AutoReadStream.cpp @@ -6,6 +6,7 @@ namespace cru::io { AutoReadStream::AutoReadStream(Stream* stream, bool auto_delete, const AutoReadStreamOptions& options) { auto buffer_stream_options = options.GetBufferStreamOptions(); + stream_ = stream; size_per_read_ = buffer_stream_options.GetBlockSizeOrDefault(); buffer_stream_ = std::make_unique<BufferStream>(buffer_stream_options); background_thread_ = std::thread(&AutoReadStream::BackgroundThreadRun, this); diff --git a/src/common/io/BufferStream.cpp b/src/common/io/BufferStream.cpp index b2467a17..3060a608 100644 --- a/src/common/io/BufferStream.cpp +++ b/src/common/io/BufferStream.cpp @@ -67,7 +67,7 @@ Index BufferStream::Write(const std::byte* buffer, Index offset, Index size) { } condition_variable_.wait(lock, [this] { - return buffer_list_.size() < max_block_count_ || + return max_block_count_ <= 0 || buffer_list_.size() < max_block_count_ || buffer_list_.back().GetBackFree() > 0; }); |