On 7/01/2020 21:11, Frederick Gotham wrote:
I propose a new allocator that uses global (i.e. static duration) memory.
This looks somewhat like an "arena allocator". You'll find many existing examples using that keyword. When specifically applied to vector, you might also want to look at static_vector: https://www.boost.org/doc/libs/1_72_0/doc/html/boost/container/static_vector...
T *allocate(std::size_t const n) { if (n > t_capacity) throw std::bad_alloc();
return buf; }
Except this is more dangerous than a real arena allocator, since it's single-shot. If the collection tries to make more than one allocation, this will return the same pointer each time, potentially corrupting the data. This makes it incompatible with list-like containers, and possibly risky even with std::vector, depending on how its resize operation is implemented. Using static_vector instead is much safer.