有标准的 memory_resource 分配器 adapter/wrapper 吗?
Is there a standard memory_resource allocator adapter/wrapper?
我很惊讶没有看到标准或其他任何人(?)提供 adapter/wrapper 使标准分配器看起来像 std::memory_resource
。我错过了什么吗?是不是有点像
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?
}
};
std
或 Boost 或其他地方是否提供了这样的包装器?我没有找到它。我正在使用 tbb::scalable_allocator
并希望能够将它与 std::pmr::vector<T>
.
一起使用
目前,我认为它仍处于试验阶段:https://en.cppreference.com/w/cpp/experimental/resource_adaptor。
从最简单的意义上说,您的实施似乎是正确的。您可以通过进行动态转换并返回
的结果来使其更高级
this->m_allocator == cast_other->m_allocator
我很惊讶没有看到标准或其他任何人(?)提供 adapter/wrapper 使标准分配器看起来像 std::memory_resource
。我错过了什么吗?是不是有点像
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?
}
};
std
或 Boost 或其他地方是否提供了这样的包装器?我没有找到它。我正在使用 tbb::scalable_allocator
并希望能够将它与 std::pmr::vector<T>
.
目前,我认为它仍处于试验阶段:https://en.cppreference.com/w/cpp/experimental/resource_adaptor。
从最简单的意义上说,您的实施似乎是正确的。您可以通过进行动态转换并返回
的结果来使其更高级this->m_allocator == cast_other->m_allocator