Class that allocates a memory block from the thread-local data stack, and owns it. The block is deallocated by the destructor. This class should be used only on the automatic storage.
The data stack is a pool in which a thread can allocate and deallocate memory in LIFO order. It is handled by an internal lifo_allocator, which in turn in built upon an instance of default_allocator. lifo_array and lifo_buffer allocate on the data stack, so they must respect the LIFO order: only the most recently allocated block can be deallocated or reallocated. Instantiating lifo_array and lifo_buffer on the automatic storage (locally in a function) is always safe, as the language guarantees destruction in reverse order, even inside arrays or aggregate types.
lifo_buffer provides a low-level service, as it allocates untyped raw memory. It allows resizing preserving the content, but it does so memcpy'ng the content when the address of the block changes.
A thread may resize a lifo_buffer only if it is the most recently instantiated lifo_buffer or lifo_array, otherwise the behavior is undefined.
void concat_and_print(const char ** i_strings)
{
while (*i_strings != nullptr)
{
auto const curr_len =
auto const additional_len = strlen(*i_strings);
buff.
resize(curr_len + additional_len + 1);
memcpy(static_cast<char *>(buff.
data()) + curr_len, *i_strings, additional_len + 1);
i_strings++;
}
std::cout << static_cast<char *>(buff.
data()) << std::endl;
}
void* resize |
( |
size_t |
i_new_size | ) |
|
|
inline |
Changes the size of the block, preserving its existing content. If the buffer grows (the new size is bigger than the previous one) the new content has undefined content.
- Parameters
-
i_new_size | size in bytes of the memory block. |
- Returns
- the new address of the block
This function may reallocate the block, so its address may change.
- Precondition
- The behavior is undefined if either:
- this block was not the most recently allocated one on the data stack of the calling thread
Exception guarantee: strong (in case of exception the function has no observable effects).