diff options
author | Lee Clagett <code@leeclagett.com> | 2020-03-18 23:22:39 +0000 |
---|---|---|
committer | Lee Clagett <code@leeclagett.com> | 2020-04-11 04:12:11 +0000 |
commit | c26c93019a52dee52ee865f4f1b23e6929968258 (patch) | |
tree | e17353bec4084e7c502921eea3169e22b7f125e1 /contrib/epee/include/byte_slice.h | |
parent | Merge pull request #6451 (diff) | |
download | monero-c26c93019a52dee52ee865f4f1b23e6929968258.tar.xz |
Add byte_stream for zero-copy serialization, and add support in ZMQ-JSON.
Diffstat (limited to 'contrib/epee/include/byte_slice.h')
-rw-r--r-- | contrib/epee/include/byte_slice.h | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/contrib/epee/include/byte_slice.h b/contrib/epee/include/byte_slice.h index bd9452b11..6b79f6d92 100644 --- a/contrib/epee/include/byte_slice.h +++ b/contrib/epee/include/byte_slice.h @@ -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 |