mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-30 20:52:30 +02:00
Reformat code
This commit is contained in:
parent
3ddb370718
commit
55534ea002
140 changed files with 46068 additions and 48277 deletions
312
libi2pd/util.h
312
libi2pd/util.h
|
@ -22,210 +22,200 @@
|
|||
#include <boost/lexical_cast.hpp>
|
||||
namespace std
|
||||
{
|
||||
template <typename T>
|
||||
std::string to_string(T value)
|
||||
{
|
||||
return boost::lexical_cast<std::string>(value);
|
||||
}
|
||||
template <typename T>
|
||||
std::string to_string(T value)
|
||||
{
|
||||
return boost::lexical_cast<std::string>(value);
|
||||
}
|
||||
|
||||
inline int stoi(const std::string& str)
|
||||
{
|
||||
return boost::lexical_cast<int>(str);
|
||||
}
|
||||
inline int stoi(const std::string& str)
|
||||
{
|
||||
return boost::lexical_cast<int>(str);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace i2p
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
namespace i2p {
|
||||
namespace util {
|
||||
|
||||
template<class T>
|
||||
class MemoryPool
|
||||
{
|
||||
//BOOST_STATIC_ASSERT_MSG(sizeof(T) >= sizeof(void*), "size cannot be less that general pointer size");
|
||||
template<class T>
|
||||
class MemoryPool {
|
||||
//BOOST_STATIC_ASSERT_MSG(sizeof(T) >= sizeof(void*), "size cannot be less that general pointer size");
|
||||
|
||||
public:
|
||||
public:
|
||||
|
||||
MemoryPool (): m_Head (nullptr) {}
|
||||
~MemoryPool ()
|
||||
{
|
||||
CleanUp ();
|
||||
}
|
||||
MemoryPool() : m_Head(nullptr) {}
|
||||
|
||||
void CleanUp ()
|
||||
{
|
||||
CleanUp (m_Head);
|
||||
m_Head = nullptr;
|
||||
}
|
||||
~MemoryPool() {
|
||||
CleanUp();
|
||||
}
|
||||
|
||||
template<typename... TArgs>
|
||||
T * Acquire (TArgs&&... args)
|
||||
{
|
||||
if (!m_Head) return new T(std::forward<TArgs>(args)...);
|
||||
else
|
||||
{
|
||||
auto tmp = m_Head;
|
||||
m_Head = static_cast<T*>(*(void * *)m_Head); // next
|
||||
return new (tmp)T(std::forward<TArgs>(args)...);
|
||||
}
|
||||
}
|
||||
void CleanUp() {
|
||||
CleanUp(m_Head);
|
||||
m_Head = nullptr;
|
||||
}
|
||||
|
||||
void Release (T * t)
|
||||
{
|
||||
if (!t) return;
|
||||
t->~T ();
|
||||
*(void * *)t = m_Head; // next
|
||||
m_Head = t;
|
||||
}
|
||||
template<typename... TArgs>
|
||||
T *Acquire(TArgs &&... args) {
|
||||
if (!m_Head) return new T(std::forward<TArgs>(args)...);
|
||||
else {
|
||||
auto tmp = m_Head;
|
||||
m_Head = static_cast<T *>(*(void **) m_Head); // next
|
||||
return new(tmp)T(std::forward<TArgs>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... TArgs>
|
||||
std::unique_ptr<T, std::function<void(T*)> > AcquireUnique (TArgs&&... args)
|
||||
{
|
||||
return std::unique_ptr<T, std::function<void(T*)> >(Acquire (std::forward<TArgs>(args)...),
|
||||
std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
|
||||
}
|
||||
void Release(T *t) {
|
||||
if (!t) return;
|
||||
t->~T();
|
||||
*(void **) t = m_Head; // next
|
||||
m_Head = t;
|
||||
}
|
||||
|
||||
template<typename... TArgs>
|
||||
std::shared_ptr<T> AcquireShared (TArgs&&... args)
|
||||
{
|
||||
return std::shared_ptr<T>(Acquire (std::forward<TArgs>(args)...),
|
||||
std::bind (&MemoryPool<T>::Release, this, std::placeholders::_1));
|
||||
}
|
||||
template<typename... TArgs>
|
||||
std::unique_ptr <T, std::function<void(T *)>> AcquireUnique(TArgs &&... args) {
|
||||
return std::unique_ptr < T, std::function < void(T * ) > > (Acquire(std::forward<TArgs>(args)...),
|
||||
std::bind(&MemoryPool<T>::Release, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
protected:
|
||||
template<typename... TArgs>
|
||||
std::shared_ptr <T> AcquireShared(TArgs &&... args) {
|
||||
return std::shared_ptr<T>(Acquire(std::forward<TArgs>(args)...),
|
||||
std::bind(&MemoryPool<T>::Release, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void CleanUp (T * head)
|
||||
{
|
||||
while (head)
|
||||
{
|
||||
auto tmp = head;
|
||||
head = static_cast<T*>(*(void * *)head); // next
|
||||
::operator delete ((void *)tmp);
|
||||
}
|
||||
}
|
||||
protected:
|
||||
|
||||
protected:
|
||||
void CleanUp(T *head) {
|
||||
while (head) {
|
||||
auto tmp = head;
|
||||
head = static_cast<T *>(*(void **) head); // next
|
||||
::operator delete((void *) tmp);
|
||||
}
|
||||
}
|
||||
|
||||
T * m_Head;
|
||||
};
|
||||
protected:
|
||||
|
||||
template<class T>
|
||||
class MemoryPoolMt: public MemoryPool<T>
|
||||
{
|
||||
public:
|
||||
T *m_Head;
|
||||
};
|
||||
|
||||
MemoryPoolMt () {}
|
||||
template<typename... TArgs>
|
||||
T * AcquireMt (TArgs&&... args)
|
||||
{
|
||||
if (!this->m_Head) return new T(std::forward<TArgs>(args)...);
|
||||
std::lock_guard<std::mutex> l(m_Mutex);
|
||||
return this->Acquire (std::forward<TArgs>(args)...);
|
||||
}
|
||||
template<class T>
|
||||
class MemoryPoolMt : public MemoryPool<T> {
|
||||
public:
|
||||
|
||||
void ReleaseMt (T * t)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(m_Mutex);
|
||||
this->Release (t);
|
||||
}
|
||||
MemoryPoolMt() {}
|
||||
|
||||
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);
|
||||
}
|
||||
template<typename... TArgs>
|
||||
T *AcquireMt(TArgs &&... args) {
|
||||
if (!this->m_Head) return new T(std::forward<TArgs>(args)...);
|
||||
std::lock_guard <std::mutex> l(m_Mutex);
|
||||
return this->Acquire(std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
template<typename... TArgs>
|
||||
std::shared_ptr<T> AcquireSharedMt (TArgs&&... args)
|
||||
{
|
||||
return std::shared_ptr<T>(AcquireMt (std::forward<TArgs>(args)...),
|
||||
std::bind<void (MemoryPoolMt<T>::*)(T *)> (&MemoryPoolMt<T>::ReleaseMt, this, std::placeholders::_1));
|
||||
}
|
||||
void ReleaseMt(T *t) {
|
||||
std::lock_guard <std::mutex> l(m_Mutex);
|
||||
this->Release(t);
|
||||
}
|
||||
|
||||
void CleanUpMt ()
|
||||
{
|
||||
T * head;
|
||||
{
|
||||
std::lock_guard<std::mutex> l(m_Mutex);
|
||||
head = this->m_Head;
|
||||
this->m_Head = nullptr;
|
||||
}
|
||||
if (head) this->CleanUp (head);
|
||||
}
|
||||
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:
|
||||
template<typename... TArgs>
|
||||
std::shared_ptr <T> AcquireSharedMt(TArgs &&... args) {
|
||||
return std::shared_ptr<T>(AcquireMt(std::forward<TArgs>(args)...),
|
||||
std::bind < void(MemoryPoolMt<T>::*)(T * ) >
|
||||
(&MemoryPoolMt<T>::ReleaseMt, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
std::mutex m_Mutex;
|
||||
};
|
||||
void CleanUpMt() {
|
||||
T *head;
|
||||
{
|
||||
std::lock_guard <std::mutex> l(m_Mutex);
|
||||
head = this->m_Head;
|
||||
this->m_Head = nullptr;
|
||||
}
|
||||
if (head) this->CleanUp(head);
|
||||
}
|
||||
|
||||
class RunnableService
|
||||
{
|
||||
protected:
|
||||
private:
|
||||
|
||||
RunnableService (const std::string& name): m_Name (name), m_IsRunning (false) {}
|
||||
virtual ~RunnableService () {}
|
||||
std::mutex m_Mutex;
|
||||
};
|
||||
|
||||
boost::asio::io_service& GetIOService () { return m_Service; }
|
||||
bool IsRunning () const { return m_IsRunning; };
|
||||
class RunnableService {
|
||||
protected:
|
||||
|
||||
void StartIOService ();
|
||||
void StopIOService ();
|
||||
RunnableService(const std::string &name) : m_Name(name), m_IsRunning(false) {}
|
||||
|
||||
private:
|
||||
virtual ~RunnableService() {}
|
||||
|
||||
void Run ();
|
||||
boost::asio::io_service &GetIOService() { return m_Service; }
|
||||
|
||||
private:
|
||||
bool IsRunning() const { return m_IsRunning; };
|
||||
|
||||
std::string m_Name;
|
||||
volatile bool m_IsRunning;
|
||||
std::unique_ptr<std::thread> m_Thread;
|
||||
boost::asio::io_service m_Service;
|
||||
};
|
||||
void StartIOService();
|
||||
|
||||
class RunnableServiceWithWork: public RunnableService
|
||||
{
|
||||
protected:
|
||||
void StopIOService();
|
||||
|
||||
RunnableServiceWithWork (const std::string& name):
|
||||
RunnableService (name), m_Work (GetIOService ()) {}
|
||||
private:
|
||||
|
||||
private:
|
||||
void Run();
|
||||
|
||||
boost::asio::io_service::work m_Work;
|
||||
};
|
||||
private:
|
||||
|
||||
void SetThreadName (const char *name);
|
||||
std::string m_Name;
|
||||
volatile bool m_IsRunning;
|
||||
std::unique_ptr <std::thread> m_Thread;
|
||||
boost::asio::io_service m_Service;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class SaveStateHelper
|
||||
{
|
||||
public:
|
||||
class RunnableServiceWithWork : public RunnableService {
|
||||
protected:
|
||||
|
||||
SaveStateHelper (T& orig): m_Original (orig), m_Copy (orig) {};
|
||||
~SaveStateHelper () { m_Original = m_Copy; };
|
||||
RunnableServiceWithWork(const std::string &name) :
|
||||
RunnableService(name), m_Work(GetIOService()) {}
|
||||
|
||||
private:
|
||||
private:
|
||||
|
||||
T& m_Original;
|
||||
T m_Copy;
|
||||
};
|
||||
boost::asio::io_service::work m_Work;
|
||||
};
|
||||
|
||||
namespace net
|
||||
{
|
||||
int GetMTU (const boost::asio::ip::address& localAddress);
|
||||
int GetMaxMTU (const boost::asio::ip::address_v6& localAddress); // check tunnel broker for ipv6 address
|
||||
const boost::asio::ip::address GetInterfaceAddress (const std::string & ifname, bool ipv6=false);
|
||||
boost::asio::ip::address_v6 GetYggdrasilAddress ();
|
||||
bool IsLocalAddress (const boost::asio::ip::address& addr);
|
||||
bool IsInReservedRange (const boost::asio::ip::address& host);
|
||||
bool IsYggdrasilAddress (const boost::asio::ip::address& addr);
|
||||
}
|
||||
}
|
||||
void SetThreadName(const char *name);
|
||||
|
||||
template<typename T>
|
||||
class SaveStateHelper {
|
||||
public:
|
||||
|
||||
SaveStateHelper(T &orig) : m_Original(orig), m_Copy(orig) {};
|
||||
|
||||
~SaveStateHelper() { m_Original = m_Copy; };
|
||||
|
||||
private:
|
||||
|
||||
T &m_Original;
|
||||
T m_Copy;
|
||||
};
|
||||
|
||||
namespace net {
|
||||
int GetMTU(const boost::asio::ip::address &localAddress);
|
||||
|
||||
int GetMaxMTU(const boost::asio::ip::address_v6 &localAddress); // check tunnel broker for ipv6 address
|
||||
const boost::asio::ip::address GetInterfaceAddress(const std::string &ifname, bool ipv6 = false);
|
||||
|
||||
boost::asio::ip::address_v6 GetYggdrasilAddress();
|
||||
|
||||
bool IsLocalAddress(const boost::asio::ip::address &addr);
|
||||
|
||||
bool IsInReservedRange(const boost::asio::ip::address &host);
|
||||
|
||||
bool IsYggdrasilAddress(const boost::asio::ip::address &addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue