Seastar
High performance C++ framework for concurrent servers
|
Temporary, self-managed byte buffer.
A temporary_buffer
is similar to an std::string
or a std::unique_ptr<char[]>
, but provides more flexible memory management. A temporary_buffer
can own the memory it points to, or it can be shared with another temporary_buffer
, or point at a substring of a buffer. It uses a deleter to manage its memory.
A temporary_buffer
should not be held indefinitely. It can be held while a request is processed, or for a similar duration, but not longer, as it can tie up more memory that its size indicates.
A buffer can be shared: two temporary_buffer
objects will point to the same data, or a subset of it. See the temporary_buffer::share() method.
Unless you created a temporary_buffer
yourself, do not modify its contents, as they may be shared with another user that does not expect the data to change.
Use cases for a temporary_buffer
include:
CharType | underlying character type (must be a variant of char ). |
#include <seastar/core/temporary_buffer.hh>
Public Member Functions | |
temporary_buffer (size_t size) | |
temporary_buffer () noexcept | |
Creates an empty temporary_buffer that does not point at anything. | |
temporary_buffer (const temporary_buffer &)=delete | |
temporary_buffer (temporary_buffer &&x) noexcept | |
Moves a temporary_buffer . | |
temporary_buffer (CharType *buf, size_t size, deleter d) noexcept | |
temporary_buffer (const CharType *src, size_t size) | |
void | operator= (const temporary_buffer &)=delete |
temporary_buffer & | operator= (temporary_buffer &&x) noexcept |
Moves a temporary_buffer . | |
const CharType * | get () const noexcept |
Gets a pointer to the beginning of the buffer. | |
CharType * | get_write () noexcept |
size_t | size () const noexcept |
Gets the buffer size. | |
const CharType * | begin () const noexcept |
Gets a pointer to the beginning of the buffer. | |
const CharType * | end () const noexcept |
Gets a pointer to the end of the buffer. | |
temporary_buffer | prefix (size_t size) &&noexcept |
CharType | operator[] (size_t pos) const noexcept |
bool | empty () const noexcept |
Checks whether the buffer is empty. | |
operator bool () const noexcept | |
Checks whether the buffer is not empty. | |
temporary_buffer | share () |
temporary_buffer | share (size_t pos, size_t len) |
temporary_buffer | clone () const |
void | trim_front (size_t pos) noexcept |
void | trim (size_t pos) noexcept |
deleter | release () noexcept |
bool | operator== (const temporary_buffer &o) const noexcept |
bool | operator!= (const temporary_buffer &o) const noexcept |
Static Public Member Functions | |
static temporary_buffer | aligned (size_t alignment, size_t size) |
static temporary_buffer | copy_of (std::string_view view) |
|
inlineexplicit |
Creates a temporary_buffer
of a specified size. The buffer is not shared with anyone, and is not initialized.
size | buffer size, in bytes |
|
inlinenoexcept |
Creates a temporary_buffer
with a specific deleter.
buf | beginning of the buffer held by this temporary_buffer |
size | size of the buffer |
d | deleter controlling destruction of the buffer. The deleter will be destroyed when there are no longer any users for the buffer. |
|
inline |
Creates a temporary_buffer
containing a copy of the provided data
src | data buffer to be copied |
size | size of data buffer in src |
|
inlinestatic |
Creates a temporary_buffer
object with a specified size, with memory aligned to a specific boundary.
alignment | Required alignment; must be a power of two and a multiple of sizeof(void *). |
size | Required size; must be a multiple of alignment. |
temporary_buffer
object.
|
inline |
Clone the current temporary_buffer
object into a new one. This creates a temporary buffer with the same length and data but not pointing to the memory of the original object.
|
inlinenoexcept |
Gets a writable pointer to the beginning of the buffer. Use only when you are certain no user expects the buffer data not to change.
|
inlinenoexcept |
Compare contents of this buffer with another buffer for inequality
o | buffer to compare with |
|
inlinenoexcept |
Compare contents of this buffer with another buffer for equality
o | buffer to compare with |
|
inlinenoexcept |
Reads a character from a specific position in the buffer.
pos | position to read character from; must be less than size. |
|
inlinenoexcept |
Returns the buffer, but with a reduced size. The original buffer is consumed by this call and can no longer be used.
size | New size; must be smaller than current size. |
|
inlinenoexcept |
Stops automatic memory management. When the temporary_buffer
object is destroyed, the underlying deleter will not be called. Instead, it is the caller's responsibility to destroy the deleter object when the data is no longer needed.
|
inline |
Create a new temporary_buffer
object referring to the same underlying data. The underlying deleter will not be destroyed until both the original and the clone have been destroyed.
|
inline |
Create a new temporary_buffer
object referring to a substring of the same underlying data. The underlying deleter will not be destroyed until both the original and the clone have been destroyed.
pos | Position of the first character to share. |
len | Length of substring to share. |
|
inlinenoexcept |
Remove a suffix from the buffer. The underlying data is not modified.
pos | Position of first character to drop. |
|
inlinenoexcept |
Remove a prefix from the buffer. The underlying data is not modified.
pos | Position of first character to retain. |