premiere-libtorrent/src/packet_buffer.cpp

220 lines
5.7 KiB
C++
Raw Normal View History

2010-11-29 02:33:05 +01:00
/*
Copyright (c) 2010-2016, Arvid Norberg
2010-11-29 02:33:05 +01:00
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h> // free and calloc
2015-05-18 01:32:13 +02:00
#include <new> // for bad_alloc
2010-11-29 02:33:05 +01:00
#include "libtorrent/packet_buffer.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/invariant_check.hpp"
2010-11-29 02:33:05 +01:00
namespace libtorrent {
bool compare_less_wrap(std::uint32_t lhs, std::uint32_t rhs
, std::uint32_t mask);
2010-11-29 02:33:05 +01:00
2015-08-17 15:01:43 +02:00
packet_buffer_impl::packet_buffer_impl()
2016-07-09 22:26:26 +02:00
: m_storage(nullptr)
2010-11-29 02:33:05 +01:00
, m_capacity(0)
, m_size(0)
, m_first(0)
, m_last(0)
{}
2014-01-21 20:26:09 +01:00
#if TORRENT_USE_INVARIANT_CHECKS
2015-08-17 15:01:43 +02:00
void packet_buffer_impl::check_invariant() const
{
int count = 0;
for (int i = 0; i < int(m_capacity); ++i)
{
count += m_storage[i] ? 1 : 0;
}
TORRENT_ASSERT(count == int(m_size));
}
#endif
2015-08-17 15:01:43 +02:00
packet_buffer_impl::~packet_buffer_impl()
2010-11-29 02:33:05 +01:00
{
free(m_storage);
}
2015-08-17 15:01:43 +02:00
void* packet_buffer_impl::insert(index_type idx, void* value)
2010-11-29 02:33:05 +01:00
{
INVARIANT_CHECK;
2010-11-29 02:33:05 +01:00
TORRENT_ASSERT_VAL(idx <= 0xffff, idx);
// you're not allowed to insert NULLs!
TORRENT_ASSERT(value);
2016-07-09 22:26:26 +02:00
if (value == nullptr) return remove(idx);
2010-11-29 02:33:05 +01:00
if (m_size != 0)
{
if (compare_less_wrap(idx, m_first, 0xffff))
{
// Index comes before m_first. If we have room, we can simply
// adjust m_first backward.
std::size_t free_space = 0;
for (index_type i = (m_first - 1) & (m_capacity - 1);
i != (m_first & (m_capacity - 1)); i = (i - 1) & (m_capacity - 1))
{
if (m_storage[i & (m_capacity - 1)])
break;
++free_space;
}
if (((m_first - idx) & 0xffff) > free_space)
reserve(((m_first - idx) & 0xffff) + m_capacity - free_space);
m_first = idx;
}
else if (idx >= m_first + m_capacity)
{
reserve(idx - m_first + 1);
}
else if (idx < m_first)
{
// We have wrapped.
2012-11-14 05:53:29 +01:00
if (idx >= ((m_first + m_capacity) & 0xffff) && m_capacity < 0xffff)
2010-11-29 02:33:05 +01:00
{
2012-11-25 08:03:37 +01:00
reserve(m_capacity + (idx + 1 - ((m_first + m_capacity) & 0xffff)));
2010-11-29 02:33:05 +01:00
}
}
if (compare_less_wrap(m_last, (idx + 1) & 0xffff, 0xffff))
m_last = (idx + 1) & 0xffff;
}
else
{
m_first = idx;
m_last = (idx + 1) & 0xffff;
}
if (m_capacity == 0) reserve(16);
void* old_value = m_storage[idx & (m_capacity - 1)];
m_storage[idx & (m_capacity - 1)] = value;
2012-03-24 17:08:49 +01:00
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
2016-07-09 22:26:26 +02:00
if (old_value == nullptr) ++m_size;
2010-11-29 02:33:05 +01:00
TORRENT_ASSERT_VAL(m_first <= 0xffff, m_first);
return old_value;
}
2015-08-17 15:01:43 +02:00
void* packet_buffer_impl::at(index_type idx) const
2010-11-29 02:33:05 +01:00
{
INVARIANT_CHECK;
2010-11-29 02:33:05 +01:00
if (idx >= m_first + m_capacity)
2016-07-09 22:26:26 +02:00
return nullptr;
2010-11-29 02:33:05 +01:00
if (compare_less_wrap(idx, m_first, 0xffff))
{
2016-07-09 22:26:26 +02:00
return nullptr;
2010-11-29 02:33:05 +01:00
}
const int mask = int(m_capacity - 1);
return m_storage[idx & mask];
2010-11-29 02:33:05 +01:00
}
2015-08-17 15:01:43 +02:00
void packet_buffer_impl::reserve(std::size_t size)
2010-11-29 02:33:05 +01:00
{
INVARIANT_CHECK;
2010-11-29 02:33:05 +01:00
TORRENT_ASSERT_VAL(size <= 0xffff, size);
std::size_t new_size = m_capacity == 0 ? 16 : m_capacity;
while (new_size < size)
new_size <<= 1;
2015-08-09 04:53:11 +02:00
void** new_storage = static_cast<void**>(malloc(sizeof(void*) * new_size));
#ifndef BOOST_NO_EXCEPTIONS
if (new_storage == nullptr) throw std::bad_alloc();
#endif
2010-11-29 02:33:05 +01:00
for (index_type i = 0; i < new_size; ++i)
2016-07-09 22:26:26 +02:00
new_storage[i] = nullptr;
2010-11-29 02:33:05 +01:00
for (index_type i = m_first; i < (m_first + m_capacity); ++i)
new_storage[i & (new_size - 1)] = m_storage[i & (m_capacity - 1)];
free(m_storage);
m_storage = new_storage;
m_capacity = new_size;
}
2015-08-17 15:01:43 +02:00
void* packet_buffer_impl::remove(index_type idx)
2010-11-29 02:33:05 +01:00
{
INVARIANT_CHECK;
2010-11-29 02:33:05 +01:00
// TODO: use compare_less_wrap for this comparison as well
if (idx >= m_first + m_capacity)
2016-07-09 22:26:26 +02:00
return nullptr;
2010-11-29 02:33:05 +01:00
if (compare_less_wrap(idx, m_first, 0xffff))
2016-07-09 22:26:26 +02:00
return nullptr;
2010-11-29 02:33:05 +01:00
const int mask = int(m_capacity - 1);
void* old_value = m_storage[idx & mask];
2016-07-09 22:26:26 +02:00
m_storage[idx & mask] = nullptr;
2010-11-29 02:33:05 +01:00
if (old_value)
{
--m_size;
if (m_size == 0) m_last = m_first;
}
if (idx == m_first && m_size != 0)
{
2011-02-06 10:34:47 +01:00
++m_first;
for (std::uint32_t i = 0; i < m_capacity; ++i, ++m_first)
2011-02-06 10:34:47 +01:00
if (m_storage[m_first & mask]) break;
2010-11-29 02:33:05 +01:00
m_first &= 0xffff;
}
if (((idx + 1) & 0xffff) == m_last && m_size != 0)
{
2011-02-06 10:34:47 +01:00
--m_last;
for (std::uint32_t i = 0; i < m_capacity; ++i, --m_last)
2011-02-06 10:34:47 +01:00
if (m_storage[m_last & mask]) break;
2010-11-29 02:33:05 +01:00
++m_last;
m_last &= 0xffff;
}
TORRENT_ASSERT_VAL(m_first <= 0xffff, m_first);
return old_value;
}
}