fixup! refactor: remove libtransmission::Buffer (#5676) (#5748)

This commit is contained in:
Charles Kerr 2023-07-08 02:25:57 -05:00 committed by GitHub
parent 97da2adbca
commit ec54e7f11e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 1 deletions

View File

@ -264,7 +264,22 @@ public:
virtual std::pair<value_type*, size_t> reserve_space(size_t n_bytes) override
{
buf_.resize(end_pos_ + n_bytes);
if (auto const free_at_end = buf_.size() - end_pos_; free_at_end < n_bytes)
{
if (auto const total_free = begin_pos_ + free_at_end; total_free >= n_bytes)
{
// move data so that all free space is at the end
auto const size = this->size();
std::copy(data(), data() + size, std::data(buf_));
begin_pos_ = 0;
end_pos_ = size;
}
else // even `total_free` is not enough, so resize
{
buf_.resize(end_pos_ + n_bytes);
}
}
return { buf_.data() + end_pos_, n_bytes };
}