2005-09-14 21:33:16 +02:00
|
|
|
/*
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2007-2016, Arvid Norberg
|
2005-09-14 21:33:16 +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 Rasterbar Software 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-06-20 17:32:06 +02:00
|
|
|
#ifndef TORRENT_BUFFER_HPP_INCLUDED
|
|
|
|
#define TORRENT_BUFFER_HPP_INCLUDED
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2007-09-23 02:51:45 +02:00
|
|
|
#include <cstring>
|
2015-01-20 18:05:45 +01:00
|
|
|
#include <limits> // for numeric_limits
|
2009-12-15 14:11:07 +01:00
|
|
|
#include <cstdlib> // malloc/free/realloc
|
2015-01-02 12:11:09 +01:00
|
|
|
#include <algorithm> // for std::swap
|
2016-06-18 20:01:38 +02:00
|
|
|
#include <cstdint>
|
2016-06-20 17:32:06 +02:00
|
|
|
|
|
|
|
#include "libtorrent/invariant_check.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
2016-07-22 18:31:42 +02:00
|
|
|
#include "libtorrent/span.hpp"
|
2016-06-27 02:41:03 +02:00
|
|
|
|
|
|
|
#if defined __GLIBC__
|
|
|
|
#include <malloc.h>
|
|
|
|
#elif defined _MSC_VER
|
|
|
|
#include <malloc.h>
|
|
|
|
#elif defined TORRENT_BSD
|
|
|
|
#include <malloc/malloc.h>
|
|
|
|
#endif
|
2016-05-23 14:15:39 +02:00
|
|
|
|
2005-09-14 21:33:16 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
namespace {
|
|
|
|
inline std::size_t round_up8(std::size_t const v)
|
|
|
|
{
|
|
|
|
return (v + 7) & (~std::size_t(0x7));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 00:54:39 +02:00
|
|
|
// TODO: eventually move this file to aux_ or create a new one
|
|
|
|
namespace aux
|
|
|
|
{
|
|
|
|
using mutable_buffer = span<char>;
|
|
|
|
using const_buffer = span<char const>;
|
|
|
|
}
|
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
// the buffer is allocated once and cannot be resized. The size() may be
|
|
|
|
// larger than requested, in case the underlying allocator over allocated. In
|
|
|
|
// order to "grow" an allocation, create a new buffer and initialize it by
|
|
|
|
// the range of bytes from the existing, and move-assign the new over the
|
|
|
|
// old.
|
2005-09-14 21:33:16 +02:00
|
|
|
class buffer
|
|
|
|
{
|
|
|
|
public:
|
2016-06-27 02:41:03 +02:00
|
|
|
|
2007-09-23 02:51:45 +02:00
|
|
|
struct interval
|
|
|
|
{
|
2015-05-17 17:23:39 +02:00
|
|
|
interval()
|
|
|
|
: begin(0)
|
|
|
|
, end(0)
|
2008-04-10 12:03:23 +02:00
|
|
|
{}
|
|
|
|
|
2015-05-17 17:23:39 +02:00
|
|
|
interval(char* b, char* e)
|
|
|
|
: begin(b)
|
|
|
|
, end(e)
|
2007-09-23 02:51:45 +02:00
|
|
|
{}
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2007-09-23 02:51:45 +02:00
|
|
|
char operator[](int index) const
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(begin + index < end);
|
2007-09-23 02:51:45 +02:00
|
|
|
return begin[index];
|
|
|
|
}
|
2015-05-17 17:23:39 +02:00
|
|
|
|
2014-11-29 02:53:22 +01:00
|
|
|
int left() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(end >= begin);
|
2016-05-17 15:24:06 +02:00
|
|
|
TORRENT_ASSERT(end - begin < (std::numeric_limits<int>::max)());
|
2014-11-29 02:53:22 +01:00
|
|
|
return int(end - begin);
|
|
|
|
}
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2007-09-23 02:51:45 +02:00
|
|
|
char* begin;
|
|
|
|
char* end;
|
|
|
|
};
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2007-09-23 02:51:45 +02:00
|
|
|
struct const_interval
|
2005-09-18 12:18:23 +02:00
|
|
|
{
|
2015-05-17 17:23:39 +02:00
|
|
|
const_interval(interval const& i)
|
|
|
|
: begin(i.begin)
|
|
|
|
, end(i.end)
|
2009-03-17 10:34:44 +01:00
|
|
|
{}
|
|
|
|
|
2015-05-17 17:23:39 +02:00
|
|
|
const_interval(char const* b, char const* e)
|
|
|
|
: begin(b)
|
|
|
|
, end(e)
|
2007-09-23 02:51:45 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
char operator[](int index) const
|
2005-09-14 21:33:16 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(begin + index < end);
|
2007-09-23 02:51:45 +02:00
|
|
|
return begin[index];
|
2005-09-14 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2007-09-23 02:51:45 +02:00
|
|
|
bool operator==(const const_interval& p_interval)
|
2005-09-14 21:33:16 +02:00
|
|
|
{
|
2014-11-29 02:53:22 +01:00
|
|
|
return begin == p_interval.begin
|
|
|
|
&& end == p_interval.end;
|
2005-09-14 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2014-11-29 02:53:22 +01:00
|
|
|
int left() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(end >= begin);
|
2016-05-17 15:24:06 +02:00
|
|
|
TORRENT_ASSERT(end - begin < (std::numeric_limits<int>::max)());
|
2014-11-29 02:53:22 +01:00
|
|
|
return int(end - begin);
|
|
|
|
}
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2007-09-23 02:51:45 +02:00
|
|
|
char const* begin;
|
|
|
|
char const* end;
|
|
|
|
};
|
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
// allocate an uninitialized buffer of the specified size
|
|
|
|
buffer(std::size_t size = 0)
|
2005-09-14 21:33:16 +02:00
|
|
|
{
|
2016-07-16 23:31:25 +02:00
|
|
|
TORRENT_ASSERT(size < std::size_t((std::numeric_limits<std::int32_t>::max)()));
|
2016-06-27 02:41:03 +02:00
|
|
|
|
|
|
|
if (size == 0) return;
|
|
|
|
|
|
|
|
size = round_up8(size);
|
|
|
|
|
|
|
|
m_begin = static_cast<char*>(std::malloc(size));
|
|
|
|
if (m_begin == nullptr)
|
|
|
|
{
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
throw std::bad_alloc();
|
|
|
|
#else
|
|
|
|
std::terminate();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// the actual allocation may be larger than we requested. If so, let the
|
|
|
|
// user take advantage of every single byte
|
|
|
|
#if defined __GLIBC__
|
|
|
|
m_size = malloc_usable_size(m_begin);
|
|
|
|
#elif defined _MSC_VER
|
|
|
|
m_size = _msize(m_begin);
|
|
|
|
#elif defined TORRENT_BSD
|
|
|
|
m_size = malloc_size(m_begin);
|
|
|
|
#else
|
|
|
|
m_size = size;
|
|
|
|
#endif
|
2005-09-14 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
// allocate an uninitialized buffer of the specified size
|
|
|
|
// and copy the initialization range into the start of the buffer
|
2016-07-22 18:31:42 +02:00
|
|
|
buffer(std::size_t const size, span<char const> initialize)
|
2016-06-27 02:41:03 +02:00
|
|
|
: buffer(size)
|
2005-09-18 12:18:23 +02:00
|
|
|
{
|
2016-06-27 02:41:03 +02:00
|
|
|
TORRENT_ASSERT(initialize.size() <= size);
|
|
|
|
if (initialize.size() > 0)
|
|
|
|
{
|
2016-07-16 23:31:25 +02:00
|
|
|
memcpy(m_begin, initialize.data(), (std::min)(initialize.size(), size));
|
2016-06-27 02:41:03 +02:00
|
|
|
}
|
2005-09-18 12:18:23 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
buffer(buffer const& b) = delete;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
buffer(buffer&& b)
|
|
|
|
: m_begin(b.m_begin)
|
|
|
|
, m_size(b.m_size)
|
|
|
|
{
|
2016-06-20 17:32:06 +02:00
|
|
|
b.m_begin = nullptr;
|
2016-06-27 02:41:03 +02:00
|
|
|
b.m_size = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2015-04-03 22:15:48 +02:00
|
|
|
|
|
|
|
buffer& operator=(buffer&& b)
|
|
|
|
{
|
|
|
|
if (&b == this) return *this;
|
|
|
|
std::free(m_begin);
|
|
|
|
m_begin = b.m_begin;
|
|
|
|
m_size = b.m_size;
|
2016-06-20 17:32:06 +02:00
|
|
|
b.m_begin = nullptr;
|
2016-06-27 02:41:03 +02:00
|
|
|
b.m_size = 0;
|
2015-04-03 22:15:48 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2013-12-17 01:51:01 +01:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
buffer& operator=(buffer const& b) = delete;
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
~buffer() { std::free(m_begin); }
|
|
|
|
|
|
|
|
// TODO: 3 fix the naming convention here
|
|
|
|
char* ptr() { return m_begin; }
|
|
|
|
char const* ptr() const { return m_begin; }
|
2005-09-18 12:18:23 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
buffer::interval data()
|
|
|
|
{ return interval(m_begin, m_begin + m_size); }
|
|
|
|
buffer::const_interval data() const
|
2016-06-27 02:41:03 +02:00
|
|
|
{ return interval(m_begin, m_begin + m_size); }
|
2005-09-18 12:18:23 +02:00
|
|
|
|
2016-07-22 18:31:42 +02:00
|
|
|
operator span<char>()
|
|
|
|
{ return span<char>(m_begin, int(m_size)); }
|
|
|
|
operator span<char const>() const
|
|
|
|
{ return span<char const>(m_begin, int(m_size)); }
|
2007-09-23 02:51:45 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::size_t size() const { return m_size; }
|
2005-09-18 12:18:23 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool empty() const { return m_size == 0; }
|
2007-10-07 18:42:31 +02:00
|
|
|
char& operator[](std::size_t i) { TORRENT_ASSERT(i < size()); return m_begin[i]; }
|
|
|
|
char const& operator[](std::size_t i) const { TORRENT_ASSERT(i < size()); return m_begin[i]; }
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2007-09-23 02:51:45 +02:00
|
|
|
char* begin() { return m_begin; }
|
|
|
|
char const* begin() const { return m_begin; }
|
2014-07-06 21:18:00 +02:00
|
|
|
char* end() { return m_begin + m_size; }
|
|
|
|
char const* end() const { return m_begin + m_size; }
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2007-09-23 02:51:45 +02:00
|
|
|
void swap(buffer& b)
|
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
swap(m_begin, b.m_begin);
|
2014-07-06 21:18:00 +02:00
|
|
|
swap(m_size, b.m_size);
|
2007-09-23 02:51:45 +02:00
|
|
|
}
|
|
|
|
private:
|
2016-06-27 02:41:03 +02:00
|
|
|
char* m_begin = nullptr;
|
|
|
|
// m_begin points to an allocation of this size.
|
|
|
|
std::size_t m_size = 0;
|
2007-09-23 02:51:45 +02:00
|
|
|
};
|
2005-09-14 21:33:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-20 17:32:06 +02:00
|
|
|
#endif // BTORRENT_BUFFER_HPP_INCLUDED
|