This commit is contained in:
Siarhei Fedartsou 2024-07-11 20:42:47 +02:00
parent 18b3c5f8ed
commit 9ef1911eae

View File

@ -22,9 +22,13 @@ template <typename T, size_t MinItemsInBlock = 1024>
class MemoryManager class MemoryManager
{ {
public: public:
static MemoryManager &instance() static std::shared_ptr<MemoryManager> instance()
{ {
static thread_local MemoryManager instance; static thread_local std::shared_ptr<MemoryManager> instance;
if (!instance)
{
instance = std::shared_ptr<MemoryManager>(new MemoryManager());
}
return instance; return instance;
} }
@ -59,10 +63,10 @@ public:
~MemoryManager() ~MemoryManager()
{ {
// std::cerr << "~MemoryManager()" << std::endl; // std::cerr << "~MemoryManager()" << std::endl;
// for (auto block : blocks_) for (auto block : blocks_)
// { {
// std::free(block); std::free(block);
// } }
} }
private: private:
MemoryManager() = default; MemoryManager() = default;
@ -105,10 +109,10 @@ class PoolAllocator
public: public:
using value_type = T; using value_type = T;
PoolAllocator() noexcept = default; PoolAllocator() noexcept : pool(MemoryManager<T, MinItemsInBlock>::instance()) {};
template <typename U> template <typename U>
PoolAllocator(const PoolAllocator<U> &) noexcept {} PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryManager<T, MinItemsInBlock>::instance()) {}
template <typename U> template <typename U>
struct rebind struct rebind
@ -118,12 +122,12 @@ public:
T *allocate(std::size_t n) T *allocate(std::size_t n)
{ {
return MemoryManager<T, MinItemsInBlock>::instance().allocate(n); return pool->allocate(n);
} }
void deallocate(T *p, std::size_t n) noexcept void deallocate(T *p, std::size_t n) noexcept
{ {
MemoryManager<T, MinItemsInBlock>::instance().deallocate(p, n); pool->deallocate(p, n);
} }
~PoolAllocator() = default; ~PoolAllocator() = default;
@ -134,6 +138,8 @@ public:
PoolAllocator &operator=(PoolAllocator &&) noexcept = default; PoolAllocator &operator=(PoolAllocator &&) noexcept = default;
private: private:
std::shared_ptr<MemoryManager<T, MinItemsInBlock>> pool;
static size_t get_next_power_of_two_exponent(size_t n) static size_t get_next_power_of_two_exponent(size_t n)
{ {
BOOST_ASSERT(n > 0); BOOST_ASSERT(n > 0);