Explorar el Código

Merge pull request #314 from pcapdump/issue_308

Issue #308: oatpp::base::memory::MemoryPool::freeByEntryHeader()]: Invalid EntryHeader
Leonid Stryzhevskyi hace 3 años
padre
commit
8a15cb43b1

+ 6 - 2
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() {

+ 13 - 4
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);
+    return pool;
 #else
-    static oatpp::base::memory::MemoryPool pool(info.poolName, sizeof(T), info.poolChunkSize);
+    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
-    return pool;
   }
 public:
   ThreadLocalPoolSharedObjectAllocator(const AllocatorPoolInfo& info)

+ 12 - 4
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; \
     } \
   \
   };

+ 6 - 2
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;

+ 6 - 2
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: