flat_buffer improvements:

fix #1345

* Revise documentation
* Add reserve() member
* Add max_size() member
* Respect Allocator max_size
* Specify exception safety
This commit is contained in:
Vinnie Falco
2018-12-07 13:00:51 -08:00
parent 5a42490921
commit 11c71d118f
4 changed files with 339 additions and 154 deletions

View File

@ -1,3 +1,9 @@
Version 198:
* flat_buffer improvements
--------------------------------------------------------------------------------
Version 197: Version 197:
* Improvements to echo-op example * Improvements to echo-op example

View File

@ -16,6 +16,7 @@
#include <boost/core/empty_value.hpp> #include <boost/core/empty_value.hpp>
#include <limits> #include <limits>
#include <memory> #include <memory>
#include <type_traits>
namespace boost { namespace boost {
namespace beast { namespace beast {
@ -61,11 +62,6 @@ class basic_flat_buffer
template rebind_alloc<char>> template rebind_alloc<char>>
#endif #endif
{ {
enum
{
min_size = 512
};
template<class OtherAlloc> template<class OtherAlloc>
friend class basic_flat_buffer; friend class basic_flat_buffer;
@ -73,9 +69,18 @@ class basic_flat_buffer
detail::allocator_traits<Allocator>:: detail::allocator_traits<Allocator>::
template rebind_alloc<char>; template rebind_alloc<char>;
static bool constexpr default_nothrow =
std::is_nothrow_default_constructible<Allocator>::value;
using alloc_traits = using alloc_traits =
detail::allocator_traits<base_alloc_type>; detail::allocator_traits<base_alloc_type>;
using pocma = typename
alloc_traits::propagate_on_container_move_assignment;
using pocca = typename
alloc_traits::propagate_on_container_copy_assignment;
static static
std::size_t std::size_t
dist(char const* first, char const* last) dist(char const* first, char const* last)
@ -99,157 +104,209 @@ public:
/** Constructor /** Constructor
Upon construction, @ref capacity will return zero. After construction, @ref capacity will return zero, and
@ref max_size will return the largest value which may
be passed to the allocator's `allocate` function.
*/ */
basic_flat_buffer(); basic_flat_buffer() noexcept(default_nothrow);
/** Constructor /** Constructor
Upon construction, @ref capacity will return zero. After construction, @ref capacity will return zero, and
@ref max_size will return the specified value of `limit`.
@param limit The setting for @ref max_size. @param limit The desired maximum size.
*/ */
explicit explicit
basic_flat_buffer(std::size_t limit); basic_flat_buffer(
std::size_t limit) noexcept(default_nothrow);
/** Constructor /** Constructor
Upon construction, @ref capacity will return zero. After construction, @ref capacity will return zero, and
@ref max_size will return the largest value which may
be passed to the allocator's `allocate` function.
@param alloc The allocator to construct with. @param alloc The allocator to use for the object.
@par Exception Safety
No-throw guarantee.
*/ */
explicit explicit
basic_flat_buffer(Allocator const& alloc); basic_flat_buffer(Allocator const& alloc) noexcept;
/** Constructor /** Constructor
Upon construction, @ref capacity will return zero. After construction, @ref capacity will return zero, and
@ref max_size will return the specified value of `limit`.
@param limit The setting for @ref max_size. @param limit The desired maximum size.
@param alloc The allocator to use. @param alloc The allocator to use for the object.
@par Exception Safety
No-throw guarantee.
*/ */
basic_flat_buffer( basic_flat_buffer(
std::size_t limit, Allocator const& alloc); std::size_t limit,
Allocator const& alloc) noexcept;
/** Move Constructor /** Move Constructor
Constructs the container with the contents of other The container is constructed with the contents of `other`
using move semantics. After the move, other is using move semantics. The maximum size will be the same
guaranteed to be empty. as the moved-from object.
Buffer sequences previously obtained using @ref data Buffer sequences previously obtained from `other` using
or @ref prepare are not invalidated after the move. @ref data or @ref prepare remain valid after the move.
@param other The object to move from. After the move, @param other The object to move from. After the move, the
the moved-from object's state will be as if default moved-from object will have zero capacity, zero readable
constructed using its current allocator and limit. bytes, and zero writable bytes.
@par Exception Safety
No-throw guarantee.
*/ */
basic_flat_buffer(basic_flat_buffer&& other); basic_flat_buffer(basic_flat_buffer&& other) noexcept;
/** Move Constructor /** Move Constructor
Using alloc as the allocator for the new container, the Using `alloc` as the allocator for the new container, the
contents of other are moved. If `alloc != other.get_allocator()`, contents of `other` are moved. If `alloc != other.get_allocator()`,
this results in a copy. After the move, other is this results in a copy. The maximum size will be the same
guaranteed to be empty. as the moved-from object.
All buffers sequences previously obtained using Buffer sequences previously obtained from `other` using
@ref data or @ref prepare are invalidated. @ref data or @ref prepare become invalid after the move.
@param other The object to move from. After the move, @param other The object to move from. After the move,
the moved-from object's state will be as if default the moved-from object will have zero capacity, zero readable
constructed using its current allocator and limit. bytes, and zero writable bytes.
@param alloc The allocator to use for the newly @param alloc The allocator to use for the object.
constructed object.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of `alloc`.
*/ */
basic_flat_buffer( basic_flat_buffer(
basic_flat_buffer&& other, Allocator const& alloc); basic_flat_buffer&& other,
Allocator const& alloc);
/** Copy Constructor /** Copy Constructor
The newly constructed object will have a copy of the This container is constructed with the contents of `other`
allocator and contents of other, and zero writable bytes. using copy semantics. The maximum size will be the same
as the copied object.
@param other The object to copy from. @param other The object to copy from.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of the allocator.
*/ */
basic_flat_buffer(basic_flat_buffer const& other); basic_flat_buffer(basic_flat_buffer const& other);
/** Copy Constructor /** Copy Constructor
The newly constructed object will have a copy of the This container is constructed with the contents of `other`
specified allocator, a copy of the contents of other, using copy semantics and the specified allocator. The maximum
and zero writable bytes. size will be the same as the copied object.
@param other The object to copy from. @param other The object to copy from.
@param alloc The allocator to use. @param alloc The allocator to use for the object.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of `alloc`.
*/ */
basic_flat_buffer(basic_flat_buffer const& other, basic_flat_buffer(
basic_flat_buffer const& other,
Allocator const& alloc); Allocator const& alloc);
/** Copy Constructor /** Copy Constructor
The newly constructed object will have a copy of the This container is constructed with the contents of `other`
contents of other, and zero writable bytes. using copy semantics. The maximum size will be the same
as the copied object.
@param other The object to copy from. @param other The object to copy from.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of the allocator.
*/ */
template<class OtherAlloc> template<class OtherAlloc>
basic_flat_buffer( basic_flat_buffer(
basic_flat_buffer<OtherAlloc> const& other); basic_flat_buffer<OtherAlloc> const& other)
noexcept(default_nothrow);;
/** Copy Constructor /** Copy Constructor
The newly constructed object will have a copy of the This container is constructed with the contents of `other`
specified allocator, a copy of the contents of other, using copy semantics. The maximum size will be the same
and zero writable bytes. as the copied object.
@param other The object to copy from. @param other The object to copy from.
@param alloc The allocator to use. @param alloc The allocator to use for the object.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of `alloc`.
*/ */
template<class OtherAlloc> template<class OtherAlloc>
basic_flat_buffer( basic_flat_buffer(
basic_flat_buffer<OtherAlloc> const& other, basic_flat_buffer<OtherAlloc> const& other,
Allocator const& alloc); Allocator const& alloc);
/** Move Assignment /** Move Assignment
Assigns the container with the contents of other The container is assigned with the contents of `other`
using move semantics. After the move, other is using move semantics. The maximum size will be the same
guaranteed to be empty. The previous contents of as the moved-from object.
this container are deleted.
Buffer sequences previously obtained using @ref data Buffer sequences previously obtained from `other` using
or @ref prepare are not invalidated after the move. @ref data or @ref prepare remain valid after the move.
@param other The object to move from. After the move, @param other The object to move from. After the move,
the moved-from object's state will be as if default the moved-from object will have zero capacity, zero readable
constructed using its current allocator and limit. bytes, and zero writable bytes.
@par Exception Safety
No-throw guarantee.
*/ */
basic_flat_buffer& basic_flat_buffer&
operator=(basic_flat_buffer&& other); operator=(basic_flat_buffer&& other) noexcept;
/** Copy Assignment /** Copy Assignment
The assigned object will have a copy of the allocator The container is assigned with the contents of `other`
and contents of other, and zero writable bytes. The using copy semantics. The maximum size will be the same
previous contents of this container are deleted. as the copied object.
After the copy, `this` will have zero writable bytes.
@param other The object to copy from. @param other The object to copy from.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of the allocator.
*/ */
basic_flat_buffer& basic_flat_buffer&
operator=(basic_flat_buffer const& other); operator=(basic_flat_buffer const& other);
/** Copy assignment /** Copy assignment
The assigned object will have a copy of the contents The container is assigned with the contents of `other`
of other, and zero writable bytes. The previous contents using copy semantics. The maximum size will be the same
of this container are deleted. as the copied object.
After the copy, `this` will have zero writable bytes.
@param other The object to copy from. @param other The object to copy from.
@throws std::length_error if `other.size()` exceeds the
maximum allocation size of the allocator.
*/ */
template<class OtherAlloc> template<class OtherAlloc>
basic_flat_buffer& basic_flat_buffer&
@ -262,10 +319,53 @@ public:
return this->get(); return this->get();
} }
/** Set the maximum allowed capacity
This function changes the currently configured upper limit
on capacity to the specified value.
@param n The maximum number of bytes ever allowed for capacity.
@par Exception Safety
No-throw guarantee.
*/
void
max_size(std::size_t n) noexcept
{
max_ = n;
}
/** Guarantee a minimum capacity
This function adjusts the internal storage (if necessary)
to guarantee space for at least `n` bytes.
Buffer sequences previously obtained using @ref data or
@ref prepare become invalid.
@param n The minimum number of byte for the new capacity.
If this value is greater than the maximum size, then the
maximum size will be adjusted upwards to this value.
@par Exception Safety
Basic guarantee.
@throws std::length_error if n is larger than the maximum
allocation size of the allocator.
*/
void
reserve(std::size_t n);
/** Reallocate the buffer to fit the readable bytes exactly. /** Reallocate the buffer to fit the readable bytes exactly.
All buffers sequences previously obtained using Buffer sequences previously obtained using @ref data or
@ref data or @ref prepare are invalidated. @ref prepare become invalid.
@par Exception Safety
Strong guarantee.
*/ */
void void
shrink_to_fit(); shrink_to_fit();
@ -338,12 +438,17 @@ public:
reallocated as needed. reallocated as needed.
All buffers sequences previously obtained using All buffers sequences previously obtained using
@ref data or @ref prepare are invalidated. @ref data or @ref prepare become invalid.
@param n The desired number of bytes in the returned buffer @param n The desired number of bytes in the returned buffer
sequence. sequence.
@throws std::length_error if `size() + n` exceeds `max_size()`. @throws std::length_error if `size() + n` exceeds either
`max_size()` or the allocator's maximum allocation size.
@par Exception Safety
Strong guarantee.
*/ */
mutable_buffers_type mutable_buffers_type
prepare(std::size_t n); prepare(std::size_t n);
@ -356,11 +461,15 @@ public:
bytes, all writable bytes are appended to the readable bytes. bytes, all writable bytes are appended to the readable bytes.
All buffers sequences previously obtained using All buffers sequences previously obtained using
@ref data or @ref prepare are invalidated. @ref data or @ref prepare become invalid.
@param n The number of bytes to append. If this number @param n The number of bytes to append. If this number
is greater than the number of writable bytes, all is greater than the number of writable bytes, all
writable bytes are appended. writable bytes are appended.
@par Exception Safety
No-throw guarantee.
*/ */
void void
commit(std::size_t n) noexcept commit(std::size_t n) noexcept
@ -373,21 +482,22 @@ public:
Removes n bytes from the beginning of the readable bytes. Removes n bytes from the beginning of the readable bytes.
All buffers sequences previously obtained using All buffers sequences previously obtained using
@ref data or @ref prepare are invalidated. @ref data or @ref prepare become invalid.
@param n The number of bytes to remove. If this number @param n The number of bytes to remove. If this number
is greater than the number of readable bytes, all is greater than the number of readable bytes, all
readable bytes are removed. readable bytes are removed.
@par Exception Safety
No-throw guarantee.
*/ */
void void
consume(std::size_t n) noexcept; consume(std::size_t n) noexcept;
private: private:
void template<class OtherAlloc>
reset(); void copy_from(basic_flat_buffer<OtherAlloc> const& other);
template<class DynamicBuffer>
void copy_from(DynamicBuffer const& other);
void move_assign(basic_flat_buffer&, std::true_type); void move_assign(basic_flat_buffer&, std::true_type);
void move_assign(basic_flat_buffer&, std::false_type); void move_assign(basic_flat_buffer&, std::false_type);
void copy_assign(basic_flat_buffer const&, std::true_type); void copy_assign(basic_flat_buffer const&, std::true_type);
@ -395,6 +505,8 @@ private:
void swap(basic_flat_buffer&); void swap(basic_flat_buffer&);
void swap(basic_flat_buffer&, std::true_type); void swap(basic_flat_buffer&, std::true_type);
void swap(basic_flat_buffer&, std::false_type); void swap(basic_flat_buffer&, std::false_type);
char* alloc(std::size_t n);
void clear();
}; };
/// A flat buffer which uses the default allocator. /// A flat buffer which uses the default allocator.

View File

@ -13,6 +13,7 @@
#include <boost/core/exchange.hpp> #include <boost/core/exchange.hpp>
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include <memory>
#include <stdexcept> #include <stdexcept>
namespace boost { namespace boost {
@ -31,53 +32,55 @@ basic_flat_buffer<Allocator>::
{ {
if(begin_) if(begin_)
alloc_traits::deallocate( alloc_traits::deallocate(
this->get(), begin_, dist(begin_, end_)); this->get(), begin_, capacity());
} }
template<class Allocator> template<class Allocator>
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
basic_flat_buffer() basic_flat_buffer() noexcept(default_nothrow)
: begin_(nullptr) : begin_(nullptr)
, in_(nullptr) , in_(nullptr)
, out_(nullptr) , out_(nullptr)
, last_(nullptr) , last_(nullptr)
, end_(nullptr) , end_(nullptr)
, max_((std::numeric_limits< , max_(alloc_traits::max_size(
std::size_t>::max)()) this->get()))
{
}
template<class Allocator>
basic_flat_buffer<Allocator>::
basic_flat_buffer(std::size_t limit)
: begin_(nullptr)
, in_(nullptr)
, out_(nullptr)
, last_(nullptr)
, end_(nullptr)
, max_(limit)
{
}
template<class Allocator>
basic_flat_buffer<Allocator>::
basic_flat_buffer(Allocator const& alloc)
: boost::empty_value<base_alloc_type>(
boost::empty_init_t(), alloc)
, begin_(nullptr)
, in_(nullptr)
, out_(nullptr)
, last_(nullptr)
, end_(nullptr)
, max_((std::numeric_limits<
std::size_t>::max)())
{ {
} }
template<class Allocator> template<class Allocator>
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
basic_flat_buffer( basic_flat_buffer(
std::size_t limit, Allocator const& alloc) std::size_t limit) noexcept(default_nothrow)
: begin_(nullptr)
, in_(nullptr)
, out_(nullptr)
, last_(nullptr)
, end_(nullptr)
, max_(limit)
{
}
template<class Allocator>
basic_flat_buffer<Allocator>::
basic_flat_buffer(Allocator const& alloc) noexcept
: boost::empty_value<base_alloc_type>(
boost::empty_init_t(), alloc)
, begin_(nullptr)
, in_(nullptr)
, out_(nullptr)
, last_(nullptr)
, end_(nullptr)
, max_(alloc_traits::max_size(
this->get()))
{
}
template<class Allocator>
basic_flat_buffer<Allocator>::
basic_flat_buffer(
std::size_t limit,
Allocator const& alloc) noexcept
: boost::empty_value<base_alloc_type>( : boost::empty_value<base_alloc_type>(
boost::empty_init_t(), alloc) boost::empty_init_t(), alloc)
, begin_(nullptr) , begin_(nullptr)
@ -91,7 +94,7 @@ basic_flat_buffer(
template<class Allocator> template<class Allocator>
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
basic_flat_buffer(basic_flat_buffer&& other) basic_flat_buffer(basic_flat_buffer&& other) noexcept
: boost::empty_value<base_alloc_type>( : boost::empty_value<base_alloc_type>(
boost::empty_init_t(), std::move(other.get())) boost::empty_init_t(), std::move(other.get()))
, begin_(boost::exchange(other.begin_, nullptr)) , begin_(boost::exchange(other.begin_, nullptr))
@ -106,7 +109,8 @@ basic_flat_buffer(basic_flat_buffer&& other)
template<class Allocator> template<class Allocator>
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
basic_flat_buffer( basic_flat_buffer(
basic_flat_buffer&& other, Allocator const& alloc) basic_flat_buffer&& other,
Allocator const& alloc)
: boost::empty_value<base_alloc_type>( : boost::empty_value<base_alloc_type>(
boost::empty_init_t(), alloc) boost::empty_init_t(), alloc)
{ {
@ -119,7 +123,7 @@ basic_flat_buffer(
end_ = nullptr; end_ = nullptr;
max_ = other.max_; max_ = other.max_;
copy_from(other); copy_from(other);
other.reset(); other.clear();
} }
else else
{ {
@ -129,6 +133,9 @@ basic_flat_buffer(
last_ = other.out_; // invalidate last_ = other.out_; // invalidate
end_ = other.end_; end_ = other.end_;
max_ = other.max_; max_ = other.max_;
BOOST_ASSERT(
alloc_traits::max_size(this->get()) ==
alloc_traits::max_size(other.get()));
other.begin_ = nullptr; other.begin_ = nullptr;
other.in_ = nullptr; other.in_ = nullptr;
other.out_ = nullptr; other.out_ = nullptr;
@ -155,9 +162,11 @@ basic_flat_buffer(basic_flat_buffer const& other)
template<class Allocator> template<class Allocator>
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
basic_flat_buffer(basic_flat_buffer const& other, basic_flat_buffer(
Allocator const& alloc) basic_flat_buffer const& other,
: boost::empty_value<base_alloc_type>(boost::empty_init_t(), alloc) Allocator const& alloc)
: boost::empty_value<base_alloc_type>(
boost::empty_init_t(), alloc)
, begin_(nullptr) , begin_(nullptr)
, in_(nullptr) , in_(nullptr)
, out_(nullptr) , out_(nullptr)
@ -172,7 +181,8 @@ template<class Allocator>
template<class OtherAlloc> template<class OtherAlloc>
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
basic_flat_buffer( basic_flat_buffer(
basic_flat_buffer<OtherAlloc> const& other) basic_flat_buffer<OtherAlloc> const& other)
noexcept(default_nothrow)
: begin_(nullptr) : begin_(nullptr)
, in_(nullptr) , in_(nullptr)
, out_(nullptr) , out_(nullptr)
@ -186,9 +196,11 @@ basic_flat_buffer(
template<class Allocator> template<class Allocator>
template<class OtherAlloc> template<class OtherAlloc>
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
basic_flat_buffer(basic_flat_buffer<OtherAlloc> const& other, basic_flat_buffer(
Allocator const& alloc) basic_flat_buffer<OtherAlloc> const& other,
: boost::empty_value<base_alloc_type>(boost::empty_init_t(), alloc) Allocator const& alloc)
: boost::empty_value<base_alloc_type>(
boost::empty_init_t(), alloc)
, begin_(nullptr) , begin_(nullptr)
, in_(nullptr) , in_(nullptr)
, out_(nullptr) , out_(nullptr)
@ -202,12 +214,11 @@ basic_flat_buffer(basic_flat_buffer<OtherAlloc> const& other,
template<class Allocator> template<class Allocator>
auto auto
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
operator=(basic_flat_buffer&& other) -> operator=(basic_flat_buffer&& other) noexcept ->
basic_flat_buffer& basic_flat_buffer&
{ {
if(this != &other) if(this != &other)
move_assign(other, std::integral_constant<bool, move_assign(other, pocma{});
alloc_traits::propagate_on_container_move_assignment::value>{});
return *this; return *this;
} }
@ -218,8 +229,7 @@ operator=(basic_flat_buffer const& other) ->
basic_flat_buffer& basic_flat_buffer&
{ {
if(this != &other) if(this != &other)
copy_assign(other, std::integral_constant<bool, copy_assign(other, pocca{});
alloc_traits::propagate_on_container_copy_assignment::value>{});
return *this; return *this;
} }
@ -227,15 +237,25 @@ template<class Allocator>
template<class OtherAlloc> template<class OtherAlloc>
auto auto
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
operator=(basic_flat_buffer<OtherAlloc> const& other) -> operator=(
basic_flat_buffer<OtherAlloc> const& other) ->
basic_flat_buffer& basic_flat_buffer&
{ {
reset();
max_ = other.max_;
copy_from(other); copy_from(other);
return *this; return *this;
} }
template<class Allocator>
void
basic_flat_buffer<Allocator>::
reserve(std::size_t n)
{
if(max_ < n)
max_ = n;
if(capacity() < n)
prepare(n - capacity());
}
template<class Allocator> template<class Allocator>
void void
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
@ -249,8 +269,7 @@ shrink_to_fit()
{ {
BOOST_ASSERT(begin_); BOOST_ASSERT(begin_);
BOOST_ASSERT(in_); BOOST_ASSERT(in_);
p = alloc_traits::allocate( p = alloc(len);
this->get(), len);
std::memcpy(p, in_, len); std::memcpy(p, in_, len);
} }
else else
@ -258,7 +277,7 @@ shrink_to_fit()
p = nullptr; p = nullptr;
} }
alloc_traits::deallocate( alloc_traits::deallocate(
this->get(), begin_, this->dist(begin_, end_)); this->get(), begin_, this->capacity());
begin_ = p; begin_ = p;
in_ = begin_; in_ = begin_;
out_ = begin_ + len; out_ = begin_ + len;
@ -300,8 +319,7 @@ prepare(std::size_t n) ->
auto const new_size = (std::min<std::size_t>)( auto const new_size = (std::min<std::size_t>)(
max_, max_,
(std::max<std::size_t>)(2 * len, len + n)); (std::max<std::size_t>)(2 * len, len + n));
auto const p = alloc_traits::allocate( auto const p = alloc(new_size);
this->get(), new_size);
if(begin_) if(begin_)
{ {
BOOST_ASSERT(p); BOOST_ASSERT(p);
@ -335,24 +353,28 @@ consume(std::size_t n) noexcept
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template<class Allocator> template<class Allocator>
template<class OtherAlloc>
void void
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
reset() copy_from(
basic_flat_buffer<OtherAlloc> const& other)
{ {
consume(size()); std::size_t const n = other.size();
shrink_to_fit(); if(begin_ && (n == 0 || n > capacity()))
} clear();
if(n > capacity())
template<class Allocator> {
template<class DynamicBuffer> BOOST_ASSERT(! begin_);
void begin_ = alloc(n);
basic_flat_buffer<Allocator>:: in_ = begin_;
copy_from(DynamicBuffer const& buffer) out_ = begin_ + n;
{ last_ = begin_ + n;
if(buffer.size() == 0) end_ = begin_ + n;
return; }
commit(net::buffer_copy( in_ = begin_;
prepare(buffer.size()), buffer.data())); out_ = begin_ + n;
last_ = begin_ + n;
std::memcpy(begin_, other.begin_, n);
} }
template<class Allocator> template<class Allocator>
@ -360,7 +382,7 @@ void
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
move_assign(basic_flat_buffer& other, std::true_type) move_assign(basic_flat_buffer& other, std::true_type)
{ {
reset(); clear();
this->get() = std::move(other.get()); this->get() = std::move(other.get());
begin_ = other.begin_; begin_ = other.begin_;
in_ = other.in_; in_ = other.in_;
@ -380,11 +402,10 @@ void
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
move_assign(basic_flat_buffer& other, std::false_type) move_assign(basic_flat_buffer& other, std::false_type)
{ {
reset();
if(this->get() != other.get()) if(this->get() != other.get())
{ {
copy_from(other); copy_from(other);
other.reset(); other.clear();
} }
else else
{ {
@ -397,7 +418,6 @@ void
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
copy_assign(basic_flat_buffer const& other, std::true_type) copy_assign(basic_flat_buffer const& other, std::true_type)
{ {
reset();
max_ = other.max_; max_ = other.max_;
this->get() = other.get(); this->get() = other.get();
copy_from(other); copy_from(other);
@ -408,7 +428,7 @@ void
basic_flat_buffer<Allocator>:: basic_flat_buffer<Allocator>::
copy_assign(basic_flat_buffer const& other, std::false_type) copy_assign(basic_flat_buffer const& other, std::false_type)
{ {
reset(); clear();
max_ = other.max_; max_ = other.max_;
copy_from(other); copy_from(other);
} }
@ -463,6 +483,31 @@ swap(
lhs.swap(rhs); lhs.swap(rhs);
} }
template<class Allocator>
char*
basic_flat_buffer<Allocator>::
alloc(std::size_t n)
{
if(n > alloc_traits::max_size(this->get()))
BOOST_THROW_EXCEPTION(std::length_error(
"A basic_flat_buffer exceeded the allocator's maximum size"));
return alloc_traits::allocate(this->get(), n);
}
template<class Allocator>
void
basic_flat_buffer<Allocator>::
clear()
{
alloc_traits::deallocate(
this->get(), begin_, size());
begin_ = nullptr;
in_ = nullptr;
out_ = nullptr;
last_ = nullptr;
end_ = nullptr;
}
} // beast } // beast
} // boost } // boost

View File

@ -292,8 +292,10 @@ public:
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7)); BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
} }
{ {
flat_buffer b2{64}; flat_buffer b2{32};
BEAST_EXPECT(b2.max_size() == 32);
b2 = b1; b2 = b1;
BEAST_EXPECT(b2.max_size() == b1.max_size());
BEAST_EXPECT(buffers_to_string(b2.data()) == s); BEAST_EXPECT(buffers_to_string(b2.data()) == s);
b2.consume(7); b2.consume(7);
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7)); BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
@ -309,6 +311,14 @@ public:
BEAST_EXPECT(buffers_to_string(b.data()) == "4567890123"); BEAST_EXPECT(buffers_to_string(b.data()) == "4567890123");
} }
// max_size
{
flat_buffer b{10};
BEAST_EXPECT(b.max_size() == 10);
b.max_size(32);
BEAST_EXPECT(b.max_size() == 32);
}
// read_size // read_size
{ {
flat_buffer b{10}; flat_buffer b{10};
@ -372,6 +382,18 @@ public:
} }
} }
// reserve
{
flat_buffer b;
BEAST_EXPECT(b.capacity() == 0);
b.reserve(50);
BEAST_EXPECT(b.capacity() == 50);
b.prepare(20);
b.commit(20);
b.reserve(50);
BEAST_EXPECT(b.capacity() == 50);
}
// shrink to fit // shrink to fit
{ {
flat_buffer b; flat_buffer b;