diff --git a/src/oatpp/core/base/StrBuffer.hpp b/src/oatpp/core/base/StrBuffer.hpp index b00513a3..cb3d38c8 100644 --- a/src/oatpp/core/base/StrBuffer.hpp +++ b/src/oatpp/core/base/StrBuffer.hpp @@ -41,8 +41,12 @@ private: static constexpr v_buff_size SM_STRING_POOL_ENTRY_SIZE = 256; static oatpp::base::memory::ThreadDistributedMemoryPool& getSmallStringPool() { - static oatpp::base::memory::ThreadDistributedMemoryPool pool("Small_String_Pool", SM_STRING_POOL_ENTRY_SIZE, 16); - return pool; + static std::once_flag flag; + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; + std::call_once(flag, []() { + pool = new oatpp::base::memory::ThreadDistributedMemoryPool("Small_String_Pool", SM_STRING_POOL_ENTRY_SIZE, 16); + }); + return *pool; } static v_buff_size getSmStringBaseSize() { diff --git a/src/oatpp/core/base/memory/Allocator.hpp b/src/oatpp/core/base/memory/Allocator.hpp index f0a8e21c..b8a07718 100644 --- a/src/oatpp/core/base/memory/Allocator.hpp +++ b/src/oatpp/core/base/memory/Allocator.hpp @@ -67,8 +67,12 @@ public: const AllocatorPoolInfo& m_poolInfo; public: static oatpp::base::memory::ThreadDistributedMemoryPool& getPool(const AllocatorPoolInfo& info){ - static oatpp::base::memory::ThreadDistributedMemoryPool pool(info.poolName, sizeof(T), info.poolChunkSize); - return pool; + static std::once_flag flag; + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; + std::call_once(flag, [&]() { + pool = new oatpp::base::memory::ThreadDistributedMemoryPool(info.poolName, sizeof(T), info.poolChunkSize); + }); + return *pool; } public: PoolSharedObjectAllocator(const AllocatorPoolInfo& info) @@ -117,10 +121,15 @@ public: static oatpp::base::memory::MemoryPool& getPool(const AllocatorPoolInfo& info){ #ifndef OATPP_COMPAT_BUILD_NO_THREAD_LOCAL static thread_local oatpp::base::memory::MemoryPool pool(info.poolName, sizeof(T), info.poolChunkSize); -#else - static oatpp::base::memory::MemoryPool pool(info.poolName, sizeof(T), info.poolChunkSize); -#endif return pool; +#else + static std::once_flag flag; + static oatpp::base::memory::MemoryPool *pool = nullptr; + std::call_once(flag, [&]() { + pool = new oatpp::base::memory::MemoryPool(info.poolName, sizeof(T), info.poolChunkSize); + }); + return *pool; +#endif } public: ThreadLocalPoolSharedObjectAllocator(const AllocatorPoolInfo& info) diff --git a/src/oatpp/core/base/memory/ObjectPool.hpp b/src/oatpp/core/base/memory/ObjectPool.hpp index 5eb69cbc..040a1718 100644 --- a/src/oatpp/core/base/memory/ObjectPool.hpp +++ b/src/oatpp/core/base/memory/ObjectPool.hpp @@ -103,8 +103,12 @@ class POOL_NAME { \ public: \ \ static oatpp::base::memory::ThreadDistributedMemoryPool& getPool(){ \ - static oatpp::base::memory::ThreadDistributedMemoryPool pool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ - return pool; \ + static std::once_flag flag; \ + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; \ + std::call_once(flag, []() { \ + pool = new oatpp::base::memory::ThreadDistributedMemoryPool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ + }); \ + return *pool; \ } \ \ }; \ @@ -189,8 +193,12 @@ static void operator delete(void* ptr, void* entry) { \ public: \ \ static oatpp::base::memory::MemoryPool& getPool(){ \ - static oatpp::base::memory::MemoryPool pool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ - return pool; \ + static std::once_flag flag; \ + static oatpp::base::memory::MemoryPool *pool = nullptr; \ + std::call_once(flag, []() { \ + pool = new oatpp::base::memory::MemoryPool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ + }); \ + return *pool; \ } \ \ }; diff --git a/src/oatpp/core/data/buffer/IOBuffer.hpp b/src/oatpp/core/data/buffer/IOBuffer.hpp index 834c8130..c15a50c9 100644 --- a/src/oatpp/core/data/buffer/IOBuffer.hpp +++ b/src/oatpp/core/data/buffer/IOBuffer.hpp @@ -45,8 +45,12 @@ public: static constexpr v_buff_size BUFFER_SIZE = 4096; private: static oatpp::base::memory::ThreadDistributedMemoryPool& getBufferPool(){ - static oatpp::base::memory::ThreadDistributedMemoryPool pool("IOBuffer_Buffer_Pool", BUFFER_SIZE, 16); - return pool; + static std::once_flag flag; + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; + std::call_once(flag, []() { + pool = new oatpp::base::memory::ThreadDistributedMemoryPool("IOBuffer_Buffer_Pool", BUFFER_SIZE, 16); + }); + return *pool; } private: void* m_entry; diff --git a/src/oatpp/core/data/stream/ChunkedBuffer.hpp b/src/oatpp/core/data/stream/ChunkedBuffer.hpp index c9995d6f..5845d86c 100644 --- a/src/oatpp/core/data/stream/ChunkedBuffer.hpp +++ b/src/oatpp/core/data/stream/ChunkedBuffer.hpp @@ -52,8 +52,12 @@ public: static const v_buff_size CHUNK_CHUNK_SIZE; static oatpp::base::memory::ThreadDistributedMemoryPool& getSegemntPool(){ - static oatpp::base::memory::ThreadDistributedMemoryPool pool(CHUNK_POOL_NAME, CHUNK_ENTRY_SIZE, CHUNK_CHUNK_SIZE); - return pool; + static std::once_flag flag; + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; + std::call_once(flag, []() { + pool = new oatpp::base::memory::ThreadDistributedMemoryPool(CHUNK_POOL_NAME, CHUNK_ENTRY_SIZE, CHUNK_CHUNK_SIZE); + }); + return *pool; } private: