Add byte_stream for zero-copy serialization, and add support in ZMQ-JSON.

This commit is contained in:
Lee Clagett 2020-03-18 23:22:39 +00:00
parent 8185054db7
commit c26c93019a
14 changed files with 806 additions and 171 deletions

View file

@ -1,4 +1,4 @@
// Copyright (c) 2019, The Monero Project
// Copyright (c) 2019-2020, The Monero Project
//
// All rights reserved.
//
@ -39,6 +39,7 @@
namespace epee
{
struct byte_slice_data;
class byte_stream;
struct release_byte_slice
{
@ -50,6 +51,12 @@ namespace epee
}
};
//! Frees ref count + buffer allocated internally by `byte_buffer`.
struct release_byte_buffer
{
void operator()(std::uint8_t* buf) const noexcept;
};
/*! Inspired by slices in golang. Storage is thread-safe reference counted,
allowing for cheap copies or range selection on the bytes. The bytes
owned by this class are always immutable.
@ -104,6 +111,9 @@ namespace epee
//! Convert `buffer` into a slice using one allocation for shared count.
explicit byte_slice(std::string&& buffer);
//! Convert `stream` into a slice with zero allocations.
explicit byte_slice(byte_stream&& stream) noexcept;
byte_slice(byte_slice&& source) noexcept;
~byte_slice() noexcept = default;
@ -149,5 +159,19 @@ namespace epee
//! \post `empty()` \return Ownership of ref-counted buffer.
std::unique_ptr<byte_slice_data, release_byte_slice> take_buffer() noexcept;
};
//! Alias for a buffer that has space for a `byte_slice` ref count.
using byte_buffer = std::unique_ptr<std::uint8_t, release_byte_buffer>;
/*! \return `buf` with a new size of exactly `length`. New bytes not
initialized. A `nullptr` is returned on allocation failure. */
byte_buffer byte_buffer_resize(byte_buffer buf, std::size_t length) noexcept;
/*! Increase `buf` of size `current` by `more` bytes.
\throw std::range_error if `current + more` exceeds `size_t` bounds.
\return Buffer of `current + more` bytes. A `nullptr` is returned on
allocation failure. */
byte_buffer byte_buffer_increase(byte_buffer buf, std::size_t current, std::size_t more);
} // epee