I'm surprised not to see the standard or anyone else(?) providing an adapter/wrapper to make standard allocators look like a std::memory_resource
. Am I missing something? Is it just something along the lines of
template <typename ByteAllocator>
class AllocatorMemoryResource : public std::memory_resource {
ByteAllocator m_allocator;
void* do_allocate(std::size_t bytes, std::size_t alignment) override {
// Do something to align the storage???
return static_cast<void*>(m_allocator.allocate(bytes));
}
void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override {
m_allocator.deallocate(p, bytes);
}
bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
return this == &other; // Right?
}
};
Is a wrapper like this provided in std
or Boost or somewhere else? I wasn't finding it. I'm using tbb::scalable_allocator
and would like to be able to use it with a std::pmr::vector<T>
.
For now, I believe it is still experimental: https://en.cppreference.com/w/cpp/experimental/resource_adaptor.
Your implementation seems to be correct, in the simplest sense. You could make it more advanced by doing a dynamic cast, and returning the result of
this->m_allocator == cast_other->m_allocator