From 9ef1911eae6293591c3d89d7bf111a4f14a3c6fa Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Thu, 11 Jul 2024 20:42:47 +0200 Subject: [PATCH] wip --- include/util/pool_allocator.hpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/include/util/pool_allocator.hpp b/include/util/pool_allocator.hpp index 0c96c0808..0b48adf16 100644 --- a/include/util/pool_allocator.hpp +++ b/include/util/pool_allocator.hpp @@ -22,9 +22,13 @@ template class MemoryManager { public: - static MemoryManager &instance() + static std::shared_ptr instance() { - static thread_local MemoryManager instance; + static thread_local std::shared_ptr instance; + if (!instance) + { + instance = std::shared_ptr(new MemoryManager()); + } return instance; } @@ -58,11 +62,11 @@ public: ~MemoryManager() { - // std::cerr << "~MemoryManager()" << std::endl; - // for (auto block : blocks_) - // { - // std::free(block); - // } + // std::cerr << "~MemoryManager()" << std::endl; + for (auto block : blocks_) + { + std::free(block); + } } private: MemoryManager() = default; @@ -105,10 +109,10 @@ class PoolAllocator public: using value_type = T; - PoolAllocator() noexcept = default; + PoolAllocator() noexcept : pool(MemoryManager::instance()) {}; template - PoolAllocator(const PoolAllocator &) noexcept {} + PoolAllocator(const PoolAllocator &) noexcept : pool(MemoryManager::instance()) {} template struct rebind @@ -118,12 +122,12 @@ public: T *allocate(std::size_t n) { - return MemoryManager::instance().allocate(n); + return pool->allocate(n); } void deallocate(T *p, std::size_t n) noexcept { - MemoryManager::instance().deallocate(p, n); + pool->deallocate(p, n); } ~PoolAllocator() = default; @@ -134,6 +138,8 @@ public: PoolAllocator &operator=(PoolAllocator &&) noexcept = default; private: + std::shared_ptr> pool; + static size_t get_next_power_of_two_exponent(size_t n) { BOOST_ASSERT(n > 0);