From 6974e708bf4641e667aa187d64a91804e595efad Mon Sep 17 00:00:00 2001 From: Andrii Arsirii Date: Thu, 1 Oct 2020 10:50:59 +0300 Subject: [PATCH 1/3] oatpp::base::memory::MemoryPool::freeByEntryHeader()]: Invalid EntryHeader --- src/oatpp/core/base/StrBuffer.hpp | 4 ++-- src/oatpp/core/base/memory/Allocator.hpp | 11 ++++++----- src/oatpp/core/base/memory/ObjectPool.hpp | 8 ++++---- src/oatpp/core/data/buffer/IOBuffer.hpp | 4 ++-- src/oatpp/core/data/stream/ChunkedBuffer.hpp | 4 ++-- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/oatpp/core/base/StrBuffer.hpp b/src/oatpp/core/base/StrBuffer.hpp index b00513a3..465a2bf5 100644 --- a/src/oatpp/core/base/StrBuffer.hpp +++ b/src/oatpp/core/base/StrBuffer.hpp @@ -41,8 +41,8 @@ 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 auto 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..8c80d350 100644 --- a/src/oatpp/core/base/memory/Allocator.hpp +++ b/src/oatpp/core/base/memory/Allocator.hpp @@ -67,8 +67,8 @@ 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 auto pool = new oatpp::base::memory::ThreadDistributedMemoryPool(info.poolName, sizeof(T), info.poolChunkSize); + return *pool; } public: PoolSharedObjectAllocator(const AllocatorPoolInfo& info) @@ -117,10 +117,11 @@ 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 auto 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..41851a2c 100644 --- a/src/oatpp/core/base/memory/ObjectPool.hpp +++ b/src/oatpp/core/base/memory/ObjectPool.hpp @@ -103,8 +103,8 @@ 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 auto pool = new oatpp::base::memory::ThreadDistributedMemoryPool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ + return *pool; \ } \ \ }; \ @@ -189,8 +189,8 @@ 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 auto 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..0bc822ab 100644 --- a/src/oatpp/core/data/buffer/IOBuffer.hpp +++ b/src/oatpp/core/data/buffer/IOBuffer.hpp @@ -45,8 +45,8 @@ 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 auto 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..751136e7 100644 --- a/src/oatpp/core/data/stream/ChunkedBuffer.hpp +++ b/src/oatpp/core/data/stream/ChunkedBuffer.hpp @@ -52,8 +52,8 @@ 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; + auto pool = new oatpp::base::memory::ThreadDistributedMemoryPool(CHUNK_POOL_NAME, CHUNK_ENTRY_SIZE, CHUNK_CHUNK_SIZE); + return *pool; } private: From f814d51d81f4d3565413cd0d02980190f1eb8ddc Mon Sep 17 00:00:00 2001 From: Andrii Arsirii Date: Thu, 1 Oct 2020 11:49:37 +0300 Subject: [PATCH 2/3] use std::call_once --- src/oatpp/core/base/StrBuffer.hpp | 8 +++++++- src/oatpp/core/base/memory/Allocator.hpp | 16 ++++++++++++++-- src/oatpp/core/base/memory/ObjectPool.hpp | 16 ++++++++++++++-- src/oatpp/core/data/buffer/IOBuffer.hpp | 8 +++++++- src/oatpp/core/data/stream/ChunkedBuffer.hpp | 8 +++++++- 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/oatpp/core/base/StrBuffer.hpp b/src/oatpp/core/base/StrBuffer.hpp index 465a2bf5..7aed5cd0 100644 --- a/src/oatpp/core/base/StrBuffer.hpp +++ b/src/oatpp/core/base/StrBuffer.hpp @@ -41,7 +41,13 @@ private: static constexpr v_buff_size SM_STRING_POOL_ENTRY_SIZE = 256; static oatpp::base::memory::ThreadDistributedMemoryPool& getSmallStringPool() { - static auto pool = new oatpp::base::memory::ThreadDistributedMemoryPool("Small_String_Pool", SM_STRING_POOL_ENTRY_SIZE, 16); + static std::once_flag flag; + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; + std::call_once(flag, []() { + if (pool == nullptr) { + pool = new oatpp::base::memory::ThreadDistributedMemoryPool("Small_String_Pool", SM_STRING_POOL_ENTRY_SIZE, 16); + } + }); return *pool; } diff --git a/src/oatpp/core/base/memory/Allocator.hpp b/src/oatpp/core/base/memory/Allocator.hpp index 8c80d350..051d2a89 100644 --- a/src/oatpp/core/base/memory/Allocator.hpp +++ b/src/oatpp/core/base/memory/Allocator.hpp @@ -67,7 +67,13 @@ public: const AllocatorPoolInfo& m_poolInfo; public: static oatpp::base::memory::ThreadDistributedMemoryPool& getPool(const AllocatorPoolInfo& info){ - static auto pool = new oatpp::base::memory::ThreadDistributedMemoryPool(info.poolName, sizeof(T), info.poolChunkSize); + static std::once_flag flag; + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; + std::call_once(flag, [&]() { + if (pool == nullptr) { + pool = new oatpp::base::memory::ThreadDistributedMemoryPool(info.poolName, sizeof(T), info.poolChunkSize); + } + }); return *pool; } public: @@ -119,7 +125,13 @@ public: static thread_local oatpp::base::memory::MemoryPool pool(info.poolName, sizeof(T), info.poolChunkSize); return pool; #else - static auto pool = new oatpp::base::memory::MemoryPool(info.poolName, sizeof(T), info.poolChunkSize); + static std::once_flag flag; + static oatpp::base::memory::MemoryPool *pool = nullptr; + std::call_once(flag, [&]() { + if (pool == nullptr) { + pool = new oatpp::base::memory::MemoryPool(info.poolName, sizeof(T), info.poolChunkSize); + } + }); return *pool; #endif } diff --git a/src/oatpp/core/base/memory/ObjectPool.hpp b/src/oatpp/core/base/memory/ObjectPool.hpp index 41851a2c..dc1af05c 100644 --- a/src/oatpp/core/base/memory/ObjectPool.hpp +++ b/src/oatpp/core/base/memory/ObjectPool.hpp @@ -103,7 +103,13 @@ class POOL_NAME { \ public: \ \ static oatpp::base::memory::ThreadDistributedMemoryPool& getPool(){ \ - static auto pool = new oatpp::base::memory::ThreadDistributedMemoryPool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ + static std::once_flag flag; \ + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; \ + std::call_once(flag, []() { \ + if (pool == nullptr) { \ + pool = new oatpp::base::memory::ThreadDistributedMemoryPool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ + } \ + }); \ return *pool; \ } \ \ @@ -189,7 +195,13 @@ static void operator delete(void* ptr, void* entry) { \ public: \ \ static oatpp::base::memory::MemoryPool& getPool(){ \ - static auto pool = new oatpp::base::memory::MemoryPool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ + static std::once_flag flag; \ + static oatpp::base::memory::MemoryPool *pool = nullptr; \ + std::call_once(flag, []() { \ + if (pool == nullptr) { \ + 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 0bc822ab..15877a74 100644 --- a/src/oatpp/core/data/buffer/IOBuffer.hpp +++ b/src/oatpp/core/data/buffer/IOBuffer.hpp @@ -45,7 +45,13 @@ public: static constexpr v_buff_size BUFFER_SIZE = 4096; private: static oatpp::base::memory::ThreadDistributedMemoryPool& getBufferPool(){ - static auto pool = new oatpp::base::memory::ThreadDistributedMemoryPool("IOBuffer_Buffer_Pool", BUFFER_SIZE, 16); + static std::once_flag flag; + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; + std::call_once(flag, []() { + if (pool == nullptr) { + pool = new oatpp::base::memory::ThreadDistributedMemoryPool("IOBuffer_Buffer_Pool", BUFFER_SIZE, 16); + } + }); return *pool; } private: diff --git a/src/oatpp/core/data/stream/ChunkedBuffer.hpp b/src/oatpp/core/data/stream/ChunkedBuffer.hpp index 751136e7..2f9b6bf6 100644 --- a/src/oatpp/core/data/stream/ChunkedBuffer.hpp +++ b/src/oatpp/core/data/stream/ChunkedBuffer.hpp @@ -52,7 +52,13 @@ public: static const v_buff_size CHUNK_CHUNK_SIZE; static oatpp::base::memory::ThreadDistributedMemoryPool& getSegemntPool(){ - auto pool = new oatpp::base::memory::ThreadDistributedMemoryPool(CHUNK_POOL_NAME, CHUNK_ENTRY_SIZE, CHUNK_CHUNK_SIZE); + static std::once_flag flag; + static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; + std::call_once(flag, []() { + if (pool == nullptr) { + pool = new oatpp::base::memory::ThreadDistributedMemoryPool(CHUNK_POOL_NAME, CHUNK_ENTRY_SIZE, CHUNK_CHUNK_SIZE); + } + }); return *pool; } From 000b5cbfe81f03e2326bb9f48d6492a0856fe70e Mon Sep 17 00:00:00 2001 From: Andrii Arsirii Date: Thu, 1 Oct 2020 12:36:13 +0300 Subject: [PATCH 3/3] simplify --- src/oatpp/core/base/StrBuffer.hpp | 4 +--- src/oatpp/core/base/memory/Allocator.hpp | 8 ++------ src/oatpp/core/base/memory/ObjectPool.hpp | 8 ++------ src/oatpp/core/data/buffer/IOBuffer.hpp | 4 +--- src/oatpp/core/data/stream/ChunkedBuffer.hpp | 4 +--- 5 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/oatpp/core/base/StrBuffer.hpp b/src/oatpp/core/base/StrBuffer.hpp index 7aed5cd0..cb3d38c8 100644 --- a/src/oatpp/core/base/StrBuffer.hpp +++ b/src/oatpp/core/base/StrBuffer.hpp @@ -44,9 +44,7 @@ private: static std::once_flag flag; static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; std::call_once(flag, []() { - if (pool == nullptr) { - pool = new oatpp::base::memory::ThreadDistributedMemoryPool("Small_String_Pool", SM_STRING_POOL_ENTRY_SIZE, 16); - } + pool = new oatpp::base::memory::ThreadDistributedMemoryPool("Small_String_Pool", SM_STRING_POOL_ENTRY_SIZE, 16); }); return *pool; } diff --git a/src/oatpp/core/base/memory/Allocator.hpp b/src/oatpp/core/base/memory/Allocator.hpp index 051d2a89..b8a07718 100644 --- a/src/oatpp/core/base/memory/Allocator.hpp +++ b/src/oatpp/core/base/memory/Allocator.hpp @@ -70,9 +70,7 @@ public: static std::once_flag flag; static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; std::call_once(flag, [&]() { - if (pool == nullptr) { - pool = new oatpp::base::memory::ThreadDistributedMemoryPool(info.poolName, sizeof(T), info.poolChunkSize); - } + pool = new oatpp::base::memory::ThreadDistributedMemoryPool(info.poolName, sizeof(T), info.poolChunkSize); }); return *pool; } @@ -128,9 +126,7 @@ public: static std::once_flag flag; static oatpp::base::memory::MemoryPool *pool = nullptr; std::call_once(flag, [&]() { - if (pool == nullptr) { - pool = new oatpp::base::memory::MemoryPool(info.poolName, sizeof(T), info.poolChunkSize); - } + pool = new oatpp::base::memory::MemoryPool(info.poolName, sizeof(T), info.poolChunkSize); }); return *pool; #endif diff --git a/src/oatpp/core/base/memory/ObjectPool.hpp b/src/oatpp/core/base/memory/ObjectPool.hpp index dc1af05c..040a1718 100644 --- a/src/oatpp/core/base/memory/ObjectPool.hpp +++ b/src/oatpp/core/base/memory/ObjectPool.hpp @@ -106,9 +106,7 @@ public: \ static std::once_flag flag; \ static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; \ std::call_once(flag, []() { \ - if (pool == nullptr) { \ - pool = new oatpp::base::memory::ThreadDistributedMemoryPool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ - } \ + pool = new oatpp::base::memory::ThreadDistributedMemoryPool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ }); \ return *pool; \ } \ @@ -198,9 +196,7 @@ static void operator delete(void* ptr, void* entry) { \ static std::once_flag flag; \ static oatpp::base::memory::MemoryPool *pool = nullptr; \ std::call_once(flag, []() { \ - if (pool == nullptr) { \ - pool = new oatpp::base::memory::MemoryPool(#POOL_NAME"<"#TYPE">", sizeof(TYPE), CHUNK_SIZE); \ - } \ + 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 15877a74..c15a50c9 100644 --- a/src/oatpp/core/data/buffer/IOBuffer.hpp +++ b/src/oatpp/core/data/buffer/IOBuffer.hpp @@ -48,9 +48,7 @@ private: static std::once_flag flag; static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; std::call_once(flag, []() { - if (pool == nullptr) { - pool = new oatpp::base::memory::ThreadDistributedMemoryPool("IOBuffer_Buffer_Pool", BUFFER_SIZE, 16); - } + pool = new oatpp::base::memory::ThreadDistributedMemoryPool("IOBuffer_Buffer_Pool", BUFFER_SIZE, 16); }); return *pool; } diff --git a/src/oatpp/core/data/stream/ChunkedBuffer.hpp b/src/oatpp/core/data/stream/ChunkedBuffer.hpp index 2f9b6bf6..5845d86c 100644 --- a/src/oatpp/core/data/stream/ChunkedBuffer.hpp +++ b/src/oatpp/core/data/stream/ChunkedBuffer.hpp @@ -55,9 +55,7 @@ public: static std::once_flag flag; static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr; std::call_once(flag, []() { - if (pool == nullptr) { - pool = new oatpp::base::memory::ThreadDistributedMemoryPool(CHUNK_POOL_NAME, CHUNK_ENTRY_SIZE, CHUNK_CHUNK_SIZE); - } + pool = new oatpp::base::memory::ThreadDistributedMemoryPool(CHUNK_POOL_NAME, CHUNK_ENTRY_SIZE, CHUNK_CHUNK_SIZE); }); return *pool; }