mirror of
https://github.com/PurpleI2P/i2pd
synced 2024-11-10 00:00:29 +03:00
multithreaded memory pool
This commit is contained in:
parent
c70817b21a
commit
dc914b1806
36
util.h
36
util.h
@ -4,6 +4,7 @@
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
#ifdef ANDROID
|
||||
@ -71,11 +72,44 @@ namespace util
|
||||
std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
|
||||
T * m_Head;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class MemoryPoolMt: public MemoryPool<T>
|
||||
{
|
||||
public:
|
||||
|
||||
MemoryPoolMt () {};
|
||||
template<typename... TArgs>
|
||||
T * AcquireMt (TArgs&&... args)
|
||||
{
|
||||
if (!this->m_Head) return new T(args...);
|
||||
std::lock_guard<std::mutex> l(m_Mutex);
|
||||
return this->Acquire (args...);
|
||||
}
|
||||
|
||||
void ReleaseMt (T * t)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(m_Mutex);
|
||||
this->Release (t);
|
||||
}
|
||||
|
||||
template<template<typename, typename...>class C, typename... R>
|
||||
void ReleaseMt(const C<T *, R...>& c)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(m_Mutex);
|
||||
for (auto& it: c)
|
||||
this->Release (it);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::mutex m_Mutex;
|
||||
};
|
||||
|
||||
namespace net
|
||||
{
|
||||
int GetMTU (const boost::asio::ip::address& localAddress);
|
||||
|
Loading…
Reference in New Issue
Block a user