Browse Source

reduce memory usage by deleting RI's buffer after parsing and saving

pull/93/head
orignal 10 years ago
parent
commit
3a8d4403f6
  1. 2
      NetDb.cpp
  2. 21
      RouterInfo.cpp
  3. 10
      RouterInfo.h

2
NetDb.cpp

@ -264,6 +264,7 @@ namespace data
#else #else
RouterInfo * r = new RouterInfo(it1->path().c_str()); RouterInfo * r = new RouterInfo(it1->path().c_str());
#endif #endif
r->DeleteBuffer ();
m_RouterInfos[r->GetIdentHash ()] = r; m_RouterInfos[r->GetIdentHash ()] = r;
if (r->IsFloodfill ()) if (r->IsFloodfill ())
m_Floodfills.push_back (r); m_Floodfills.push_back (r);
@ -305,6 +306,7 @@ namespace data
{ {
it.second->SaveToFile (GetFilePath(fullDirectory, it.second)); it.second->SaveToFile (GetFilePath(fullDirectory, it.second));
it.second->SetUpdated (false); it.second->SetUpdated (false);
it.second->DeleteBuffer ();
count++; count++;
} }
else else

21
RouterInfo.cpp

@ -20,19 +20,28 @@ namespace data
RouterInfo::RouterInfo (const char * filename): RouterInfo::RouterInfo (const char * filename):
m_IsUpdated (false), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0) m_IsUpdated (false), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0)
{ {
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
ReadFromFile (filename); ReadFromFile (filename);
} }
RouterInfo::RouterInfo (const uint8_t * buf, int len): RouterInfo::RouterInfo (const uint8_t * buf, int len):
m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0) m_IsUpdated (true), m_IsUnreachable (false), m_SupportedTransports (0), m_Caps (0)
{ {
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
memcpy (m_Buffer, buf, len); memcpy (m_Buffer, buf, len);
m_BufferLen = len; m_BufferLen = len;
ReadFromBuffer (); ReadFromBuffer ();
} }
RouterInfo::~RouterInfo ()
{
delete m_Buffer;
}
void RouterInfo::Update (const uint8_t * buf, int len) void RouterInfo::Update (const uint8_t * buf, int len)
{ {
if (!m_Buffer)
m_Buffer = new uint8_t[MAX_RI_BUFFER_SIZE];
m_IsUpdated = true; m_IsUpdated = true;
m_IsUnreachable = false; m_IsUnreachable = false;
m_SupportedTransports = 0; m_SupportedTransports = 0;
@ -42,12 +51,13 @@ namespace data
memcpy (m_Buffer, buf, len); memcpy (m_Buffer, buf, len);
m_BufferLen = len; m_BufferLen = len;
ReadFromBuffer (); ReadFromBuffer ();
// don't delete buffer until save to file
} }
void RouterInfo::SetRouterIdentity (const Identity& identity) void RouterInfo::SetRouterIdentity (const Identity& identity)
{ {
m_RouterIdentity = identity; m_RouterIdentity = identity;
m_IdentHash = m_RouterIdentity.Hash (); m_IdentHash = m_RouterIdentity.Hash ();
UpdateIdentHashBase64 (); UpdateIdentHashBase64 ();
UpdateRoutingKey (); UpdateRoutingKey ();
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
@ -338,8 +348,13 @@ namespace data
void RouterInfo::SaveToFile (const std::string& fullPath) void RouterInfo::SaveToFile (const std::string& fullPath)
{ {
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out); if (m_Buffer)
f.write ((char *)m_Buffer, m_BufferLen); {
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
f.write ((char *)m_Buffer, m_BufferLen);
}
else
LogPrint ("Can't save to file");
} }
size_t RouterInfo::ReadString (char * str, std::istream& s) size_t RouterInfo::ReadString (char * str, std::istream& s)

10
RouterInfo.h

@ -64,11 +64,11 @@ namespace data
}; };
RouterInfo (const char * filename); RouterInfo (const char * filename);
RouterInfo () = default; RouterInfo (): m_Buffer (nullptr) {};
RouterInfo (const RouterInfo& ) = default; RouterInfo (const RouterInfo& ) = default;
RouterInfo& operator=(const RouterInfo& ) = default; RouterInfo& operator=(const RouterInfo& ) = default;
RouterInfo (const uint8_t * buf, int len); RouterInfo (const uint8_t * buf, int len);
void Update (const uint8_t * buf, int len); ~RouterInfo ();
const Identity& GetRouterIdentity () const { return m_RouterIdentity; }; const Identity& GetRouterIdentity () const { return m_RouterIdentity; };
void SetRouterIdentity (const Identity& identity); void SetRouterIdentity (const Identity& identity);
@ -108,10 +108,14 @@ namespace data
void SetUpdated (bool updated) { m_IsUpdated = updated; }; void SetUpdated (bool updated) { m_IsUpdated = updated; };
void SaveToFile (const std::string& fullPath); void SaveToFile (const std::string& fullPath);
void Update (const uint8_t * buf, int len);
void DeleteBuffer () { delete m_Buffer; m_Buffer = nullptr; };
// implements RoutingDestination // implements RoutingDestination
const IdentHash& GetIdentHash () const { return m_IdentHash; }; const IdentHash& GetIdentHash () const { return m_IdentHash; };
const uint8_t * GetEncryptionPublicKey () const { return m_RouterIdentity.publicKey; }; const uint8_t * GetEncryptionPublicKey () const { return m_RouterIdentity.publicKey; };
bool IsDestination () const { return false; }; bool IsDestination () const { return false; };
private: private:
@ -131,7 +135,7 @@ namespace data
IdentHash m_IdentHash; IdentHash m_IdentHash;
RoutingKey m_RoutingKey; RoutingKey m_RoutingKey;
char m_IdentHashBase64[48], m_IdentHashAbbreviation[5]; char m_IdentHashBase64[48], m_IdentHashAbbreviation[5];
uint8_t m_Buffer[MAX_RI_BUFFER_SIZE]; uint8_t * m_Buffer;
int m_BufferLen; int m_BufferLen;
uint64_t m_Timestamp; uint64_t m_Timestamp;
std::vector<Address> m_Addresses; std::vector<Address> m_Addresses;

Loading…
Cancel
Save