26 #ifndef Common_DynamicBuffer_h
27 #define Common_DynamicBuffer_h
53 :
size(initial_size),
own(own_buffer) {
95 void reserve(
size_t len,
bool nocopy =
false) {
110 memcpy(
ptr, data, len);
122 uint8_t *
add(
const void *data,
size_t len) {
132 void set(
const void *data,
size_t len) {
163 uint8_t *rbuf =
base;
176 void grow(
size_t new_size,
bool nocopy =
false) {
177 uint8_t *new_buf =
new uint8_t[new_size];
215 #endif // Common_DynamicBuffer_h
void free()
Frees resources.
bool empty() const
Returns true if the buffer is empty.
void set_mark()
Sets the mark; the mark can be used by the caller just like a bookmark.
std::shared_ptr< DynamicBuffer > DynamicBufferPtr
DynamicBuffer(size_t initial_size=0, bool own_buffer=true)
Constructor.
uint8_t * ptr
Pointer to the end of the used part of the buffer.
A dynamic, resizable and reference counted memory buffer.
void grow(size_t new_size, bool nocopy=false)
Grows the buffer and copies the data unless nocopy is true.
uint8_t * add(const void *data, size_t len)
Adds more data WITH boundary checks; if required the buffer is resized and existing data is preserved...
~DynamicBuffer()
Destructor; releases the buffer if it "owns" it.
uint32_t size
The size of the allocated memory buffer (base)
bool own
If true then the buffer (base) will be released when going out of scope; if false then the caller has...
void clear()
Clears the buffer.
uint8_t * base
Pointer to the allocated memory buffer.
size_t fill() const
Returns the size of the used portion.
void set(const void *data, size_t len)
Overwrites the existing data.
uint8_t * release(size_t *lenp=0)
Moves ownership of the buffer to the caller.
uint8_t * mark
A "bookmark", can be set by the caller.
void ensure(size_t len)
Ensure space for additional data Will grow the space to 1.5 of the needed space with existing data un...
uint8_t * add_unchecked(const void *data, size_t len)
Adds additional data without boundary checks.
size_t remaining() const
Returns the size of the unused portion.
void reserve(size_t len, bool nocopy=false)
Reserve space for additional data Will grow the space to exactly what's needed.