From 5f35d170b0fc2d1120f3c6cfa9586d6945a25147 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 10 Apr 2008 09:11:54 +0000 Subject: [PATCH] disk io thread cleanup (all read operations are now read into disk buffers, no custom buffers) --- include/libtorrent/storage.hpp | 1 - src/disk_io_thread.cpp | 28 +++++++++++++++------------- src/storage.cpp | 5 ++--- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 32cf7b9c5..7a953f210 100755 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -212,7 +212,6 @@ namespace libtorrent void async_read( peer_request const& r , boost::function const& handler - , char* buffer = 0 , int priority = 0); void async_write( diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 3afd7a5c3..11f86dbd4 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -741,7 +741,6 @@ namespace libtorrent int ret = 0; - bool allocated_buffer = false; TORRENT_ASSERT(j.storage); #ifdef TORRENT_DISK_STATS ptime start = time_now(); @@ -770,17 +769,14 @@ namespace libtorrent #endif mutex_t::scoped_lock l(m_mutex); INVARIANT_CHECK; + TORRENT_ASSERT(j.buffer == 0); + j.buffer = allocate_buffer(); + TORRENT_ASSERT(j.buffer_size <= m_block_size); if (j.buffer == 0) { - j.buffer = allocate_buffer(); - allocated_buffer = true; - TORRENT_ASSERT(j.buffer_size <= m_block_size); - if (j.buffer == 0) - { - ret = -1; - j.str = "out of memory"; - break; - } + ret = -1; + j.str = "out of memory"; + break; } ret = try_read_from_cache(j, l); @@ -789,7 +785,7 @@ namespace libtorrent // or that the read cache is disabled if (ret == -1) { - if (allocated_buffer) free_buffer(j.buffer, l); + free_buffer(j.buffer, l); j.buffer = 0; j.str = j.storage->error(); j.storage->clear_error(); @@ -802,7 +798,7 @@ namespace libtorrent , j.buffer_size); if (ret < 0) { - if (allocated_buffer) free_buffer(j.buffer); + free_buffer(j.buffer); j.str = j.storage->error(); j.storage->clear_error(); break; @@ -876,6 +872,7 @@ namespace libtorrent #ifdef TORRENT_DISK_STATS m_log << log_time() << " move" << std::endl; #endif + TORRENT_ASSERT(j.buffer == 0); ret = j.storage->move_storage_impl(j.str) ? 1 : 0; if (ret != 0) { @@ -891,6 +888,7 @@ namespace libtorrent #ifdef TORRENT_DISK_STATS m_log << log_time() << " release" << std::endl; #endif + TORRENT_ASSERT(j.buffer == 0); mutex_t::scoped_lock l(m_mutex); INVARIANT_CHECK; @@ -924,6 +922,7 @@ namespace libtorrent #ifdef TORRENT_DISK_STATS m_log << log_time() << " delete" << std::endl; #endif + TORRENT_ASSERT(j.buffer == 0); mutex_t::scoped_lock l(m_mutex); INVARIANT_CHECK; @@ -1017,7 +1016,10 @@ namespace libtorrent #endif if (handler) m_ios.post(bind(handler, ret, j)); #ifndef BOOST_NO_EXCEPTIONS - } catch (std::exception&) {} + } catch (std::exception&) + { + if (j.buffer) free_buffer(j.buffer); + } #endif } TORRENT_ASSERT(false); diff --git a/src/storage.cpp b/src/storage.cpp index 1a59a3bd3..0436063a9 100755 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -1221,7 +1221,6 @@ namespace libtorrent void piece_manager::async_read( peer_request const& r , boost::function const& handler - , char* buffer , int priority) { disk_io_job j; @@ -1230,11 +1229,11 @@ namespace libtorrent j.piece = r.piece; j.offset = r.start; j.buffer_size = r.length; - j.buffer = buffer; + j.buffer = 0; j.priority = priority; // if a buffer is not specified, only one block can be read // since that is the size of the pool allocator's buffers - TORRENT_ASSERT(r.length <= 16 * 1024 || buffer != 0); + TORRENT_ASSERT(r.length <= 16 * 1024); m_io_thread.add_job(j, handler); #ifndef NDEBUG boost::recursive_mutex::scoped_lock l(m_mutex);