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

4
libi2pd/NetDb.hpp

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

33
libi2pd/RouterInfo.cpp

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

8
libi2pd/RouterInfo.h

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

Loading…
Cancel
Save