2011-08-14 01:01:38 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2007-2018, Arvid Norberg
|
2011-08-14 01:01:38 +02: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 "libtorrent/chained_buffer.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
|
2018-11-05 13:08:55 +01:00
|
|
|
#include <algorithm> // for copy
|
2016-08-26 06:14:11 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2011-08-14 01:01:38 +02:00
|
|
|
void chained_buffer::pop_front(int bytes_to_pop)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(!m_destructed);
|
2011-08-14 01:01:38 +02:00
|
|
|
TORRENT_ASSERT(bytes_to_pop <= m_bytes);
|
|
|
|
while (bytes_to_pop > 0 && !m_vec.empty())
|
|
|
|
{
|
|
|
|
buffer_t& b = m_vec.front();
|
|
|
|
if (b.used_size > bytes_to_pop)
|
|
|
|
{
|
2016-12-29 02:57:46 +01:00
|
|
|
b.buf += bytes_to_pop;
|
2011-08-14 01:01:38 +02:00
|
|
|
b.used_size -= bytes_to_pop;
|
2016-12-29 02:57:46 +01:00
|
|
|
b.size -= bytes_to_pop;
|
|
|
|
m_capacity -= bytes_to_pop;
|
2011-08-14 01:01:38 +02:00
|
|
|
m_bytes -= bytes_to_pop;
|
|
|
|
TORRENT_ASSERT(m_bytes <= m_capacity);
|
|
|
|
TORRENT_ASSERT(m_bytes >= 0);
|
|
|
|
TORRENT_ASSERT(m_capacity >= 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
b.destruct_holder(static_cast<void*>(&b.holder));
|
2011-08-14 01:01:38 +02:00
|
|
|
m_bytes -= b.used_size;
|
|
|
|
m_capacity -= b.size;
|
|
|
|
bytes_to_pop -= b.used_size;
|
|
|
|
TORRENT_ASSERT(m_bytes >= 0);
|
|
|
|
TORRENT_ASSERT(m_capacity >= 0);
|
|
|
|
TORRENT_ASSERT(m_bytes <= m_capacity);
|
|
|
|
m_vec.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the number of bytes available at the
|
|
|
|
// end of the last chained buffer.
|
|
|
|
int chained_buffer::space_in_last_buffer()
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(!m_destructed);
|
2011-08-14 01:01:38 +02:00
|
|
|
if (m_vec.empty()) return 0;
|
|
|
|
buffer_t& b = m_vec.back();
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(b.buf != nullptr);
|
2016-12-29 02:57:46 +01:00
|
|
|
return b.size - b.used_size;
|
2011-08-14 01:01:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// tries to copy the given buffer to the end of the
|
|
|
|
// last chained buffer. If there's not enough room
|
2017-04-23 06:37:29 +02:00
|
|
|
// it returns nullptr
|
|
|
|
char* chained_buffer::append(span<char const> buf)
|
2011-08-14 01:01:38 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(!m_destructed);
|
2017-04-23 06:37:29 +02:00
|
|
|
char* const insert = allocate_appendix(static_cast<int>(buf.size()));
|
2016-07-09 22:26:26 +02:00
|
|
|
if (insert == nullptr) return nullptr;
|
2018-11-05 13:08:55 +01:00
|
|
|
std::copy(buf.begin(), buf.end(), insert);
|
2011-08-14 01:01:38 +02:00
|
|
|
return insert;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tries to allocate memory from the end
|
|
|
|
// of the last buffer. If there isn't
|
|
|
|
// enough room, returns 0
|
2017-01-13 14:44:08 +01:00
|
|
|
char* chained_buffer::allocate_appendix(int const s)
|
2011-08-14 01:01:38 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(!m_destructed);
|
2016-07-09 22:26:26 +02:00
|
|
|
if (m_vec.empty()) return nullptr;
|
2011-08-14 01:01:38 +02:00
|
|
|
buffer_t& b = m_vec.back();
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(b.buf != nullptr);
|
2016-12-29 02:57:46 +01:00
|
|
|
char* const insert = b.buf + b.used_size;
|
2016-07-09 22:26:26 +02:00
|
|
|
if (insert + s > b.buf + b.size) return nullptr;
|
2011-08-14 01:01:38 +02:00
|
|
|
b.used_size += s;
|
|
|
|
m_bytes += s;
|
|
|
|
TORRENT_ASSERT(m_bytes <= m_capacity);
|
|
|
|
return insert;
|
|
|
|
}
|
|
|
|
|
2019-02-08 15:16:51 +01:00
|
|
|
span<boost::asio::const_buffer const> chained_buffer::build_iovec(int const to_send)
|
2011-08-14 01:01:38 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(!m_destructed);
|
2011-08-14 01:01:38 +02:00
|
|
|
m_tmp_vec.clear();
|
2014-11-23 07:14:47 +01:00
|
|
|
build_vec(to_send, m_tmp_vec);
|
|
|
|
return m_tmp_vec;
|
|
|
|
}
|
|
|
|
|
2016-07-28 04:31:08 +02:00
|
|
|
void chained_buffer::build_mutable_iovec(int bytes, std::vector<span<char>> &vec)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(!m_destructed);
|
2014-11-23 07:14:47 +01:00
|
|
|
build_vec(bytes, vec);
|
|
|
|
}
|
2011-08-14 01:01:38 +02:00
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
template <typename Buffer>
|
2016-12-29 02:47:18 +01:00
|
|
|
void chained_buffer::build_vec(int bytes, std::vector<Buffer>& vec)
|
2014-11-23 07:14:47 +01:00
|
|
|
{
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(!m_destructed);
|
|
|
|
for (auto i = m_vec.begin(), end(m_vec.end()); bytes > 0 && i != end; ++i)
|
2011-08-14 01:01:38 +02:00
|
|
|
{
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(i->buf != nullptr);
|
2014-11-23 07:14:47 +01:00
|
|
|
if (i->used_size > bytes)
|
2011-08-14 01:01:38 +02:00
|
|
|
{
|
2014-11-23 07:14:47 +01:00
|
|
|
TORRENT_ASSERT(bytes > 0);
|
2018-03-03 15:32:14 +01:00
|
|
|
vec.emplace_back(i->buf, std::size_t(bytes));
|
2011-08-14 01:01:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
TORRENT_ASSERT(i->used_size > 0);
|
2018-03-03 15:32:14 +01:00
|
|
|
vec.emplace_back(i->buf, std::size_t(i->used_size));
|
2014-11-23 07:14:47 +01:00
|
|
|
bytes -= i->used_size;
|
2011-08-14 01:01:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void chained_buffer::clear()
|
|
|
|
{
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(!m_destructed);
|
|
|
|
for (auto& b : m_vec)
|
|
|
|
b.destruct_holder(static_cast<void*>(&b.holder));
|
2014-07-06 21:18:00 +02:00
|
|
|
m_bytes = 0;
|
|
|
|
m_capacity = 0;
|
|
|
|
m_vec.clear();
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:01:38 +02:00
|
|
|
chained_buffer::~chained_buffer()
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!m_destructed);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2011-08-14 01:01:38 +02:00
|
|
|
TORRENT_ASSERT(m_bytes >= 0);
|
|
|
|
TORRENT_ASSERT(m_capacity >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
clear();
|
2016-12-29 02:47:18 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
m_destructed = true;
|
|
|
|
#endif
|
2011-08-14 01:01:38 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 01:40:31 +02:00
|
|
|
}
|