mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
2.4 KiB
128 lines
2.4 KiB
11 years ago
|
#ifndef UTIL_H
|
||
|
#define UTIL_H
|
||
|
|
||
|
#include <string>
|
||
8 years ago
|
#include <functional>
|
||
|
#include <memory>
|
||
8 years ago
|
#include <mutex>
|
||
10 years ago
|
#include <boost/asio.hpp>
|
||
11 years ago
|
|
||
9 years ago
|
#ifdef ANDROID
|
||
8 years ago
|
#include <boost/lexical_cast.hpp>
|
||
9 years ago
|
namespace std
|
||
|
{
|
||
|
template <typename T>
|
||
|
std::string to_string(T value)
|
||
|
{
|
||
|
return boost::lexical_cast<std::string>(value);
|
||
|
}
|
||
8 years ago
|
|
||
|
inline int stoi(const std::string& str)
|
||
|
{
|
||
|
return boost::lexical_cast<int>(str);
|
||
|
}
|
||
9 years ago
|
}
|
||
|
#endif
|
||
|
|
||
11 years ago
|
namespace i2p
|
||
|
{
|
||
|
namespace util
|
||
|
{
|
||
8 years ago
|
|
||
|
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>
|
||
8 years ago
|
T * Acquire (TArgs&&... args)
|
||
8 years ago
|
{
|
||
|
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)
|
||
|
{
|
||
8 years ago
|
if (!t) return;
|
||
8 years ago
|
t->~T ();
|
||
8 years ago
|
*(void * *)t = m_Head; // next
|
||
8 years ago
|
m_Head = t;
|
||
|
}
|
||
|
|
||
8 years ago
|
template<typename... TArgs>
|
||
|
std::unique_ptr<T, std::function<void(T*)> > AcquireUnique (TArgs&&... args)
|
||
|
{
|
||
|
return std::unique_ptr<T, std::function<void(T*)> >(Acquire (args...),
|
||
|
std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
|
||
|
}
|
||
|
|
||
8 years ago
|
template<typename... TArgs>
|
||
|
std::shared_ptr<T> AcquireShared (TArgs&&... args)
|
||
|
{
|
||
|
return std::shared_ptr<T>(Acquire (args...), std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
|
||
|
}
|
||
|
|
||
8 years ago
|
protected:
|
||
8 years ago
|
|
||
|
T * m_Head;
|
||
|
};
|
||
|
|
||
8 years ago
|
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;
|
||
|
};
|
||
|
|
||
10 years ago
|
namespace net
|
||
|
{
|
||
|
int GetMTU (const boost::asio::ip::address& localAddress);
|
||
9 years ago
|
const boost::asio::ip::address GetInterfaceAddress(const std::string & ifname, bool ipv6=false);
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|