Browse Source

memory pool for RouterInfo buffer

pull/1717/head
orignal 3 years ago
parent
commit
f567417bb3
  1. 2
      libi2pd/NetDb.cpp
  2. 4
      libi2pd/NetDb.hpp
  3. 33
      libi2pd/RouterInfo.cpp
  4. 8
      libi2pd/RouterInfo.h

2
libi2pd/NetDb.cpp

@ -659,6 +659,8 @@ namespace data
++it; ++it;
} }
} }
m_RouterInfoBuffersPool.CleanUpMt ();
} }
void NetDb::RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete, bool direct) void NetDb::RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete, bool direct)

4
libi2pd/NetDb.hpp

@ -30,6 +30,7 @@
#include "NetDbRequests.h" #include "NetDbRequests.h"
#include "Family.h" #include "Family.h"
#include "version.h" #include "version.h"
#include "util.h"
namespace i2p namespace i2p
{ {
@ -120,6 +121,7 @@ namespace data
size_t VisitRandomRouterInfos(RouterInfoFilter f, RouterInfoVisitor v, size_t n); size_t VisitRandomRouterInfos(RouterInfoFilter f, RouterInfoVisitor v, size_t n);
void ClearRouterInfos () { m_RouterInfos.clear (); }; void ClearRouterInfos () { m_RouterInfos.clear (); };
std::shared_ptr<RouterInfo::Buffer> NewRouterInfoBuffer () { return m_RouterInfoBuffersPool.AcquireSharedMt (); };
uint32_t GetPublishReplyToken () const { return m_PublishReplyToken; }; uint32_t GetPublishReplyToken () const { return m_PublishReplyToken; };
@ -175,6 +177,8 @@ namespace data
std::set<IdentHash> m_PublishExcluded; std::set<IdentHash> m_PublishExcluded;
uint32_t m_PublishReplyToken = 0; uint32_t m_PublishReplyToken = 0;
i2p::util::MemoryPoolMt<RouterInfo::Buffer> m_RouterInfoBuffersPool;
}; };
extern NetDb netdb; extern NetDb netdb;

33
libi2pd/RouterInfo.cpp

@ -40,7 +40,7 @@ namespace data
m_Caps (0), m_Version (0) m_Caps (0), m_Version (0)
{ {
m_Addresses = boost::make_shared<Addresses>(); // create empty list m_Addresses = boost::make_shared<Addresses>(); // create empty list
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; m_Buffer = netdb.NewRouterInfoBuffer ();
ReadFromFile (fullPath); ReadFromFile (fullPath);
} }
@ -51,8 +51,8 @@ namespace data
m_Addresses = boost::make_shared<Addresses>(); // create empty list m_Addresses = boost::make_shared<Addresses>(); // create empty list
if (len <= MAX_RI_BUFFER_SIZE) if (len <= MAX_RI_BUFFER_SIZE)
{ {
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; m_Buffer = netdb.NewRouterInfoBuffer ();
memcpy (m_Buffer, buf, len); memcpy (m_Buffer->data (), buf, len);
m_BufferLen = len; m_BufferLen = len;
ReadFromBuffer (true); ReadFromBuffer (true);
} }
@ -66,7 +66,6 @@ namespace data
RouterInfo::~RouterInfo () RouterInfo::~RouterInfo ()
{ {
delete[] m_Buffer;
} }
void RouterInfo::Update (const uint8_t * buf, size_t len) void RouterInfo::Update (const uint8_t * buf, size_t len)
@ -91,13 +90,13 @@ namespace data
m_Properties.clear (); m_Properties.clear ();
// copy buffer // copy buffer
if (!m_Buffer) if (!m_Buffer)
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; m_Buffer = netdb.NewRouterInfoBuffer ();
memcpy (m_Buffer, buf, len); memcpy (m_Buffer->data (), buf, len);
m_BufferLen = len; m_BufferLen = len;
// skip identity // skip identity
size_t identityLen = m_RouterIdentity->GetFullLen (); size_t identityLen = m_RouterIdentity->GetFullLen ();
// read new RI // read new RI
std::stringstream str (std::string ((char *)m_Buffer + identityLen, m_BufferLen - identityLen)); std::stringstream str (std::string ((char *)m_Buffer->data () + identityLen, m_BufferLen - identityLen));
ReadFromStream (str); ReadFromStream (str);
// don't delete buffer until saved to the file // don't delete buffer until saved to the file
} }
@ -128,8 +127,8 @@ namespace data
} }
s.seekg(0, std::ios::beg); s.seekg(0, std::ios::beg);
if (!m_Buffer) if (!m_Buffer)
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; m_Buffer = netdb.NewRouterInfoBuffer ();
s.read((char *)m_Buffer, m_BufferLen); s.read((char *)m_Buffer->data (), m_BufferLen);
} }
else else
{ {
@ -154,7 +153,7 @@ namespace data
m_IsUnreachable = true; m_IsUnreachable = true;
return; return;
} }
m_RouterIdentity = std::make_shared<IdentityEx>(m_Buffer, m_BufferLen); m_RouterIdentity = std::make_shared<IdentityEx>(m_Buffer->data (), m_BufferLen);
size_t identityLen = m_RouterIdentity->GetFullLen (); size_t identityLen = m_RouterIdentity->GetFullLen ();
if (identityLen >= m_BufferLen) if (identityLen >= m_BufferLen)
{ {
@ -173,7 +172,7 @@ namespace data
} }
// verify signature // verify signature
int l = m_BufferLen - m_RouterIdentity->GetSignatureLen (); int l = m_BufferLen - m_RouterIdentity->GetSignatureLen ();
if (l < 0 || !m_RouterIdentity->Verify ((uint8_t *)m_Buffer, l, (uint8_t *)m_Buffer + l)) if (l < 0 || !m_RouterIdentity->Verify ((uint8_t *)m_Buffer->data (), l, (uint8_t *)m_Buffer->data () + l))
{ {
LogPrint (eLogError, "RouterInfo: Signature verification failed"); LogPrint (eLogError, "RouterInfo: Signature verification failed");
m_IsUnreachable = true; m_IsUnreachable = true;
@ -183,7 +182,7 @@ namespace data
} }
// parse RI // parse RI
std::stringstream str; std::stringstream str;
str.write ((const char *)m_Buffer + identityLen, m_BufferLen - identityLen); str.write ((const char *)m_Buffer->data () + identityLen, m_BufferLen - identityLen);
ReadFromStream (str); ReadFromStream (str);
if (!str) if (!str)
{ {
@ -775,7 +774,7 @@ namespace data
if (LoadFile (fullPath)) if (LoadFile (fullPath))
LogPrint (eLogDebug, "RouterInfo: Buffer for ", GetIdentHashAbbreviation (GetIdentHash ()), " loaded from file"); LogPrint (eLogDebug, "RouterInfo: Buffer for ", GetIdentHashAbbreviation (GetIdentHash ()), " loaded from file");
} }
return m_Buffer; return m_Buffer->data ();
} }
void RouterInfo::CreateBuffer (const PrivateKeys& privateKeys) void RouterInfo::CreateBuffer (const PrivateKeys& privateKeys)
@ -789,12 +788,12 @@ namespace data
WriteToStream (s); WriteToStream (s);
m_BufferLen = s.str ().size (); m_BufferLen = s.str ().size ();
if (!m_Buffer) if (!m_Buffer)
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE]; m_Buffer = netdb.NewRouterInfoBuffer ();
if (m_BufferLen + signatureLen < MAX_RI_BUFFER_SIZE) if (m_BufferLen + signatureLen < MAX_RI_BUFFER_SIZE)
{ {
memcpy (m_Buffer, s.str ().c_str (), m_BufferLen); memcpy (m_Buffer->data (), s.str ().c_str (), m_BufferLen);
// signature // signature
privateKeys.Sign ((uint8_t *)m_Buffer, m_BufferLen, (uint8_t *)m_Buffer + m_BufferLen); privateKeys.Sign ((uint8_t *)m_Buffer->data (), m_BufferLen, (uint8_t *)m_Buffer->data () + m_BufferLen);
m_BufferLen += signatureLen; m_BufferLen += signatureLen;
} }
else else
@ -813,7 +812,7 @@ namespace data
LogPrint(eLogError, "RouterInfo: Can't save to ", fullPath); LogPrint(eLogError, "RouterInfo: Can't save to ", fullPath);
return false; return false;
} }
f.write ((char *)m_Buffer, m_BufferLen); f.write ((char *)m_Buffer->data (), m_BufferLen);
return true; return true;
} }

8
libi2pd/RouterInfo.h

@ -13,6 +13,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <vector> #include <vector>
#include <array>
#include <iostream> #include <iostream>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
@ -158,6 +159,7 @@ namespace data
bool IsV6 () const { return (caps & AddressCaps::eV6) || (host.is_v6 () && !host.is_unspecified ()); }; bool IsV6 () const { return (caps & AddressCaps::eV6) || (host.is_v6 () && !host.is_unspecified ()); };
}; };
typedef std::vector<std::shared_ptr<Address> > Addresses; typedef std::vector<std::shared_ptr<Address> > Addresses;
typedef std::array<uint8_t, MAX_RI_BUFFER_SIZE> Buffer;
RouterInfo (); RouterInfo ();
RouterInfo (const std::string& fullPath); RouterInfo (const std::string& fullPath);
@ -225,7 +227,7 @@ namespace data
void SetUnreachable (bool unreachable) { m_IsUnreachable = unreachable; }; void SetUnreachable (bool unreachable) { m_IsUnreachable = unreachable; };
bool IsUnreachable () const { return m_IsUnreachable; }; bool IsUnreachable () const { return m_IsUnreachable; };
const uint8_t * GetBuffer () const { return m_Buffer; }; const uint8_t * GetBuffer () const { return m_Buffer->data (); };
const uint8_t * LoadBuffer (const std::string& fullPath); // load if necessary const uint8_t * LoadBuffer (const std::string& fullPath); // load if necessary
int GetBufferLen () const { return m_BufferLen; }; int GetBufferLen () const { return m_BufferLen; };
void CreateBuffer (const PrivateKeys& privateKeys); void CreateBuffer (const PrivateKeys& privateKeys);
@ -238,7 +240,7 @@ namespace data
void SaveProfile () { if (m_Profile) m_Profile->Save (GetIdentHash ()); }; void SaveProfile () { if (m_Profile) m_Profile->Save (GetIdentHash ()); };
void Update (const uint8_t * buf, size_t len); void Update (const uint8_t * buf, size_t len);
void DeleteBuffer () { delete[] m_Buffer; m_Buffer = nullptr; }; void DeleteBuffer () { m_Buffer = nullptr; };
bool IsNewer (const uint8_t * buf, size_t len) const; bool IsNewer (const uint8_t * buf, size_t len) const;
/** return true if we are in a router family and the signature is valid */ /** return true if we are in a router family and the signature is valid */
@ -269,7 +271,7 @@ namespace data
std::string m_Family; std::string m_Family;
std::shared_ptr<const IdentityEx> m_RouterIdentity; std::shared_ptr<const IdentityEx> m_RouterIdentity;
uint8_t * m_Buffer; std::shared_ptr<Buffer> m_Buffer;
size_t m_BufferLen; size_t m_BufferLen;
uint64_t m_Timestamp; uint64_t m_Timestamp;
boost::shared_ptr<Addresses> m_Addresses; // TODO: use std::shared_ptr and std::atomic_store for gcc >= 4.9 boost::shared_ptr<Addresses> m_Addresses; // TODO: use std::shared_ptr and std::atomic_store for gcc >= 4.9

Loading…
Cancel
Save