implements std::pmr::memory_resource interface.
This class can be used with Polymorphic Memory Allocator. Forward Linked List has typedef for PMR called pmr_flat_forward_list. This memory resource allows constructing flat forward list on a buffer owned by someone else.
This memory resource can be instantiated to point to a buffer that is owned elsewhere. Using this memory resource you can allocate buffer to a single owned Consequent allocations will throw bad_alloc until buffer is deallocated. On deallocation it checks that deallocated buffer is the same as allocation buffer. It will fail-fast on mismatch. Deallocated buffer can be allocated again. Deallocation does not free the buffer. It is assumed that buffer is owned by someone else.
Sample usage:
In the below example memory resource is instantiated to point to the input buffer. Container is parametrized to use this memory resource. We resize container buffer to consume entire input buffer in one allocation. After that we keep appending data to the container try_* methods that would not attempt to reallocate larger buffer, and consequently are noexcept as long as caller's functor does not throw. Instead they return false when buffer cannot fit new element. Once a try_* function returns false, we know that buffer is filled, and we can exit. To let caller know how much of the buffer was filled with data we will set output_size to the size of part of the buffer used by the inserted elements. Optionally, if we want caller to adopt buffer without verification, we can return offset to the last element in the buffer. Container destructor will deallocate memory back to resource. Memory resource destructor will detach from the buffer. Buffer content remains unattached, and still contains a valid list. Once method returns, caller can examine buffer using flat forward list reference or view or attach it to the container with allocator compatible to how buffer was allocated.
void build_result(char *buffer, size_t buffer_size, size_t *output_size) {
for(;;) {
if(!ffl.try_push_back(<data>, <data_size>) {
break;
}
}
*output_size = ffl.used_capacity();
}
Thread safety:
This class is not thread safe and cannot be used to perform concurent allocations from multiple threads.