general cleanup of disk_buffer_holder and tailqueue (#1156)

general cleanup of disk_buffer_holder and tailqueue
This commit is contained in:
Arvid Norberg 2016-09-26 14:22:38 -04:00 committed by GitHub
parent 179e238561
commit 9cebd4569f
5 changed files with 50 additions and 46 deletions

View File

@ -66,10 +66,10 @@ namespace libtorrent
struct TORRENT_EXTRA_EXPORT disk_buffer_holder struct TORRENT_EXTRA_EXPORT disk_buffer_holder
{ {
// internal // internal
disk_buffer_holder(buffer_allocator_interface& alloc, char* buf); disk_buffer_holder(buffer_allocator_interface& alloc, char* buf) noexcept;
disk_buffer_holder& operator=(disk_buffer_holder&&); disk_buffer_holder& operator=(disk_buffer_holder&&) noexcept;
disk_buffer_holder(disk_buffer_holder&&); disk_buffer_holder(disk_buffer_holder&&) noexcept;
disk_buffer_holder& operator=(disk_buffer_holder const&) = delete; disk_buffer_holder& operator=(disk_buffer_holder const&) = delete;
disk_buffer_holder(disk_buffer_holder const&) = delete; disk_buffer_holder(disk_buffer_holder const&) = delete;
@ -77,7 +77,7 @@ namespace libtorrent
// construct a buffer holder that will free the held buffer // construct a buffer holder that will free the held buffer
// using a disk buffer pool directly (there's only one // using a disk buffer pool directly (there's only one
// disk_buffer_pool per session) // disk_buffer_pool per session)
disk_buffer_holder(buffer_allocator_interface& alloc, disk_io_job const& j); disk_buffer_holder(buffer_allocator_interface& alloc, disk_io_job const& j) noexcept;
// frees any unreleased disk buffer held by this object // frees any unreleased disk buffer held by this object
~disk_buffer_holder(); ~disk_buffer_holder();
@ -85,10 +85,10 @@ namespace libtorrent
// return the held disk buffer and clear it from the // return the held disk buffer and clear it from the
// holder. The responsibility to free it is passed on // holder. The responsibility to free it is passed on
// to the caller // to the caller
char* release(); char* release() noexcept;
// return a pointer to the held buffer // return a pointer to the held buffer
char* get() const { return m_buf; } char* get() const noexcept { return m_buf; }
// set the holder object to hold the specified buffer // set the holder object to hold the specified buffer
// (or nullptr by default). If it's already holding a // (or nullptr by default). If it's already holding a
@ -97,18 +97,18 @@ namespace libtorrent
void reset(disk_io_job const& j); void reset(disk_io_job const& j);
// swap pointers of two disk buffer holders. // swap pointers of two disk buffer holders.
void swap(disk_buffer_holder& h) void swap(disk_buffer_holder& h) noexcept
{ {
TORRENT_ASSERT(h.m_allocator == m_allocator); TORRENT_ASSERT(h.m_allocator == m_allocator);
std::swap(h.m_buf, m_buf); std::swap(h.m_buf, m_buf);
std::swap(h.m_ref, m_ref); std::swap(h.m_ref, m_ref);
} }
block_cache_reference ref() const { return m_ref; } block_cache_reference ref() const noexcept { return m_ref; }
// implicitly convertible to true if the object is currently holding a // implicitly convertible to true if the object is currently holding a
// buffer // buffer
explicit operator bool() const { return m_buf != nullptr; } explicit operator bool() const noexcept { return m_buf != nullptr; }
private: private:

View File

@ -40,7 +40,7 @@ namespace libtorrent
template <typename T> template <typename T>
struct tailqueue_node struct tailqueue_node
{ {
tailqueue_node() : next(0) {} tailqueue_node() : next(nullptr) {}
T* next; T* next;
}; };
@ -81,12 +81,12 @@ namespace libtorrent
void append(tailqueue<T>& rhs) void append(tailqueue<T>& rhs)
{ {
TORRENT_ASSERT(m_last == 0 || m_last->next == 0); TORRENT_ASSERT(m_last == nullptr || m_last->next == nullptr);
TORRENT_ASSERT(rhs.m_last == 0 || rhs.m_last->next == 0); TORRENT_ASSERT(rhs.m_last == nullptr || rhs.m_last->next == nullptr);
if (rhs.m_first == 0) return; if (rhs.m_first == nullptr) return;
if (m_first == 0) if (m_first == nullptr)
{ {
swap(rhs); swap(rhs);
return; return;
@ -95,21 +95,23 @@ namespace libtorrent
m_last->next = rhs.m_first; m_last->next = rhs.m_first;
m_last = rhs.m_last; m_last = rhs.m_last;
m_size += rhs.m_size; m_size += rhs.m_size;
rhs.m_first = 0; rhs.m_first = nullptr;
rhs.m_last = 0; rhs.m_last = nullptr;
rhs.m_size = 0; rhs.m_size = 0;
TORRENT_ASSERT(m_last == 0 || m_last->next == 0); TORRENT_ASSERT(m_last == nullptr || m_last->next == nullptr);
} }
void prepend(tailqueue<T>& rhs) void prepend(tailqueue<T>& rhs)
{ {
TORRENT_ASSERT(m_last == 0 || m_last->next == 0); TORRENT_ASSERT(m_last == nullptr || m_last->next == nullptr);
TORRENT_ASSERT(rhs.m_last == 0 || rhs.m_last->next == 0); TORRENT_ASSERT(rhs.m_last == nullptr || rhs.m_last->next == nullptr);
TORRENT_ASSERT((m_last == nullptr) == (m_first == nullptr));
TORRENT_ASSERT((rhs.m_last == nullptr) == (rhs.m_first == nullptr));
if (rhs.m_first == 0) return; if (rhs.m_first == nullptr) return;
if (m_first == 0) if (m_first == nullptr)
{ {
swap(rhs); swap(rhs);
return; return;
@ -117,22 +119,24 @@ namespace libtorrent
swap(rhs); swap(rhs);
append(rhs); append(rhs);
TORRENT_ASSERT(m_last == nullptr || m_last->next == nullptr);
TORRENT_ASSERT(rhs.m_last == nullptr || rhs.m_last->next == nullptr);
} }
T* pop_front() T* pop_front()
{ {
TORRENT_ASSERT(m_last == 0 || m_last->next == 0); TORRENT_ASSERT(m_last == nullptr || m_last->next == nullptr);
T* e = m_first; T* e = m_first;
m_first = m_first->next; m_first = m_first->next;
if (e == m_last) m_last = 0; if (e == m_last) m_last = nullptr;
e->next = 0; e->next = nullptr;
--m_size; --m_size;
return e; return e;
} }
void push_front(T* e) void push_front(T* e)
{ {
TORRENT_ASSERT(e->next == 0); TORRENT_ASSERT(e->next == nullptr);
TORRENT_ASSERT(m_last == 0 || m_last->next == 0); TORRENT_ASSERT(m_last == nullptr || m_last->next == nullptr);
e->next = m_first; e->next = m_first;
m_first = e; m_first = e;
if (!m_last) m_last = e; if (!m_last) m_last = e;
@ -140,20 +144,20 @@ namespace libtorrent
} }
void push_back(T* e) void push_back(T* e)
{ {
TORRENT_ASSERT(e->next == 0); TORRENT_ASSERT(e->next == nullptr);
TORRENT_ASSERT(m_last == 0 || m_last->next == 0); TORRENT_ASSERT(m_last == nullptr || m_last->next == nullptr);
if (m_last) m_last->next = e; if (m_last) m_last->next = e;
else m_first = e; else m_first = e;
m_last = e; m_last = e;
e->next = 0; e->next = nullptr;
++m_size; ++m_size;
} }
T* get_all() T* get_all()
{ {
TORRENT_ASSERT(m_last == 0 || m_last->next == 0); TORRENT_ASSERT(m_last == nullptr || m_last->next == nullptr);
T* e = m_first; T* e = m_first;
m_first = 0; m_first = nullptr;
m_last = 0; m_last = nullptr;
m_size = 0; m_size = 0;
return e; return e;
} }

View File

@ -84,7 +84,7 @@ TORRENT_TEST(cache_after_checking)
p.set_int(lt::settings_pack::cache_size, 100); p.set_int(lt::settings_pack::cache_size, 100);
}, },
[](lt::session& ses) { [](lt::session& ses) {
int cache = get_cache_size(ses); int const cache = get_cache_size(ses);
TEST_CHECK(cache > 0); TEST_CHECK(cache > 0);
std::vector<lt::torrent_handle> tor = ses.get_torrents(); std::vector<lt::torrent_handle> tor = ses.get_torrents();
@ -102,7 +102,7 @@ TORRENT_TEST(checking_no_cache)
p.set_int(lt::settings_pack::cache_size, 0); p.set_int(lt::settings_pack::cache_size, 0);
}, },
[](lt::session& ses) { [](lt::session& ses) {
int cache = get_cache_size(ses); int const cache = get_cache_size(ses);
TEST_EQUAL(cache, 0); TEST_EQUAL(cache, 0);
std::vector<lt::torrent_handle> tor = ses.get_torrents(); std::vector<lt::torrent_handle> tor = ses.get_torrents();
@ -121,7 +121,7 @@ TORRENT_TEST(checking_limit_volatile)
p.set_int(lt::settings_pack::cache_size_volatile, 2); p.set_int(lt::settings_pack::cache_size_volatile, 2);
}, },
[](lt::session& ses) { [](lt::session& ses) {
int cache = get_cache_size(ses); int const cache = get_cache_size(ses);
// the cache fits 300 blocks, but only allows two volatile blocks // the cache fits 300 blocks, but only allows two volatile blocks
TEST_EQUAL(cache, 2); TEST_EQUAL(cache, 2);
@ -141,7 +141,7 @@ TORRENT_TEST(checking_volatile_limit_cache_size)
p.set_int(lt::settings_pack::cache_size_volatile, 300); p.set_int(lt::settings_pack::cache_size_volatile, 300);
}, },
[](lt::session& ses) { [](lt::session& ses) {
int cache = get_cache_size(ses); int const cache = get_cache_size(ses);
// the cache allows 300 volatile blocks, but only fits 2 blocks // the cache allows 300 volatile blocks, but only fits 2 blocks
TEST_CHECK(cache > 0); TEST_CHECK(cache > 0);
TEST_CHECK(cache <= 10); TEST_CHECK(cache <= 10);

View File

@ -36,7 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent namespace libtorrent
{ {
disk_buffer_holder::disk_buffer_holder(buffer_allocator_interface& alloc, char* buf) disk_buffer_holder::disk_buffer_holder(buffer_allocator_interface& alloc, char* buf) noexcept
: m_allocator(&alloc), m_buf(buf) : m_allocator(&alloc), m_buf(buf)
{ {
m_ref.storage = nullptr; m_ref.storage = nullptr;
@ -44,20 +44,20 @@ namespace libtorrent
m_ref.block = -1; m_ref.block = -1;
} }
disk_buffer_holder& disk_buffer_holder::operator=(disk_buffer_holder&& h) disk_buffer_holder& disk_buffer_holder::operator=(disk_buffer_holder&& h) noexcept
{ {
disk_buffer_holder(std::move(h)).swap(*this); disk_buffer_holder(std::move(h)).swap(*this);
return *this; return *this;
} }
disk_buffer_holder::disk_buffer_holder(disk_buffer_holder&& h) disk_buffer_holder::disk_buffer_holder(disk_buffer_holder&& h) noexcept
: m_allocator(h.m_allocator), m_buf(h.m_buf), m_ref(h.m_ref) : m_allocator(h.m_allocator), m_buf(h.m_buf), m_ref(h.m_ref)
{ {
// we own this buffer now // we own this buffer now
h.release(); h.release();
} }
disk_buffer_holder::disk_buffer_holder(buffer_allocator_interface& alloc, disk_io_job const& j) disk_buffer_holder::disk_buffer_holder(buffer_allocator_interface& alloc, disk_io_job const& j) noexcept
: m_allocator(&alloc), m_buf(j.buffer.disk_block), m_ref(j.d.io.ref) : m_allocator(&alloc), m_buf(j.buffer.disk_block), m_ref(j.d.io.ref)
{ {
TORRENT_ASSERT(m_ref.storage == nullptr || m_ref.piece >= 0); TORRENT_ASSERT(m_ref.storage == nullptr || m_ref.piece >= 0);
@ -94,7 +94,7 @@ namespace libtorrent
m_ref.storage = nullptr; m_ref.storage = nullptr;
} }
char* disk_buffer_holder::release() char* disk_buffer_holder::release() noexcept
{ {
char* ret = m_buf; char* ret = m_buf;
m_buf = nullptr; m_buf = nullptr;

View File

@ -2748,11 +2748,11 @@ namespace libtorrent
// if the block we got is already finished, then ignore it // if the block we got is already finished, then ignore it
if (picker.is_downloaded(block_finished)) if (picker.is_downloaded(block_finished))
{ {
waste_reason reason; waste_reason const reason
if (b->timed_out) reason = waste_reason::piece_timed_out; = (b->timed_out) ? waste_reason::piece_timed_out
else if (b->not_wanted) reason = waste_reason::piece_cancelled; : (b->not_wanted) ? waste_reason::piece_cancelled
else if (b->busy) reason = waste_reason::piece_end_game; : (b->busy) ? waste_reason::piece_end_game
else reason = waste_reason::piece_unknown; : waste_reason::piece_unknown;
t->add_redundant_bytes(p.length, reason); t->add_redundant_bytes(p.length, reason);