diff options
author | Lee Clagett <code@leeclagett.com> | 2020-10-13 15:09:18 +0000 |
---|---|---|
committer | Lee Clagett <code@leeclagett.com> | 2020-10-13 15:09:18 +0000 |
commit | 249eae5f4aad8c90e4e0eae19268fbf74fe05783 (patch) | |
tree | fd4672980a4090fb666f8c003799cfff64537bf9 /contrib/epee/src | |
parent | Merge pull request #6927 (diff) | |
download | monero-249eae5f4aad8c90e4e0eae19268fbf74fe05783.tar.xz |
Allow byte_stream->byte_slice conversion to shrink unused buffer space
Diffstat (limited to 'contrib/epee/src')
-rw-r--r-- | contrib/epee/src/byte_slice.cpp | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/contrib/epee/src/byte_slice.cpp b/contrib/epee/src/byte_slice.cpp index faf7689be..453b63a4c 100644 --- a/contrib/epee/src/byte_slice.cpp +++ b/contrib/epee/src/byte_slice.cpp @@ -36,6 +36,11 @@ #include "byte_slice.h" #include "byte_stream.h" +namespace +{ + const std::size_t page_size = 4096; +} + namespace epee { struct byte_slice_data @@ -173,16 +178,27 @@ namespace epee : byte_slice(adapt_buffer{}, std::move(buffer)) {} - byte_slice::byte_slice(byte_stream&& stream) noexcept + byte_slice::byte_slice(byte_stream&& stream, const bool shrink) : storage_(nullptr), portion_(stream.data(), stream.size()) { - if (stream.size()) + if (portion_.size()) { - std::uint8_t* const data = stream.take_buffer().release() - sizeof(raw_byte_slice); + byte_buffer buf; + if (shrink && page_size <= stream.available()) + { + buf = byte_buffer_resize(stream.take_buffer(), portion_.size()); + if (!buf) + throw std::bad_alloc{}; + portion_ = {buf.get(), portion_.size()}; + } + else // no need to shrink buffer + buf = stream.take_buffer(); + + std::uint8_t* const data = buf.release() - sizeof(raw_byte_slice); new (data) raw_byte_slice{}; storage_.reset(reinterpret_cast<raw_byte_slice*>(data)); } - else + else // empty stream portion_ = nullptr; } |