mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-03-10 11:28:27 +01:00
initial commit for memory pool
This commit is contained in:
parent
ca6f755634
commit
feab95ce4b
1 changed files with 41 additions and 0 deletions
41
util.h
41
util.h
|
@ -27,6 +27,47 @@ namespace i2p
|
||||||
{
|
{
|
||||||
namespace util
|
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
|
namespace net
|
||||||
{
|
{
|
||||||
int GetMTU (const boost::asio::ip::address& localAddress);
|
int GetMTU (const boost::asio::ip::address& localAddress);
|
||||||
|
|
Loading…
Add table
Reference in a new issue