From 21dff61cb5bf286cedd59ab0fc28010943b43009 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 18 Jul 2008 15:31:22 +0000 Subject: [PATCH] clears read cache when paused. Saves memory --- include/libtorrent/disk_io_thread.hpp | 1 + include/libtorrent/storage.hpp | 4 ++++ src/disk_io_thread.cpp | 33 +++++++++++++++++++++++++++ src/storage.cpp | 9 ++++++++ src/torrent.cpp | 1 + 5 files changed, 48 insertions(+) diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index e7abbbf8b..922896741 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -86,6 +86,7 @@ namespace libtorrent , save_resume_data , rename_file , abort_thread + , clear_read_cache }; action_t action; diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 9f6b6b2b3..d2847cb8e 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -236,6 +236,10 @@ namespace libtorrent boost::function const& handler = boost::function()); + void async_clear_read_cache( + boost::function const& handler + = boost::function()); + void async_delete_files( boost::function const& handler = boost::function()); diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 175d68f31..f091c3db8 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -1003,6 +1003,39 @@ namespace libtorrent if (ret != 0) test_error(j); break; } + case disk_io_job::clear_read_cache: + { +#ifdef TORRENT_DISK_STATS + m_log << log_time() << " clear-cache" << std::endl; +#endif + TORRENT_ASSERT(j.buffer == 0); + + mutex_t::scoped_lock l(m_piece_mutex); + INVARIANT_CHECK; + + for (cache_t::iterator i = m_read_pieces.begin(); + i != m_read_pieces.end();) + { + if (i->storage == j.storage) + { + free_piece(*i, l); + i = m_read_pieces.erase(i); + } + else + { + ++i; + } + } + l.unlock(); +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR + { + mutex_t::scoped_lock l(m_pool_mutex); + m_pool.release_memory(); + } +#endif + ret = 0; + break; + } case disk_io_job::delete_files: { #ifdef TORRENT_DISK_STATS diff --git a/src/storage.cpp b/src/storage.cpp index 6bdd1b5fc..3db8ac904 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -1275,6 +1275,15 @@ namespace libtorrent m_io_thread.add_job(j, handler); } + void piece_manager::async_clear_read_cache( + boost::function const& handler) + { + disk_io_job j; + j.storage = this; + j.action = disk_io_job::clear_read_cache; + m_io_thread.add_job(j, handler); + } + void piece_manager::async_release_files( boost::function const& handler) { diff --git a/src/torrent.cpp b/src/torrent.cpp index 66d6d1c49..3cba55d65 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -3753,6 +3753,7 @@ namespace libtorrent TORRENT_ASSERT(m_storage); m_storage->async_release_files( bind(&torrent::on_torrent_paused, shared_from_this(), _1, _2)); + m_storage->async_clear_read_cache(); } else {