From 8c8127dda6881fac62f7e167a5d6f68c68359392 Mon Sep 17 00:00:00 2001 From: brain5lug Date: Fri, 29 Sep 2017 00:03:07 +0300 Subject: [PATCH] fixed perfect forwarding for the memory pool --- libi2pd/Streaming.h | 4 ++-- libi2pd/util.h | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/libi2pd/Streaming.h b/libi2pd/Streaming.h index c598245a..eae959ed 100644 --- a/libi2pd/Streaming.h +++ b/libi2pd/Streaming.h @@ -276,8 +276,8 @@ namespace stream /** set max connections per minute per destination */ void SetMaxConnsPerMinute(const uint32_t conns); - Packet * NewPacket () { return m_PacketsPool.Acquire (); }; - void DeletePacket (Packet * p) { if (p) m_PacketsPool.Release (p); }; + Packet * NewPacket () { return m_PacketsPool.Acquire (); } + void DeletePacket (Packet * p) { m_PacketsPool.Release (p); } private: diff --git a/libi2pd/util.h b/libi2pd/util.h index 38ca6b28..1a9b9a73 100644 --- a/libi2pd/util.h +++ b/libi2pd/util.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #ifdef ANDROID @@ -14,7 +15,7 @@ namespace std template std::string to_string(T value) { - return boost::lexical_cast(value); + return boost::lexical_cast(value); } inline int stoi(const std::string& str) @@ -29,12 +30,12 @@ namespace i2p namespace util { - template + template class MemoryPool { public: - MemoryPool (): m_Head (nullptr) {}; + MemoryPool (): m_Head (nullptr) {} ~MemoryPool () { while (m_Head) @@ -48,12 +49,12 @@ namespace util template T * Acquire (TArgs&&... args) { - if (!m_Head) return new T(args...); + if (!m_Head) return new T(std::forward(args)...); else { auto tmp = m_Head; m_Head = static_cast(*(void * *)m_Head); // next - return new (tmp)T(args...); + return new (tmp)T(std::forward(args)...); } } @@ -68,14 +69,15 @@ namespace util template std::unique_ptr > AcquireUnique (TArgs&&... args) { - return std::unique_ptr >(Acquire (args...), + return std::unique_ptr >(Acquire (std::forward(args)...), std::bind (&MemoryPool::Release, this, std::placeholders::_1)); } template std::shared_ptr AcquireShared (TArgs&&... args) { - return std::shared_ptr(Acquire (args...), std::bind (&MemoryPool::Release, this, std::placeholders::_1)); + return std::shared_ptr(Acquire (std::forward(args)...), + std::bind (&MemoryPool::Release, this, std::placeholders::_1)); } protected: @@ -88,13 +90,13 @@ namespace util { public: - MemoryPoolMt () {}; + MemoryPoolMt () {} template T * AcquireMt (TArgs&&... args) { - if (!this->m_Head) return new T(args...); + if (!this->m_Head) return new T(std::forward(args)...); std::lock_guard l(m_Mutex); - return this->Acquire (args...); + return this->Acquire (std::forward(args)...); } void ReleaseMt (T * t)