diff --git a/include/libtorrent/packet_buffer.hpp b/include/libtorrent/packet_buffer.hpp index 35f194d5a..bbfc9200a 100644 --- a/include/libtorrent/packet_buffer.hpp +++ b/include/libtorrent/packet_buffer.hpp @@ -101,6 +101,9 @@ namespace libtorrent private: void** m_storage; std::size_t m_capacity; + + // this is the total number of elements that are occupied + // in the array std::size_t m_size; // This defines the first index that is part of the m_storage. diff --git a/src/packet_buffer.cpp b/src/packet_buffer.cpp index 299869a4c..da7f91d91 100644 --- a/src/packet_buffer.cpp +++ b/src/packet_buffer.cpp @@ -123,10 +123,10 @@ namespace libtorrent { void* old_value = m_storage[idx & (m_capacity - 1)]; m_storage[idx & (m_capacity - 1)] = value; - if (m_size++ == 0) - { - m_first = idx; - } + if (m_size == 0) m_first = idx; + // if we're just replacing an old value, the number + // of elements in the buffer doesn't actually increase + if (old_value == 0) ++m_size; TORRENT_ASSERT_VAL(m_first <= 0xffff, m_first); return old_value;