- allocate chunks of memory on any buffers (stack allocated, heap allocated)
- deallocate memory based on just a pointer
- have minimal fragmentation during allocation
- perform allocation in logarithmic time - log n
To use you will need std::pmr::polymorphic_allocator<T>
which is available in the std library only with C++17 or later.
Here's an example of using a 1040 byte buffer to create a 256-element std::vector
:
size_t n = 1040;
std::byte* buffer = new std::byte[n]; // buffer creation
gmf_memory_resource mr { buffer, buffer + n }; // create gmf_memory_resource based on a given buffer
const std::pmr::polymorphic_allocator<int> allocator { &mr }; // create allocator based on a given memory resource
std::vector<int, std::pmr::polymorphic_allocator<int>> vec { allocator }; // creating a vector based on a given allocator
vec.resize(256); // it's ok
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.