initial commit for memory pool

This commit is contained in:
orignal 2017-01-10 16:14:18 -05:00
parent ca6f755634
commit feab95ce4b

41
util.h
View File

@ -27,6 +27,47 @@ namespace i2p
{
namespace util
{
template<class T>
class MemoryPool
{
public:
MemoryPool (): m_Head (nullptr) {};
~MemoryPool ()
{
while (m_Head)
{
auto tmp = m_Head;
m_Head = static_cast<T*>(*(void * *)m_Head); // next
delete tmp;
}
}
template<typename... TArgs>
T * Acquire (TArgs... args)
{
if (!m_Head) return new T(args...);
else
{
auto tmp = m_Head;
m_Head = static_cast<T*>(*(void * *)m_Head); // next
return new (tmp)T(args...);
}
}
void Release (T * t)
{
t->~T ();
*(void * *)t = m_Head;
m_Head = t;
}
private:
T * m_Head;
};
namespace net
{
int GetMTU (const boost::asio::ip::address& localAddress);