forked from premiere/premiere-libtorrent
disk receive buffer cleanup
This commit is contained in:
parent
7388144ec7
commit
d15573ecc2
|
@ -47,7 +47,13 @@ namespace libtorrent
|
||||||
disk_buffer_holder(disk_io_thread& iothread, char* buf);
|
disk_buffer_holder(disk_io_thread& iothread, char* buf);
|
||||||
~disk_buffer_holder();
|
~disk_buffer_holder();
|
||||||
char* release();
|
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:
|
private:
|
||||||
disk_io_thread& m_iothread;
|
disk_io_thread& m_iothread;
|
||||||
char* m_buf;
|
char* m_buf;
|
||||||
|
|
|
@ -457,7 +457,7 @@ namespace libtorrent
|
||||||
#ifndef TORRENT_DISABLE_ENCRYPTION
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
||||||
buffer::interval wr_recv_buffer()
|
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);
|
TORRENT_ASSERT(m_disk_recv_buffer_size == 0);
|
||||||
if (m_recv_buffer.empty()) return buffer::interval(0,0);
|
if (m_recv_buffer.empty()) return buffer::interval(0,0);
|
||||||
return buffer::interval(&m_recv_buffer[0]
|
return buffer::interval(&m_recv_buffer[0]
|
||||||
|
@ -568,7 +568,7 @@ namespace libtorrent
|
||||||
// read into. This eliminates a memcopy from
|
// read into. This eliminates a memcopy from
|
||||||
// the receive buffer into the disk buffer
|
// the receive buffer into the disk buffer
|
||||||
int m_disk_recv_buffer_size;
|
int m_disk_recv_buffer_size;
|
||||||
char* m_disk_recv_buffer;
|
disk_buffer_holder m_disk_recv_buffer;
|
||||||
|
|
||||||
chained_buffer m_send_buffer;
|
chained_buffer m_send_buffer;
|
||||||
|
|
||||||
|
|
|
@ -1671,7 +1671,7 @@ namespace libtorrent
|
||||||
detail::write_int32(r.start, ptr);
|
detail::write_int32(r.start, ptr);
|
||||||
send_buffer(msg, sizeof(msg));
|
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::bind(&session_impl::free_disk_buffer
|
||||||
, boost::ref(m_ses), _1));
|
, boost::ref(m_ses), _1));
|
||||||
buffer.release();
|
buffer.release();
|
||||||
|
|
|
@ -49,6 +49,12 @@ namespace libtorrent
|
||||||
TORRENT_ASSERT(buf == 0 || m_iothread.is_disk_buffer(buf));
|
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* disk_buffer_holder::release()
|
||||||
{
|
{
|
||||||
char* ret = m_buf;
|
char* ret = m_buf;
|
||||||
|
|
|
@ -81,7 +81,7 @@ namespace libtorrent
|
||||||
, m_packet_size(0)
|
, m_packet_size(0)
|
||||||
, m_recv_pos(0)
|
, m_recv_pos(0)
|
||||||
, m_disk_recv_buffer_size(0)
|
, m_disk_recv_buffer_size(0)
|
||||||
, m_disk_recv_buffer(0)
|
, m_disk_recv_buffer(ses, 0)
|
||||||
, m_reading_bytes(0)
|
, m_reading_bytes(0)
|
||||||
, m_last_receive(time_now())
|
, m_last_receive(time_now())
|
||||||
, m_last_sent(time_now())
|
, m_last_sent(time_now())
|
||||||
|
@ -180,7 +180,7 @@ namespace libtorrent
|
||||||
, m_packet_size(0)
|
, m_packet_size(0)
|
||||||
, m_recv_pos(0)
|
, m_recv_pos(0)
|
||||||
, m_disk_recv_buffer_size(0)
|
, m_disk_recv_buffer_size(0)
|
||||||
, m_disk_recv_buffer(0)
|
, m_disk_recv_buffer(ses, 0)
|
||||||
, m_reading_bytes(0)
|
, m_reading_bytes(0)
|
||||||
, m_last_receive(time_now())
|
, m_last_receive(time_now())
|
||||||
, m_last_sent(time_now())
|
, m_last_sent(time_now())
|
||||||
|
@ -476,12 +476,7 @@ namespace libtorrent
|
||||||
TORRENT_ASSERT(!m_in_constructor);
|
TORRENT_ASSERT(!m_in_constructor);
|
||||||
TORRENT_ASSERT(m_disconnecting);
|
TORRENT_ASSERT(m_disconnecting);
|
||||||
|
|
||||||
if (m_disk_recv_buffer)
|
m_disk_recv_buffer_size = 0;
|
||||||
{
|
|
||||||
m_ses.free_disk_buffer(m_disk_recv_buffer);
|
|
||||||
m_disk_recv_buffer = 0;
|
|
||||||
m_disk_recv_buffer_size = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
||||||
if (m_logger)
|
if (m_logger)
|
||||||
|
@ -1426,7 +1421,7 @@ namespace libtorrent
|
||||||
boost::shared_ptr<torrent> t = m_torrent.lock();
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
||||||
TORRENT_ASSERT(t);
|
TORRENT_ASSERT(t);
|
||||||
|
|
||||||
TORRENT_ASSERT(m_disk_recv_buffer == 0);
|
TORRENT_ASSERT(!m_disk_recv_buffer);
|
||||||
TORRENT_ASSERT(m_disk_recv_buffer_size == 0);
|
TORRENT_ASSERT(m_disk_recv_buffer_size == 0);
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_EXTENSIONS
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
||||||
|
@ -2372,7 +2367,7 @@ namespace libtorrent
|
||||||
|
|
||||||
TORRENT_ASSERT(m_packet_size > 0);
|
TORRENT_ASSERT(m_packet_size > 0);
|
||||||
TORRENT_ASSERT(m_recv_pos <= m_packet_size - disk_buffer_size);
|
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);
|
TORRENT_ASSERT(disk_buffer_size <= 16 * 1024);
|
||||||
|
|
||||||
if (disk_buffer_size > 16 * 1024)
|
if (disk_buffer_size > 16 * 1024)
|
||||||
|
@ -2381,8 +2376,8 @@ namespace libtorrent
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_disk_recv_buffer = m_ses.allocate_disk_buffer();
|
m_disk_recv_buffer.reset(m_ses.allocate_disk_buffer());
|
||||||
if (m_disk_recv_buffer == 0)
|
if (!m_disk_recv_buffer)
|
||||||
{
|
{
|
||||||
disconnect("out of memory");
|
disconnect("out of memory");
|
||||||
return false;
|
return false;
|
||||||
|
@ -2393,10 +2388,8 @@ namespace libtorrent
|
||||||
|
|
||||||
char* peer_connection::release_disk_receive_buffer()
|
char* peer_connection::release_disk_receive_buffer()
|
||||||
{
|
{
|
||||||
char* ret = m_disk_recv_buffer;
|
|
||||||
m_disk_recv_buffer = 0;
|
|
||||||
m_disk_recv_buffer_size = 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)
|
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)
|
if (int(m_recv_buffer.size()) < regular_buffer_size)
|
||||||
m_recv_buffer.resize(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
|
// only receive into regular buffer
|
||||||
TORRENT_ASSERT(m_recv_pos + max_receive <= int(m_recv_buffer.size()));
|
TORRENT_ASSERT(m_recv_pos + max_receive <= int(m_recv_buffer.size()));
|
||||||
|
@ -2841,7 +2834,7 @@ namespace libtorrent
|
||||||
// only receive into disk buffer
|
// only receive into disk buffer
|
||||||
TORRENT_ASSERT(m_recv_pos - regular_buffer_size >= 0);
|
TORRENT_ASSERT(m_recv_pos - regular_buffer_size >= 0);
|
||||||
TORRENT_ASSERT(m_recv_pos - regular_buffer_size + max_receive <= m_disk_recv_buffer_size);
|
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)
|
, max_receive)
|
||||||
, bind(&peer_connection::on_receive_data, self(), _1, _2));
|
, bind(&peer_connection::on_receive_data, self(), _1, _2));
|
||||||
}
|
}
|
||||||
|
@ -2856,7 +2849,7 @@ namespace libtorrent
|
||||||
boost::array<asio::mutable_buffer, 2> vec;
|
boost::array<asio::mutable_buffer, 2> vec;
|
||||||
vec[0] = asio::buffer(&m_recv_buffer[m_recv_pos]
|
vec[0] = asio::buffer(&m_recv_buffer[m_recv_pos]
|
||||||
, regular_buffer_size - 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);
|
, max_receive - regular_buffer_size + m_recv_pos);
|
||||||
m_socket->async_read_some(vec, bind(&peer_connection::on_receive_data
|
m_socket->async_read_some(vec, bind(&peer_connection::on_receive_data
|
||||||
, self(), _1, _2));
|
, self(), _1, _2));
|
||||||
|
@ -2874,7 +2867,7 @@ namespace libtorrent
|
||||||
std::pair<buffer::interval, buffer::interval> vec;
|
std::pair<buffer::interval, buffer::interval> vec;
|
||||||
int regular_buffer_size = m_packet_size - m_disk_recv_buffer_size;
|
int regular_buffer_size = m_packet_size - m_disk_recv_buffer_size;
|
||||||
TORRENT_ASSERT(regular_buffer_size >= 0);
|
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]
|
vec.first = buffer::interval(&m_recv_buffer[0]
|
||||||
+ m_recv_pos - bytes, &m_recv_buffer[0] + m_recv_pos);
|
+ 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)
|
else if (m_recv_pos - bytes >= regular_buffer_size)
|
||||||
{
|
{
|
||||||
vec.first = buffer::interval(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 + m_recv_pos
|
- regular_buffer_size - bytes, m_disk_recv_buffer.get() + m_recv_pos
|
||||||
- regular_buffer_size);
|
- regular_buffer_size);
|
||||||
vec.second = buffer::interval(0,0);
|
vec.second = buffer::interval(0,0);
|
||||||
}
|
}
|
||||||
|
@ -2893,8 +2886,8 @@ namespace libtorrent
|
||||||
TORRENT_ASSERT(m_recv_pos > regular_buffer_size);
|
TORRENT_ASSERT(m_recv_pos > regular_buffer_size);
|
||||||
vec.first = buffer::interval(&m_recv_buffer[0] + m_recv_pos - bytes
|
vec.first = buffer::interval(&m_recv_buffer[0] + m_recv_pos - bytes
|
||||||
, &m_recv_buffer[0] + regular_buffer_size);
|
, &m_recv_buffer[0] + regular_buffer_size);
|
||||||
vec.second = buffer::interval(m_disk_recv_buffer
|
vec.second = buffer::interval(m_disk_recv_buffer.get()
|
||||||
, m_disk_recv_buffer + m_recv_pos - regular_buffer_size);
|
, m_disk_recv_buffer.get() + m_recv_pos - regular_buffer_size);
|
||||||
}
|
}
|
||||||
TORRENT_ASSERT(vec.first.left() + vec.second.left() == bytes);
|
TORRENT_ASSERT(vec.first.left() + vec.second.left() == bytes);
|
||||||
return vec;
|
return vec;
|
||||||
|
@ -3064,7 +3057,7 @@ namespace libtorrent
|
||||||
m_recv_buffer.resize(regular_buffer_size);
|
m_recv_buffer.resize(regular_buffer_size);
|
||||||
|
|
||||||
error_code ec;
|
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
|
// only receive into regular buffer
|
||||||
TORRENT_ASSERT(m_recv_pos + max_receive <= int(m_recv_buffer.size()));
|
TORRENT_ASSERT(m_recv_pos + max_receive <= int(m_recv_buffer.size()));
|
||||||
|
@ -3076,7 +3069,7 @@ namespace libtorrent
|
||||||
// only receive into disk buffer
|
// only receive into disk buffer
|
||||||
TORRENT_ASSERT(m_recv_pos - regular_buffer_size >= 0);
|
TORRENT_ASSERT(m_recv_pos - regular_buffer_size >= 0);
|
||||||
TORRENT_ASSERT(m_recv_pos - regular_buffer_size + max_receive <= m_disk_recv_buffer_size);
|
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 - regular_buffer_size, (std::min)(m_packet_size
|
||||||
- m_recv_pos, max_receive)), ec);
|
- m_recv_pos, max_receive)), ec);
|
||||||
}
|
}
|
||||||
|
@ -3091,7 +3084,7 @@ namespace libtorrent
|
||||||
boost::array<asio::mutable_buffer, 2> vec;
|
boost::array<asio::mutable_buffer, 2> vec;
|
||||||
vec[0] = asio::buffer(&m_recv_buffer[m_recv_pos]
|
vec[0] = asio::buffer(&m_recv_buffer[m_recv_pos]
|
||||||
, regular_buffer_size - 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
|
, (std::min)(m_disk_recv_buffer_size
|
||||||
, max_receive - regular_buffer_size + m_recv_pos));
|
, max_receive - regular_buffer_size + m_recv_pos));
|
||||||
bytes_transferred = m_socket->read_some(vec, ec);
|
bytes_transferred = m_socket->read_some(vec, ec);
|
||||||
|
@ -3306,7 +3299,7 @@ namespace libtorrent
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
void peer_connection::check_invariant() const
|
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<torrent> t = m_torrent.lock();
|
boost::shared_ptr<torrent> t = m_torrent.lock();
|
||||||
if (m_disconnecting)
|
if (m_disconnecting)
|
||||||
|
|
|
@ -1230,7 +1230,7 @@ namespace libtorrent
|
||||||
{
|
{
|
||||||
TORRENT_ASSERT(r.length <= 16 * 1024);
|
TORRENT_ASSERT(r.length <= 16 * 1024);
|
||||||
// the buffer needs to be allocated through the io_thread
|
// 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;
|
disk_io_job j;
|
||||||
j.storage = this;
|
j.storage = this;
|
||||||
|
@ -1238,7 +1238,7 @@ namespace libtorrent
|
||||||
j.piece = r.piece;
|
j.piece = r.piece;
|
||||||
j.offset = r.start;
|
j.offset = r.start;
|
||||||
j.buffer_size = r.length;
|
j.buffer_size = r.length;
|
||||||
j.buffer = buffer.buffer();
|
j.buffer = buffer.get();
|
||||||
m_io_thread.add_job(j, handler);
|
m_io_thread.add_job(j, handler);
|
||||||
buffer.release();
|
buffer.release();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue