cleanup packet_buffer class

This commit is contained in:
arvidn 2017-01-28 23:53:39 -05:00 committed by Arvid Norberg
parent fc70d70e33
commit 36858eae85
2 changed files with 20 additions and 42 deletions

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_PACKET_BUFFER_HPP_INCLUDED #define TORRENT_PACKET_BUFFER_HPP_INCLUDED
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/aux_/unique_ptr.hpp"
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
@ -71,45 +72,40 @@ namespace libtorrent
public: public:
typedef std::uint32_t index_type; typedef std::uint32_t index_type;
packet_buffer_impl();
~packet_buffer_impl();
void* insert(index_type idx, void* value); void* insert(index_type idx, void* value);
std::size_t size() const int size() const
{ return m_size; } { return m_size; }
std::size_t capacity() const int capacity() const
{ return m_capacity; } { return m_capacity; }
void* at(index_type idx) const; void* at(index_type idx) const;
void* remove(index_type idx); void* remove(index_type idx);
void reserve(std::size_t size); void reserve(int size);
index_type cursor() const index_type cursor() const { return m_first; }
{ return m_first; }
index_type span() const index_type span() const { return (m_last - m_first) & 0xffff; }
{ return (m_last - m_first) & 0xffff; }
#if TORRENT_USE_INVARIANT_CHECKS #if TORRENT_USE_INVARIANT_CHECKS
void check_invariant() const; void check_invariant() const;
#endif #endif
private: private:
void** m_storage; aux::unique_ptr<void*[], index_type> m_storage;
std::size_t m_capacity; int m_capacity = 0;
// this is the total number of elements that are occupied // this is the total number of elements that are occupied
// in the array // in the array
std::size_t m_size; int m_size = 0;
// This defines the first index that is part of the m_storage. // This defines the first index that is part of the m_storage.
// last is one passed the last used slot // last is one passed the last used slot
index_type m_first; index_type m_first{0};
index_type m_last; index_type m_last{0};
}; };
template <typename T> template <typename T>

View File

@ -41,14 +41,6 @@ namespace libtorrent {
bool compare_less_wrap(std::uint32_t lhs, std::uint32_t rhs bool compare_less_wrap(std::uint32_t lhs, std::uint32_t rhs
, std::uint32_t mask); , std::uint32_t mask);
packet_buffer_impl::packet_buffer_impl()
: m_storage(nullptr)
, m_capacity(0)
, m_size(0)
, m_first(0)
, m_last(0)
{}
#if TORRENT_USE_INVARIANT_CHECKS #if TORRENT_USE_INVARIANT_CHECKS
void packet_buffer_impl::check_invariant() const void packet_buffer_impl::check_invariant() const
{ {
@ -61,11 +53,6 @@ namespace libtorrent {
} }
#endif #endif
packet_buffer_impl::~packet_buffer_impl()
{
free(m_storage);
}
void* packet_buffer_impl::insert(index_type idx, void* value) void* packet_buffer_impl::insert(index_type idx, void* value)
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
@ -83,7 +70,7 @@ namespace libtorrent {
// Index comes before m_first. If we have room, we can simply // Index comes before m_first. If we have room, we can simply
// adjust m_first backward. // adjust m_first backward.
std::size_t free_space = 0; int free_space = 0;
for (index_type i = (m_first - 1) & (m_capacity - 1); for (index_type i = (m_first - 1) & (m_capacity - 1);
i != (m_first & (m_capacity - 1)); i = (i - 1) & (m_capacity - 1)) i != (m_first & (m_capacity - 1)); i = (i - 1) & (m_capacity - 1))
@ -93,7 +80,7 @@ namespace libtorrent {
++free_space; ++free_space;
} }
if (((m_first - idx) & 0xffff) > free_space) if (((m_first - idx) & 0xffff) > std::uint32_t(free_space))
reserve(((m_first - idx) & 0xffff) + m_capacity - free_space); reserve(((m_first - idx) & 0xffff) + m_capacity - free_space);
m_first = idx; m_first = idx;
@ -148,29 +135,24 @@ namespace libtorrent {
return m_storage[idx & mask]; return m_storage[idx & mask];
} }
void packet_buffer_impl::reserve(std::size_t size) void packet_buffer_impl::reserve(int size)
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
TORRENT_ASSERT_VAL(size <= 0xffff, size); TORRENT_ASSERT_VAL(size <= 0xffff, size);
std::size_t new_size = m_capacity == 0 ? 16 : m_capacity; int new_size = m_capacity == 0 ? 16 : m_capacity;
while (new_size < size) while (new_size < size)
new_size <<= 1; new_size <<= 1;
void** new_storage = static_cast<void**>(malloc(sizeof(void*) * new_size)); aux::unique_ptr<void*[], index_type> new_storage(new void*[new_size]);
#ifndef BOOST_NO_EXCEPTIONS
if (new_storage == nullptr) throw std::bad_alloc();
#endif
for (index_type i = 0; i < new_size; ++i) for (index_type i = 0; i < std::uint32_t(new_size); ++i)
new_storage[i] = nullptr; new_storage[i] = nullptr;
for (index_type i = m_first; i < (m_first + m_capacity); ++i) for (index_type i = m_first; i < (m_first + m_capacity); ++i)
new_storage[i & (new_size - 1)] = m_storage[i & (m_capacity - 1)]; new_storage[i & (new_size - 1)] = m_storage[i & (m_capacity - 1)];
free(m_storage); m_storage = std::move(new_storage);
m_storage = new_storage;
m_capacity = new_size; m_capacity = new_size;
} }
@ -197,7 +179,7 @@ namespace libtorrent {
if (idx == m_first && m_size != 0) if (idx == m_first && m_size != 0)
{ {
++m_first; ++m_first;
for (std::uint32_t i = 0; i < m_capacity; ++i, ++m_first) for (int i = 0; i < m_capacity; ++i, ++m_first)
if (m_storage[m_first & mask]) break; if (m_storage[m_first & mask]) break;
m_first &= 0xffff; m_first &= 0xffff;
} }
@ -205,7 +187,7 @@ namespace libtorrent {
if (((idx + 1) & 0xffff) == m_last && m_size != 0) if (((idx + 1) & 0xffff) == m_last && m_size != 0)
{ {
--m_last; --m_last;
for (std::uint32_t i = 0; i < m_capacity; ++i, --m_last) for (int i = 0; i < m_capacity; ++i, --m_last)
if (m_storage[m_last & mask]) break; if (m_storage[m_last & mask]) break;
++m_last; ++m_last;
m_last &= 0xffff; m_last &= 0xffff;