From 49cbbad5d7cb835f8cbe42e8e581900c8b8bd62f Mon Sep 17 00:00:00 2001 From: rakshasa Date: Thu, 18 Feb 2010 06:26:06 +0000 Subject: [PATCH] * Make the torrent::Object immediately after loading a file to lower memory footprint. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1133 e378c898-3ddf-0310-93e7-cc216c733640 --- src/core/download_factory.cc | 51 +++++++++++++++++++++--------------- src/core/download_factory.h | 1 + src/core/download_list.cc | 21 +++++++++++++++ src/core/download_list.h | 1 + 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/core/download_factory.cc b/src/core/download_factory.cc index 16aee928..24402871 100644 --- a/src/core/download_factory.cc +++ b/src/core/download_factory.cc @@ -70,9 +70,27 @@ is_network_uri(const std::string& uri) { std::strncmp(uri.c_str(), "ftp://", 6) == 0; } +static bool +download_factory_add_stream(torrent::Object* root, const char* key, const char* filename) { + std::fstream stream(filename, std::ios::in | std::ios::binary); + + if (!stream.is_open()) + return false; + + torrent::Object obj; + stream >> obj; + + if (!stream.good()) + return false; + + root->insert_key_move(key, obj); + return true; +} + DownloadFactory::DownloadFactory(Manager* m) : m_manager(m), m_stream(NULL), + m_object(NULL), m_commited(false), m_loaded(false), @@ -95,6 +113,7 @@ DownloadFactory::~DownloadFactory() { priority_queue_erase(&taskScheduler, &m_taskCommit); delete m_stream; + delete m_object; m_stream = NULL; } @@ -140,10 +159,13 @@ DownloadFactory::receive_load() { if (!stream.is_open()) return receive_failed("Could not open file"); - m_stream = new std::stringstream; - m_isFile = true; + m_object = new torrent::Object; + stream >> *m_object; - *m_stream << stream.rdbuf(); + if (!stream.good()) + return receive_failed("Reading torrent file failed"); + + m_isFile = true; receive_loaded(); } @@ -165,29 +187,16 @@ DownloadFactory::receive_commit() { receive_success(); } -static bool -download_factory_add_stream(torrent::Object* root, const char* key, const char* filename) { - std::fstream stream(filename, std::ios::in | std::ios::binary); - - if (!stream.is_open()) - return false; - - torrent::Object obj; - stream >> obj; - - if (!stream.good()) - return false; - - root->insert_key_move(key, obj); - return true; -} - void DownloadFactory::receive_success() { if (m_stream == NULL) throw torrent::internal_error("DownloadFactory::receive_success() called on an object with m_stream == NULL."); - Download* download = m_manager->download_list()->create(m_stream, m_printLog); + Download* download = m_stream != NULL ? + m_manager->download_list()->create(m_stream, m_printLog) : + m_manager->download_list()->create(m_object, m_printLog); + + m_object = NULL; if (download == NULL) { // core::Manager should already have added the error message to diff --git a/src/core/download_factory.h b/src/core/download_factory.h index 045c9dca..4b8549d0 100644 --- a/src/core/download_factory.h +++ b/src/core/download_factory.h @@ -93,6 +93,7 @@ private: Manager* m_manager; std::iostream* m_stream; + torrent::Object* m_object; bool m_commited; bool m_loaded; diff --git a/src/core/download_list.cc b/src/core/download_list.cc index bba7ff48..e8b48b7c 100644 --- a/src/core/download_list.cc +++ b/src/core/download_list.cc @@ -112,6 +112,27 @@ DownloadList::find_hex_ptr(const char* hash) { return itr != end() ? *itr : NULL; } +Download* +DownloadList::create(torrent::Object* obj, bool printLog) { + torrent::Download download; + + try { + download = torrent::download_add(obj); + + } catch (torrent::local_error& e) { + delete obj; + + if (printLog) + control->core()->push_log(e.what()); + + return NULL; + } + + // There's no non-critical exceptions that should be throwable by + // the ctor, so don't catch. + return new Download(download); +} + Download* DownloadList::create(std::istream* str, bool printLog) { torrent::Object* object = new torrent::Object; diff --git a/src/core/download_list.h b/src/core/download_list.h index f7828ead..b52c8808 100644 --- a/src/core/download_list.h +++ b/src/core/download_list.h @@ -87,6 +87,7 @@ public: // Might move this to DownloadFactory. Download* create(std::istream* str, bool printLog); + Download* create(torrent::Object* obj, bool printLog); iterator insert(Download* d);