From d15573ecc2d2222a6b1a06fb1809f63bdd69af69 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 5 May 2008 06:25:22 +0000 Subject: [PATCH] disk receive buffer cleanup --- include/libtorrent/disk_buffer_holder.hpp | 8 +++- include/libtorrent/peer_connection.hpp | 4 +- src/bt_peer_connection.cpp | 2 +- src/disk_buffer_holder.cpp | 6 +++ src/peer_connection.cpp | 47 ++++++++++------------- src/storage.cpp | 4 +- 6 files changed, 38 insertions(+), 33 deletions(-) diff --git a/include/libtorrent/disk_buffer_holder.hpp b/include/libtorrent/disk_buffer_holder.hpp index 1a60909f7..31be6b3c7 100644 --- a/include/libtorrent/disk_buffer_holder.hpp +++ b/include/libtorrent/disk_buffer_holder.hpp @@ -47,7 +47,13 @@ namespace libtorrent disk_buffer_holder(disk_io_thread& iothread, char* buf); ~disk_buffer_holder(); char* release(); - char* buffer() const { return m_buf; } + char* get() const { return m_buf; } + void reset(char* buf = 0); + + typedef char* (disk_buffer_holder::*unspecified_bool_type)(); + operator unspecified_bool_type() const + { return m_buf == 0? 0: &disk_buffer_holder::release; } + private: disk_io_thread& m_iothread; char* m_buf; diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 4514bb86e..28b452063 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -457,7 +457,7 @@ namespace libtorrent #ifndef TORRENT_DISABLE_ENCRYPTION buffer::interval wr_recv_buffer() { - TORRENT_ASSERT(m_disk_recv_buffer == 0); + TORRENT_ASSERT(!m_disk_recv_buffer); TORRENT_ASSERT(m_disk_recv_buffer_size == 0); if (m_recv_buffer.empty()) return buffer::interval(0,0); return buffer::interval(&m_recv_buffer[0] @@ -568,7 +568,7 @@ namespace libtorrent // read into. This eliminates a memcopy from // the receive buffer into the disk buffer int m_disk_recv_buffer_size; - char* m_disk_recv_buffer; + disk_buffer_holder m_disk_recv_buffer; chained_buffer m_send_buffer; diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index f2324697e..e4eb00ceb 100755 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -1671,7 +1671,7 @@ namespace libtorrent detail::write_int32(r.start, ptr); send_buffer(msg, sizeof(msg)); - append_send_buffer(buffer.buffer(), r.length + append_send_buffer(buffer.get(), r.length , boost::bind(&session_impl::free_disk_buffer , boost::ref(m_ses), _1)); buffer.release(); diff --git a/src/disk_buffer_holder.cpp b/src/disk_buffer_holder.cpp index 01e0f21ce..1ee83e93e 100644 --- a/src/disk_buffer_holder.cpp +++ b/src/disk_buffer_holder.cpp @@ -49,6 +49,12 @@ namespace libtorrent TORRENT_ASSERT(buf == 0 || m_iothread.is_disk_buffer(buf)); } + void disk_buffer_holder::reset(char* buf) + { + if (m_buf) m_iothread.free_buffer(m_buf); + m_buf = buf; + } + char* disk_buffer_holder::release() { char* ret = m_buf; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 7fa6b5503..3651c0e7a 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -81,7 +81,7 @@ namespace libtorrent , m_packet_size(0) , m_recv_pos(0) , m_disk_recv_buffer_size(0) - , m_disk_recv_buffer(0) + , m_disk_recv_buffer(ses, 0) , m_reading_bytes(0) , m_last_receive(time_now()) , m_last_sent(time_now()) @@ -180,7 +180,7 @@ namespace libtorrent , m_packet_size(0) , m_recv_pos(0) , m_disk_recv_buffer_size(0) - , m_disk_recv_buffer(0) + , m_disk_recv_buffer(ses, 0) , m_reading_bytes(0) , m_last_receive(time_now()) , m_last_sent(time_now()) @@ -476,12 +476,7 @@ namespace libtorrent TORRENT_ASSERT(!m_in_constructor); TORRENT_ASSERT(m_disconnecting); - if (m_disk_recv_buffer) - { - m_ses.free_disk_buffer(m_disk_recv_buffer); - m_disk_recv_buffer = 0; - m_disk_recv_buffer_size = 0; - } + m_disk_recv_buffer_size = 0; #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING if (m_logger) @@ -1426,7 +1421,7 @@ namespace libtorrent boost::shared_ptr t = m_torrent.lock(); TORRENT_ASSERT(t); - TORRENT_ASSERT(m_disk_recv_buffer == 0); + TORRENT_ASSERT(!m_disk_recv_buffer); TORRENT_ASSERT(m_disk_recv_buffer_size == 0); #ifndef TORRENT_DISABLE_EXTENSIONS @@ -2372,7 +2367,7 @@ namespace libtorrent TORRENT_ASSERT(m_packet_size > 0); TORRENT_ASSERT(m_recv_pos <= m_packet_size - disk_buffer_size); - TORRENT_ASSERT(m_disk_recv_buffer == 0); + TORRENT_ASSERT(!m_disk_recv_buffer); TORRENT_ASSERT(disk_buffer_size <= 16 * 1024); if (disk_buffer_size > 16 * 1024) @@ -2381,8 +2376,8 @@ namespace libtorrent return false; } - m_disk_recv_buffer = m_ses.allocate_disk_buffer(); - if (m_disk_recv_buffer == 0) + m_disk_recv_buffer.reset(m_ses.allocate_disk_buffer()); + if (!m_disk_recv_buffer) { disconnect("out of memory"); return false; @@ -2393,10 +2388,8 @@ namespace libtorrent char* peer_connection::release_disk_receive_buffer() { - char* ret = m_disk_recv_buffer; - m_disk_recv_buffer = 0; m_disk_recv_buffer_size = 0; - return ret; + return m_disk_recv_buffer.release(); } void peer_connection::cut_receive_buffer(int size, int packet_size) @@ -2829,7 +2822,7 @@ namespace libtorrent if (int(m_recv_buffer.size()) < regular_buffer_size) m_recv_buffer.resize(regular_buffer_size); - if (m_disk_recv_buffer == 0 || regular_buffer_size >= m_recv_pos + max_receive) + if (!m_disk_recv_buffer || regular_buffer_size >= m_recv_pos + max_receive) { // only receive into regular buffer TORRENT_ASSERT(m_recv_pos + max_receive <= int(m_recv_buffer.size())); @@ -2841,7 +2834,7 @@ namespace libtorrent // only receive into disk buffer TORRENT_ASSERT(m_recv_pos - regular_buffer_size >= 0); TORRENT_ASSERT(m_recv_pos - regular_buffer_size + max_receive <= m_disk_recv_buffer_size); - m_socket->async_read_some(asio::buffer(m_disk_recv_buffer + m_recv_pos - regular_buffer_size + m_socket->async_read_some(asio::buffer(m_disk_recv_buffer.get() + m_recv_pos - regular_buffer_size , max_receive) , bind(&peer_connection::on_receive_data, self(), _1, _2)); } @@ -2856,7 +2849,7 @@ namespace libtorrent boost::array vec; vec[0] = asio::buffer(&m_recv_buffer[m_recv_pos] , regular_buffer_size - m_recv_pos); - vec[1] = asio::buffer(m_disk_recv_buffer + vec[1] = asio::buffer(m_disk_recv_buffer.get() , max_receive - regular_buffer_size + m_recv_pos); m_socket->async_read_some(vec, bind(&peer_connection::on_receive_data , self(), _1, _2)); @@ -2874,7 +2867,7 @@ namespace libtorrent std::pair vec; int regular_buffer_size = m_packet_size - m_disk_recv_buffer_size; TORRENT_ASSERT(regular_buffer_size >= 0); - if (m_disk_recv_buffer == 0 || regular_buffer_size >= m_recv_pos) + if (!m_disk_recv_buffer || regular_buffer_size >= m_recv_pos) { vec.first = buffer::interval(&m_recv_buffer[0] + m_recv_pos - bytes, &m_recv_buffer[0] + m_recv_pos); @@ -2882,8 +2875,8 @@ namespace libtorrent } else if (m_recv_pos - bytes >= regular_buffer_size) { - vec.first = buffer::interval(m_disk_recv_buffer + m_recv_pos - - regular_buffer_size - bytes, m_disk_recv_buffer + m_recv_pos + vec.first = buffer::interval(m_disk_recv_buffer.get() + m_recv_pos + - regular_buffer_size - bytes, m_disk_recv_buffer.get() + m_recv_pos - regular_buffer_size); vec.second = buffer::interval(0,0); } @@ -2893,8 +2886,8 @@ namespace libtorrent TORRENT_ASSERT(m_recv_pos > regular_buffer_size); vec.first = buffer::interval(&m_recv_buffer[0] + m_recv_pos - bytes , &m_recv_buffer[0] + regular_buffer_size); - vec.second = buffer::interval(m_disk_recv_buffer - , m_disk_recv_buffer + m_recv_pos - regular_buffer_size); + vec.second = buffer::interval(m_disk_recv_buffer.get() + , m_disk_recv_buffer.get() + m_recv_pos - regular_buffer_size); } TORRENT_ASSERT(vec.first.left() + vec.second.left() == bytes); return vec; @@ -3064,7 +3057,7 @@ namespace libtorrent m_recv_buffer.resize(regular_buffer_size); error_code ec; - if (m_disk_recv_buffer == 0 || regular_buffer_size >= m_recv_pos + max_receive) + if (!m_disk_recv_buffer || regular_buffer_size >= m_recv_pos + max_receive) { // only receive into regular buffer TORRENT_ASSERT(m_recv_pos + max_receive <= int(m_recv_buffer.size())); @@ -3076,7 +3069,7 @@ namespace libtorrent // only receive into disk buffer TORRENT_ASSERT(m_recv_pos - regular_buffer_size >= 0); TORRENT_ASSERT(m_recv_pos - regular_buffer_size + max_receive <= m_disk_recv_buffer_size); - bytes_transferred = m_socket->read_some(asio::buffer(m_disk_recv_buffer + bytes_transferred = m_socket->read_some(asio::buffer(m_disk_recv_buffer.get() + m_recv_pos - regular_buffer_size, (std::min)(m_packet_size - m_recv_pos, max_receive)), ec); } @@ -3091,7 +3084,7 @@ namespace libtorrent boost::array vec; vec[0] = asio::buffer(&m_recv_buffer[m_recv_pos] , regular_buffer_size - m_recv_pos); - vec[1] = asio::buffer(m_disk_recv_buffer + vec[1] = asio::buffer(m_disk_recv_buffer.get() , (std::min)(m_disk_recv_buffer_size , max_receive - regular_buffer_size + m_recv_pos)); bytes_transferred = m_socket->read_some(vec, ec); @@ -3306,7 +3299,7 @@ namespace libtorrent #ifndef NDEBUG void peer_connection::check_invariant() const { - TORRENT_ASSERT((m_disk_recv_buffer != 0) == (m_disk_recv_buffer_size > 0)); + TORRENT_ASSERT(bool(m_disk_recv_buffer) == (m_disk_recv_buffer_size > 0)); boost::shared_ptr t = m_torrent.lock(); if (m_disconnecting) diff --git a/src/storage.cpp b/src/storage.cpp index 51fab7e12..0596cc1b9 100755 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -1230,7 +1230,7 @@ namespace libtorrent { TORRENT_ASSERT(r.length <= 16 * 1024); // the buffer needs to be allocated through the io_thread - TORRENT_ASSERT(m_io_thread.is_disk_buffer(buffer.buffer())); + TORRENT_ASSERT(m_io_thread.is_disk_buffer(buffer.get())); disk_io_job j; j.storage = this; @@ -1238,7 +1238,7 @@ namespace libtorrent j.piece = r.piece; j.offset = r.start; j.buffer_size = r.length; - j.buffer = buffer.buffer(); + j.buffer = buffer.get(); m_io_thread.add_job(j, handler); buffer.release(); }