#include <multibuf.h>
Classes | |
class | const_iterator |
A const std::forward_iterator over the bytes of a MultiBuf . More... | |
class | iterator |
An std::forward_iterator over the bytes of a MultiBuf . More... | |
Public Member Functions | |
MultiBuf (const MultiBuf &)=delete | |
MultiBuf & | operator= (const MultiBuf &)=delete |
constexpr | MultiBuf (MultiBuf &&other) noexcept=default |
MultiBuf & | operator= (MultiBuf &&other) noexcept=default |
void | Release () noexcept |
~MultiBuf ()=default | |
This destructor will acquire a mutex and is not IRQ safe. | |
size_t | size () const |
bool | empty () const |
bool | IsContiguous () const |
std::optional< ByteSpan > | ContiguousSpan () |
std::optional< ConstByteSpan > | ContiguousSpan () const |
iterator | begin () |
Returns an iterator pointing to the first byte of this MultiBuf . | |
const_iterator | begin () const |
Returns a const iterator pointing to the first byte of this MultiBuf . | |
const_iterator | cbegin () const |
Returns a const iterator pointing to the first byte of this MultiBuf . | |
iterator | end () |
Returns an iterator pointing to the end of this MultiBuf . | |
const_iterator | end () const |
Returns a const iterator pointing to the end of this MultiBuf . | |
const_iterator | cend () const |
Returns a const iterator pointing to the end of this MultiBuf . | |
bool | ClaimPrefix (size_t bytes_to_claim) |
bool | ClaimSuffix (size_t bytes_to_claim) |
void | DiscardPrefix (size_t bytes_to_discard) |
void | Slice (size_t begin, size_t end) |
void | Truncate (size_t len) |
void | TruncateAfter (iterator pos) |
std::optional< MultiBuf > | TakePrefix (size_t bytes_to_take) |
std::optional< MultiBuf > | TakeSuffix (size_t bytes_to_take) |
void | PushPrefix (MultiBuf &&front) |
void | PushSuffix (MultiBuf &&tail) |
StatusWithSize | CopyTo (ByteSpan dest, size_t position=0) const |
StatusWithSize | CopyFrom (ConstByteSpan source, size_t position=0) |
StatusWithSize | CopyFromAndTruncate (ConstByteSpan source, size_t position=0) |
void | PushFrontChunk (OwnedChunk &&chunk) |
void | PushBackChunk (OwnedChunk &&chunk) |
OwnedChunk | TakeFrontChunk () |
MultiBufChunks::iterator | InsertChunk (MultiBufChunks::iterator position, OwnedChunk &&chunk) |
std::tuple< MultiBufChunks::iterator, OwnedChunk > | TakeChunk (MultiBufChunks::iterator position) |
constexpr MultiBufChunks & | Chunks () |
Returns a Chunk -oriented view of this MultiBuf . | |
constexpr const MultiBufChunks & | Chunks () const |
Returns a const Chunk -oriented view of this MultiBuf . | |
constexpr const MultiBufChunks & | ConstChunks () const |
Returns a const Chunk -oriented view of this MultiBuf . | |
Static Public Member Functions | |
static MultiBuf | FromChunk (OwnedChunk &&chunk) |
A byte buffer optimized for zero-copy data transfer.
A MultiBuf
consists of multiple Chunk
s of data.
MultiBuf
inherits privately from MultiBufChunks
. This allows one class to provide either a byte-oriented or a Chunk
-oriented interface, and keeps those interfaces separate.
bool pw::multibuf::MultiBuf::ClaimPrefix | ( | size_t | bytes_to_claim | ) |
Attempts to add bytes_to_claim
to the front of this buffer by advancing its range backwards in memory. Returns true
if the operation succeeded.
This will only succeed if the first Chunk
in this buffer points to a section of a region that has unreferenced bytes preceding it. See also Chunk::ClaimPrefix
.
This method will acquire a mutex and is not IRQ safe.
bool pw::multibuf::MultiBuf::ClaimSuffix | ( | size_t | bytes_to_claim | ) |
Attempts to add bytes_to_claim
to the front of this buffer by advancing its range forwards in memory. Returns true
if the operation succeeded.
This will only succeed if the last Chunk
in this buffer points to a section of a region that has unreferenced bytes following it. See also Chunk::ClaimSuffix
.
This method will acquire a mutex and is not IRQ safe.
|
inline |
|
inline |
Copies bytes from the provided buffer into the multibuf.
[in] | source | Data to copy into the MultiBuf . |
[in] | position | Offset in the MultiBuf from which to start. |
embed:rst:leading-asterisk * * .. pw-status-codes:: * * OK: All bytes were copied. The :cpp:class:`pw::StatusWithSize` includes * the number of bytes copied, which is the size of the `MultiBuf`. * * RESOURCE_EXHAUSTED: Some bytes were copied, but the source was larger * than the destination. The :cpp:class:`pw::StatusWithSize` includes the * number of bytes copied. * *
|
inline |
Copies bytes from the provided buffer into this MultiBuf
and truncates it to the end of the copied data. This is a more efficient version of:
[in] | source | Data to copy into the MultiBuf . |
[in] | position | Offset in the MultiBuf from which to start. |
embed:rst:leading-asterisk * * .. pw-status-codes:: * * OK: All bytes were copied and the :cpp:class:`MultiBuf` was truncated. * The :cpp:class:`pw::StatusWithSize` includes the new * :cpp:func:`MultiBuf::size`. * * RESOURCE_EXHAUSTED: Some bytes were copied, but the source buffer was * larger than the :cpp:class:`MultiBuf`. The returned * :cpp:class:`pw::StatusWithSize` includes the number of bytes copied, * which is the size of the :cpp:class:`MultiBuf`. * *
StatusWithSize pw::multibuf::MultiBuf::CopyTo | ( | ByteSpan | dest, |
size_t | position = 0 |
||
) | const |
Copies bytes from the multibuf into the provided buffer.
[out] | dest | Destination into which to copy data from the MultiBuf . |
[in] | position | Offset in the MultiBuf from which to start. |
embed:rst:leading-asterisk * * .. pw-status-codes:: * * OK: All bytes were copied into the destination. The * :cpp:class:`pw::StatusWithSize` includes the number of bytes copied, * which is the size of the :cpp:class:`MultiBuf`. * * RESOURCE_EXHAUSTED: Some bytes were copied, but the * :cpp:class:`MultiBuf` was larger than the destination buffer. The * :cpp:class:`pw::StatusWithSize` includes the number of bytes copied. * *
void pw::multibuf::MultiBuf::DiscardPrefix | ( | size_t | bytes_to_discard | ) |
Shrinks this handle to refer to the data beginning at offset bytes_to_discard
.
Does not modify the underlying data. The discarded memory continues to be held by the underlying region as long as any Chunk
s exist within it. This allows the memory to be later reclaimed using ClaimPrefix
.
This method will acquire a mutex and is not IRQ safe.
bool pw::multibuf::MultiBuf::empty | ( | ) | const |
|
inline |
|
inline |
|
inline |
|
inline |
void pw::multibuf::MultiBuf::PushPrefix | ( | MultiBuf && | front | ) |
|
inline |
|
inlinenoexcept |
Decrements the reference count on the underlying chunks of data and empties this MultiBuf
so that size() == 0
.
Does not modify the underlying data, but may cause it to be deallocated.
This method is equivalent to { MultiBuf _unused = std::move(multibuf); }
This method will acquire a mutex and is not IRQ safe.
|
inline |
void pw::multibuf::MultiBuf::Slice | ( | size_t | begin, |
size_t | end | ||
) |
Shrinks this handle to refer to data in the range begin..<end
.
Does not modify the underlying data. The discarded memory continues to be held by the underlying region as long as any Chunk
s exist within it. This allows the memory to be later reclaimed using ClaimPrefix
or ClaimSuffix
.
This method will acquire a mutex and is not IRQ safe.
|
inline |
|
inline |
Removes the first Chunk
.
This operation does not move any data and is O(1)
.
std::optional< MultiBuf > pw::multibuf::MultiBuf::TakePrefix | ( | size_t | bytes_to_take | ) |
Attempts to shrink this handle to refer to the data beginning at offset bytes_to_take
, returning the first bytes_to_take
bytes as a new MultiBuf
.
If the inner call to AllocateChunkClass
fails, this function will return std::nullopt
and this handle's span will not change.
This method will acquire a mutex and is not IRQ safe.
std::optional< MultiBuf > pw::multibuf::MultiBuf::TakeSuffix | ( | size_t | bytes_to_take | ) |
Attempts to shrink this handle to refer only the first len - bytes_to_take
bytes, returning the last bytes_to_take
bytes as a new MultiBuf
.
If the inner call to AllocateChunkClass
fails, this function will return std::nullopt
and this handle's span will not change.
This method will acquire a mutex and is not IRQ safe.
void pw::multibuf::MultiBuf::Truncate | ( | size_t | len | ) |
Shrinks this handle to refer to only the first len
bytes.
Does not modify the underlying data. The discarded memory continues to be held by the underlying region as long as any Chunk
s exist within it. This allows the memory to be later reclaimed using ClaimSuffix
.
This method will acquire a mutex and is not IRQ safe.
void pw::multibuf::MultiBuf::TruncateAfter | ( | iterator | pos | ) |
Truncates the MultiBuf
after the current iterator. All bytes following the iterator are removed.
Does not modify the underlying data.
This method will acquire a mutex and is not IRQ safe.