Merge pull request #966 from brain5lug/openssl

fixed perfect forwarding for the memory pool
This commit is contained in:
orignal 2017-09-28 17:38:58 -04:00 committed by GitHub
commit cb0552e20d
2 changed files with 14 additions and 12 deletions

View File

@ -276,8 +276,8 @@ namespace stream
/** set max connections per minute per destination */ /** set max connections per minute per destination */
void SetMaxConnsPerMinute(const uint32_t conns); void SetMaxConnsPerMinute(const uint32_t conns);
Packet * NewPacket () { return m_PacketsPool.Acquire (); }; Packet * NewPacket () { return m_PacketsPool.Acquire (); }
void DeletePacket (Packet * p) { if (p) m_PacketsPool.Release (p); }; void DeletePacket (Packet * p) { m_PacketsPool.Release (p); }
private: private:

View File

@ -5,6 +5,7 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <utility>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#ifdef ANDROID #ifdef ANDROID
@ -34,7 +35,7 @@ namespace util
{ {
public: public:
MemoryPool (): m_Head (nullptr) {}; MemoryPool (): m_Head (nullptr) {}
~MemoryPool () ~MemoryPool ()
{ {
while (m_Head) while (m_Head)
@ -48,12 +49,12 @@ namespace util
template<typename... TArgs> template<typename... TArgs>
T * Acquire (TArgs&&... args) T * Acquire (TArgs&&... args)
{ {
if (!m_Head) return new T(args...); if (!m_Head) return new T(std::forward<TArgs>(args)...);
else else
{ {
auto tmp = m_Head; auto tmp = m_Head;
m_Head = static_cast<T*>(*(void * *)m_Head); // next m_Head = static_cast<T*>(*(void * *)m_Head); // next
return new (tmp)T(args...); return new (tmp)T(std::forward<TArgs>(args)...);
} }
} }
@ -68,14 +69,15 @@ namespace util
template<typename... TArgs> template<typename... TArgs>
std::unique_ptr<T, std::function<void(T*)> > AcquireUnique (TArgs&&... args) std::unique_ptr<T, std::function<void(T*)> > AcquireUnique (TArgs&&... args)
{ {
return std::unique_ptr<T, std::function<void(T*)> >(Acquire (args...), return std::unique_ptr<T, std::function<void(T*)> >(Acquire (std::forward<TArgs>(args)...),
std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1)); std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
} }
template<typename... TArgs> template<typename... TArgs>
std::shared_ptr<T> AcquireShared (TArgs&&... args) std::shared_ptr<T> AcquireShared (TArgs&&... args)
{ {
return std::shared_ptr<T>(Acquire (args...), std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1)); return std::shared_ptr<T>(Acquire (std::forward<TArgs>(args)...),
std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
} }
protected: protected:
@ -88,13 +90,13 @@ namespace util
{ {
public: public:
MemoryPoolMt () {}; MemoryPoolMt () {}
template<typename... TArgs> template<typename... TArgs>
T * AcquireMt (TArgs&&... args) T * AcquireMt (TArgs&&... args)
{ {
if (!this->m_Head) return new T(args...); if (!this->m_Head) return new T(std::forward<TArgs>(args)...);
std::lock_guard<std::mutex> l(m_Mutex); std::lock_guard<std::mutex> l(m_Mutex);
return this->Acquire (args...); return this->Acquire (std::forward<TArgs>(args)...);
} }
void ReleaseMt (T * t) void ReleaseMt (T * t)