diff options
author | crupest <crupest@outlook.com> | 2024-05-12 00:19:16 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2024-05-18 22:56:24 +0800 |
commit | c3cf29afa2b2dd3f2e972a25f35ef5840ad1e2f5 (patch) | |
tree | 6260a4143f254e7c974f66453be0be61f9b935f4 /src/common/io/BufferStream.cpp | |
parent | b05c7d76dcfef6d882bc01fea4663a4c2fcdcdf2 (diff) | |
download | cru-c3cf29afa2b2dd3f2e972a25f35ef5840ad1e2f5.tar.gz cru-c3cf29afa2b2dd3f2e972a25f35ef5840ad1e2f5.tar.bz2 cru-c3cf29afa2b2dd3f2e972a25f35ef5840ad1e2f5.zip |
feat: completed AutoReadStream.
NEED TEST: BufferStream, AutoReadStream.
Diffstat (limited to 'src/common/io/BufferStream.cpp')
-rw-r--r-- | src/common/io/BufferStream.cpp | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/common/io/BufferStream.cpp b/src/common/io/BufferStream.cpp index 1becc1d9..b2467a17 100644 --- a/src/common/io/BufferStream.cpp +++ b/src/common/io/BufferStream.cpp @@ -3,11 +3,8 @@ namespace cru::io { BufferStream::BufferStream(const BufferStreamOptions& options) { - block_size_ = - options.block_size <= 0 ? kDefaultBlockSize : options.block_size; - total_size_limit_ = options.total_size_limit < 0 ? kDefaultTotalSizeLimit - : options.total_size_limit; - block_count_limit_ = total_size_limit_ / block_size_; + block_size_ = options.GetBlockSizeOrDefault(); + max_block_count_ = options.GetMaxBlockCount(); eof_ = false; } @@ -33,7 +30,7 @@ Index BufferStream::Read(std::byte* buffer, Index offset, Index size) { return 0; } - auto full = buffer_list_.size() == block_count_limit_; + auto full = max_block_count_ > 0 && buffer_list_.size() == max_block_count_; Index read = 0; @@ -50,7 +47,7 @@ Index BufferStream::Read(std::byte* buffer, Index offset, Index size) { } } - if (full && buffer_list_.size() < block_count_limit_) { + if (full && buffer_list_.size() < max_block_count_) { // By convention, there should be at most one producer waiting. So // notify_one and notify_all should be the same. condition_variable_.notify_one(); @@ -70,7 +67,7 @@ Index BufferStream::Write(const std::byte* buffer, Index offset, Index size) { } condition_variable_.wait(lock, [this] { - return buffer_list_.size() < block_count_limit_ || + return buffer_list_.size() < max_block_count_ || buffer_list_.back().GetBackFree() > 0; }); @@ -78,8 +75,11 @@ Index BufferStream::Write(const std::byte* buffer, Index offset, Index size) { Index written = 0; - while (buffer_list_.size() != block_count_limit_) { + while (true) { if (buffer_list_.back().GetBackFree() == 0) { + if (max_block_count_ > 0 && buffer_list_.size() == max_block_count_) { + break; + } buffer_list_.push_back(Buffer(block_size_)); } auto& stream_buffer = buffer_list_.back(); |