1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249 |
- //
- // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- // Official repository: https://github.com/boostorg/beast
- //
- #ifndef BOOST_BEAST_IMPL_MULTI_BUFFER_HPP
- #define BOOST_BEAST_IMPL_MULTI_BUFFER_HPP
- #include <boost/beast/core/buffer_traits.hpp>
- #include <boost/config/workaround.hpp>
- #include <boost/core/exchange.hpp>
- #include <boost/assert.hpp>
- #include <boost/throw_exception.hpp>
- #include <algorithm>
- #include <exception>
- #include <sstream>
- #include <string>
- #include <type_traits>
- #include <utility>
- namespace boost {
- namespace beast {
- /* These diagrams illustrate the layout and state variables.
- 1 Input and output contained entirely in one element:
- 0 out_
- |<------+-----------+--------------------------------+----->|
- in_pos_ out_pos_ out_end_
- 2 Output contained in first and second elements:
- out_
- |<------+-----------+------>| |<-------------------+----->|
- in_pos_ out_pos_ out_end_
- 3 Output contained in the second element:
- out_
- |<------+------------------>| |<----+--------------+----->|
- in_pos_ out_pos_ out_end_
- 4 Output contained in second and third elements:
- out_
- |<------+------->| |<-------+------>| |<---------+----->|
- in_pos_ out_pos_ out_end_
- 5 Input sequence is empty:
- out_
- |<------+------------------>| |<-------------------+----->|
- out_pos_ out_end_
- in_pos_
- 6 Output sequence is empty:
- out_
- |<------+------------------>| |<------+------------------>|
- in_pos_ out_pos_
- out_end_
- 7 The end of output can point to the end of an element.
- But out_pos_ should never point to the end:
- out_
- |<------+------------------>| |<------+------------------>|
- in_pos_ out_pos_ out_end_
- 8 When the input sequence entirely fills the last element and
- the output sequence is empty, out_ will point to the end of
- the list of buffers, and out_pos_ and out_end_ will be 0:
- |<------+------------------>| out_ == list_.end()
- in_pos_ out_pos_ == 0
- out_end_ == 0
- */
- //------------------------------------------------------------------------------
- #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
- # pragma warning (push)
- # pragma warning (disable: 4521) // multiple copy constructors specified
- # pragma warning (disable: 4522) // multiple assignment operators specified
- #endif
- template<class Allocator>
- template<bool isMutable>
- class basic_multi_buffer<Allocator>::readable_bytes
- {
- basic_multi_buffer const* b_;
- friend class basic_multi_buffer;
- explicit
- readable_bytes(
- basic_multi_buffer const& b) noexcept
- : b_(&b)
- {
- }
- public:
- using value_type = typename
- std::conditional<
- isMutable,
- net::mutable_buffer,
- net::const_buffer>::type;
- class const_iterator;
- readable_bytes() = delete;
- #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
- readable_bytes(readable_bytes const& other)
- : b_(other.b_)
- {
- }
- readable_bytes& operator=(readable_bytes const& other)
- {
- b_ = other.b_;
- return *this;
- }
- #else
- readable_bytes(readable_bytes const&) = default;
- readable_bytes& operator=(readable_bytes const&) = default;
- #endif
- template<
- bool isMutable_ = isMutable,
- class = typename std::enable_if<! isMutable_>::type>
- readable_bytes(
- readable_bytes<true> const& other) noexcept
- : b_(other.b_)
- {
- }
- template<
- bool isMutable_ = isMutable,
- class = typename std::enable_if<! isMutable_>::type>
- readable_bytes& operator=(
- readable_bytes<true> const& other) noexcept
- {
- b_ = other.b_;
- return *this;
- }
- const_iterator begin() const noexcept;
- const_iterator end() const noexcept;
- std::size_t
- buffer_bytes() const noexcept
- {
- return b_->size();
- }
- };
- #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
- # pragma warning (pop)
- #endif
- //------------------------------------------------------------------------------
- template<class Allocator>
- template<bool isMutable>
- class
- basic_multi_buffer<Allocator>::
- readable_bytes<isMutable>::
- const_iterator
- {
- basic_multi_buffer const* b_ = nullptr;
- typename list_type::const_iterator it_;
- public:
- using value_type =
- typename readable_bytes::value_type;
- using pointer = value_type const*;
- using reference = value_type;
- using difference_type = std::ptrdiff_t;
- using iterator_category =
- std::bidirectional_iterator_tag;
- const_iterator() = default;
- const_iterator(
- const_iterator const& other) = default;
- const_iterator& operator=(
- const_iterator const& other) = default;
- const_iterator(
- basic_multi_buffer const& b, typename
- list_type::const_iterator const& it) noexcept
- : b_(&b)
- , it_(it)
- {
- }
- bool
- operator==(const_iterator const& other) const noexcept
- {
- return b_ == other.b_ && it_ == other.it_;
- }
- bool
- operator!=(const_iterator const& other) const noexcept
- {
- return !(*this == other);
- }
- reference
- operator*() const noexcept
- {
- auto const& e = *it_;
- return value_type{e.data(),
- (b_->out_ == b_->list_.end() ||
- &e != &*b_->out_) ? e.size() : b_->out_pos_} +
- (&e == &*b_->list_.begin() ? b_->in_pos_ : 0);
- }
- pointer
- operator->() const = delete;
- const_iterator&
- operator++() noexcept
- {
- ++it_;
- return *this;
- }
- const_iterator
- operator++(int) noexcept
- {
- auto temp = *this;
- ++(*this);
- return temp;
- }
- const_iterator&
- operator--() noexcept
- {
- --it_;
- return *this;
- }
- const_iterator
- operator--(int) noexcept
- {
- auto temp = *this;
- --(*this);
- return temp;
- }
- };
- //------------------------------------------------------------------------------
- template<class Allocator>
- class basic_multi_buffer<Allocator>::mutable_buffers_type
- {
- basic_multi_buffer const* b_;
- friend class basic_multi_buffer;
- explicit
- mutable_buffers_type(
- basic_multi_buffer const& b) noexcept
- : b_(&b)
- {
- }
- public:
- using value_type = net::mutable_buffer;
- class const_iterator;
- mutable_buffers_type() = delete;
- mutable_buffers_type(mutable_buffers_type const&) = default;
- mutable_buffers_type& operator=(mutable_buffers_type const&) = default;
- const_iterator begin() const noexcept;
- const_iterator end() const noexcept;
- };
- //------------------------------------------------------------------------------
- template<class Allocator>
- class basic_multi_buffer<Allocator>::mutable_buffers_type::const_iterator
- {
- basic_multi_buffer const* b_ = nullptr;
- typename list_type::const_iterator it_;
- public:
- using value_type = typename
- mutable_buffers_type::value_type;
- using pointer = value_type const*;
- using reference = value_type;
- using difference_type = std::ptrdiff_t;
- using iterator_category =
- std::bidirectional_iterator_tag;
- const_iterator() = default;
- const_iterator(const_iterator const& other) = default;
- const_iterator& operator=(const_iterator const& other) = default;
- const_iterator(
- basic_multi_buffer const& b,
- typename list_type::const_iterator const& it) noexcept
- : b_(&b)
- , it_(it)
- {
- }
- bool
- operator==(const_iterator const& other) const noexcept
- {
- return b_ == other.b_ && it_ == other.it_;
- }
- bool
- operator!=(const_iterator const& other) const noexcept
- {
- return !(*this == other);
- }
- reference
- operator*() const noexcept
- {
- auto const& e = *it_;
- return value_type{e.data(),
- &e == &*std::prev(b_->list_.end()) ?
- b_->out_end_ : e.size()} +
- (&e == &*b_->out_ ? b_->out_pos_ : 0);
- }
- pointer
- operator->() const = delete;
- const_iterator&
- operator++() noexcept
- {
- ++it_;
- return *this;
- }
- const_iterator
- operator++(int) noexcept
- {
- auto temp = *this;
- ++(*this);
- return temp;
- }
- const_iterator&
- operator--() noexcept
- {
- --it_;
- return *this;
- }
- const_iterator
- operator--(int) noexcept
- {
- auto temp = *this;
- --(*this);
- return temp;
- }
- };
- //------------------------------------------------------------------------------
- template<class Allocator>
- template<bool isMutable>
- auto
- basic_multi_buffer<Allocator>::
- readable_bytes<isMutable>::
- begin() const noexcept ->
- const_iterator
- {
- return const_iterator{*b_, b_->list_.begin()};
- }
- template<class Allocator>
- template<bool isMutable>
- auto
- basic_multi_buffer<Allocator>::
- readable_bytes<isMutable>::
- end() const noexcept ->
- const_iterator
- {
- return const_iterator{*b_, b_->out_ ==
- b_->list_.end() ? b_->list_.end() :
- std::next(b_->out_)};
- }
- template<class Allocator>
- auto
- basic_multi_buffer<Allocator>::
- mutable_buffers_type::
- begin() const noexcept ->
- const_iterator
- {
- return const_iterator{*b_, b_->out_};
- }
- template<class Allocator>
- auto
- basic_multi_buffer<Allocator>::
- mutable_buffers_type::
- end() const noexcept ->
- const_iterator
- {
- return const_iterator{*b_, b_->list_.end()};
- }
- //------------------------------------------------------------------------------
- template<class Allocator>
- basic_multi_buffer<Allocator>::
- ~basic_multi_buffer()
- {
- destroy(list_);
- }
- template<class Allocator>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer() noexcept(default_nothrow)
- : max_(alloc_traits::max_size(this->get()))
- , out_(list_.end())
- {
- }
- template<class Allocator>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer(
- std::size_t limit) noexcept(default_nothrow)
- : max_(limit)
- , out_(list_.end())
- {
- }
- template<class Allocator>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer(
- Allocator const& alloc) noexcept
- : boost::empty_value<Allocator>(
- boost::empty_init_t(), alloc)
- , max_(alloc_traits::max_size(this->get()))
- , out_(list_.end())
- {
- }
- template<class Allocator>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer(
- std::size_t limit,
- Allocator const& alloc) noexcept
- : boost::empty_value<Allocator>(
- boost::empty_init_t(), alloc)
- , max_(limit)
- , out_(list_.end())
- {
- }
- template<class Allocator>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer(
- basic_multi_buffer&& other) noexcept
- : boost::empty_value<Allocator>(
- boost::empty_init_t(), std::move(other.get()))
- , max_(other.max_)
- , in_size_(boost::exchange(other.in_size_, 0))
- , in_pos_(boost::exchange(other.in_pos_, 0))
- , out_pos_(boost::exchange(other.out_pos_, 0))
- , out_end_(boost::exchange(other.out_end_, 0))
- {
- auto const at_end =
- other.out_ == other.list_.end();
- list_ = std::move(other.list_);
- out_ = at_end ? list_.end() : other.out_;
- other.out_ = other.list_.end();
- }
- template<class Allocator>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer(
- basic_multi_buffer&& other,
- Allocator const& alloc)
- : boost::empty_value<Allocator>(
- boost::empty_init_t(), alloc)
- , max_(other.max_)
- {
- if(this->get() != other.get())
- {
- out_ = list_.end();
- copy_from(other);
- other.clear();
- other.shrink_to_fit();
- return;
- }
- auto const at_end =
- other.out_ == other.list_.end();
- list_ = std::move(other.list_);
- out_ = at_end ? list_.end() : other.out_;
- in_size_ = other.in_size_;
- in_pos_ = other.in_pos_;
- out_pos_ = other.out_pos_;
- out_end_ = other.out_end_;
- other.in_size_ = 0;
- other.out_ = other.list_.end();
- other.in_pos_ = 0;
- other.out_pos_ = 0;
- other.out_end_ = 0;
- }
- template<class Allocator>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer(
- basic_multi_buffer const& other)
- : boost::empty_value<Allocator>(
- boost::empty_init_t(), alloc_traits::
- select_on_container_copy_construction(
- other.get()))
- , max_(other.max_)
- , out_(list_.end())
- {
- copy_from(other);
- }
- template<class Allocator>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer(
- basic_multi_buffer const& other,
- Allocator const& alloc)
- : boost::empty_value<Allocator>(
- boost::empty_init_t(), alloc)
- , max_(other.max_)
- , out_(list_.end())
- {
- copy_from(other);
- }
- template<class Allocator>
- template<class OtherAlloc>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer(
- basic_multi_buffer<OtherAlloc> const& other)
- : out_(list_.end())
- {
- copy_from(other);
- }
- template<class Allocator>
- template<class OtherAlloc>
- basic_multi_buffer<Allocator>::
- basic_multi_buffer(
- basic_multi_buffer<OtherAlloc> const& other,
- allocator_type const& alloc)
- : boost::empty_value<Allocator>(
- boost::empty_init_t(), alloc)
- , max_(other.max_)
- , out_(list_.end())
- {
- copy_from(other);
- }
- template<class Allocator>
- auto
- basic_multi_buffer<Allocator>::
- operator=(basic_multi_buffer&& other) ->
- basic_multi_buffer&
- {
- if(this == &other)
- return *this;
- clear();
- max_ = other.max_;
- move_assign(other, pocma{});
- return *this;
- }
- template<class Allocator>
- auto
- basic_multi_buffer<Allocator>::
- operator=(basic_multi_buffer const& other) ->
- basic_multi_buffer&
- {
- if(this == &other)
- return *this;
- copy_assign(other, pocca{});
- return *this;
- }
- template<class Allocator>
- template<class OtherAlloc>
- auto
- basic_multi_buffer<Allocator>::
- operator=(
- basic_multi_buffer<OtherAlloc> const& other) ->
- basic_multi_buffer&
- {
- copy_from(other);
- return *this;
- }
- //------------------------------------------------------------------------------
- template<class Allocator>
- std::size_t
- basic_multi_buffer<Allocator>::
- capacity() const noexcept
- {
- auto pos = out_;
- if(pos == list_.end())
- return in_size_;
- auto n = pos->size() - out_pos_;
- while(++pos != list_.end())
- n += pos->size();
- return in_size_ + n;
- }
- template<class Allocator>
- auto
- basic_multi_buffer<Allocator>::
- data() const noexcept ->
- const_buffers_type
- {
- return const_buffers_type(*this);
- }
- template<class Allocator>
- auto
- basic_multi_buffer<Allocator>::
- data() noexcept ->
- mutable_data_type
- {
- return mutable_data_type(*this);
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- reserve(std::size_t n)
- {
- // VFALCO The amount needs to be adjusted for
- // the sizeof(element) plus padding
- if(n > alloc_traits::max_size(this->get()))
- BOOST_THROW_EXCEPTION(std::length_error(
- "A basic_multi_buffer exceeded the allocator's maximum size"));
- std::size_t total = in_size_;
- if(n <= total)
- return;
- if(out_ != list_.end())
- {
- total += out_->size() - out_pos_;
- if(n <= total)
- return;
- for(auto it = out_;;)
- {
- if(++it == list_.end())
- break;
- total += it->size();
- if(n <= total)
- return;
- }
- }
- BOOST_ASSERT(n > total);
- (void)prepare(n - size());
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- shrink_to_fit()
- {
- // empty list
- if(list_.empty())
- return;
- // zero readable bytes
- if(in_size_ == 0)
- {
- destroy(list_);
- list_.clear();
- out_ = list_.end();
- in_size_ = 0;
- in_pos_ = 0;
- out_pos_ = 0;
- out_end_ = 0;
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- return;
- }
- // one or more unused output buffers
- if(out_ != list_.end())
- {
- if(out_ != list_.iterator_to(list_.back()))
- {
- // unused list
- list_type extra;
- extra.splice(
- extra.end(),
- list_,
- std::next(out_),
- list_.end());
- destroy(extra);
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- // unused out_
- BOOST_ASSERT(out_ ==
- list_.iterator_to(list_.back()));
- if(out_pos_ == 0)
- {
- BOOST_ASSERT(out_ != list_.begin());
- auto& e = *out_;
- list_.erase(out_);
- out_ = list_.end();
- destroy(e);
- out_end_ = 0;
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- }
- auto const replace =
- [&](iter pos, element& e)
- {
- auto it =
- list_.insert(pos, e);
- auto& e0 = *pos;
- list_.erase(pos);
- destroy(e0);
- return it;
- };
- // partial last buffer
- if(list_.size() > 1 && out_ != list_.end())
- {
- BOOST_ASSERT(out_ ==
- list_.iterator_to(list_.back()));
- BOOST_ASSERT(out_pos_ != 0);
- auto& e = alloc(out_pos_);
- std::memcpy(
- e.data(),
- out_->data(),
- out_pos_);
- replace(out_, e);
- out_ = list_.end();
- out_pos_ = 0;
- out_end_ = 0;
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- // partial first buffer
- if(in_pos_ != 0)
- {
- if(out_ != list_.begin())
- {
- auto const n =
- list_.front().size() - in_pos_;
- auto& e = alloc(n);
- std::memcpy(
- e.data(),
- list_.front().data() + in_pos_,
- n);
- replace(list_.begin(), e);
- in_pos_ = 0;
- }
- else
- {
- BOOST_ASSERT(list_.size() == 1);
- BOOST_ASSERT(out_pos_ > in_pos_);
- auto const n = out_pos_ - in_pos_;
- auto& e = alloc(n);
- std::memcpy(
- e.data(),
- list_.front().data() + in_pos_,
- n);
- replace(list_.begin(), e);
- in_pos_ = 0;
- out_ = list_.end();
- }
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- clear() noexcept
- {
- out_ = list_.begin();
- in_size_ = 0;
- in_pos_ = 0;
- out_pos_ = 0;
- out_end_ = 0;
- }
- template<class Allocator>
- auto
- basic_multi_buffer<Allocator>::
- prepare(size_type n) ->
- mutable_buffers_type
- {
- if(in_size_ > max_ || n > (max_ - in_size_))
- BOOST_THROW_EXCEPTION(std::length_error{
- "basic_multi_buffer too long"});
- list_type reuse;
- std::size_t total = in_size_;
- // put all empty buffers on reuse list
- if(out_ != list_.end())
- {
- total += out_->size() - out_pos_;
- if(out_ != list_.iterator_to(list_.back()))
- {
- out_end_ = out_->size();
- reuse.splice(reuse.end(), list_,
- std::next(out_), list_.end());
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- auto const avail = out_->size() - out_pos_;
- if(n > avail)
- {
- out_end_ = out_->size();
- n -= avail;
- }
- else
- {
- out_end_ = out_pos_ + n;
- n = 0;
- }
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- // get space from reuse buffers
- while(n > 0 && ! reuse.empty())
- {
- auto& e = reuse.front();
- reuse.erase(reuse.iterator_to(e));
- list_.push_back(e);
- total += e.size();
- if(n > e.size())
- {
- out_end_ = e.size();
- n -= e.size();
- }
- else
- {
- out_end_ = n;
- n = 0;
- }
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- BOOST_ASSERT(total <= max_);
- if(! reuse.empty() || n > 0)
- {
- destroy(reuse);
- if(n > 0)
- {
- static auto const growth_factor = 2.0f;
- auto const size =
- (std::min<std::size_t>)(
- max_ - total,
- (std::max<std::size_t>)({
- static_cast<std::size_t>(
- in_size_ * growth_factor - in_size_),
- 512,
- n}));
- auto& e = alloc(size);
- list_.push_back(e);
- if(out_ == list_.end())
- out_ = list_.iterator_to(e);
- out_end_ = n;
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- }
- return mutable_buffers_type(*this);
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- commit(size_type n) noexcept
- {
- if(list_.empty())
- return;
- if(out_ == list_.end())
- return;
- auto const back =
- list_.iterator_to(list_.back());
- while(out_ != back)
- {
- auto const avail =
- out_->size() - out_pos_;
- if(n < avail)
- {
- out_pos_ += n;
- in_size_ += n;
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- return;
- }
- ++out_;
- n -= avail;
- out_pos_ = 0;
- in_size_ += avail;
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- n = (std::min)(n, out_end_ - out_pos_);
- out_pos_ += n;
- in_size_ += n;
- if(out_pos_ == out_->size())
- {
- ++out_;
- out_pos_ = 0;
- out_end_ = 0;
- }
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- consume(size_type n) noexcept
- {
- if(list_.empty())
- return;
- for(;;)
- {
- if(list_.begin() != out_)
- {
- auto const avail =
- list_.front().size() - in_pos_;
- if(n < avail)
- {
- in_size_ -= n;
- in_pos_ += n;
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- break;
- }
- n -= avail;
- in_size_ -= avail;
- in_pos_ = 0;
- auto& e = list_.front();
- list_.erase(list_.iterator_to(e));
- destroy(e);
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- }
- else
- {
- auto const avail = out_pos_ - in_pos_;
- if(n < avail)
- {
- in_size_ -= n;
- in_pos_ += n;
- }
- else
- {
- in_size_ = 0;
- if(out_ != list_.iterator_to(list_.back()) ||
- out_pos_ != out_end_)
- {
- in_pos_ = out_pos_;
- }
- else
- {
- // Input and output sequences are empty, reuse buffer.
- // Alternatively we could deallocate it.
- in_pos_ = 0;
- out_pos_ = 0;
- out_end_ = 0;
- }
- }
- #if BOOST_BEAST_MULTI_BUFFER_DEBUG_CHECK
- debug_check();
- #endif
- break;
- }
- }
- }
- template<class Allocator>
- template<class OtherAlloc>
- void
- basic_multi_buffer<Allocator>::
- copy_from(basic_multi_buffer<OtherAlloc> const& other)
- {
- clear();
- max_ = other.max_;
- if(other.size() == 0)
- return;
- commit(net::buffer_copy(
- prepare(other.size()), other.data()));
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- move_assign(basic_multi_buffer& other, std::true_type) noexcept
- {
- this->get() = std::move(other.get());
- auto const at_end =
- other.out_ == other.list_.end();
- list_ = std::move(other.list_);
- out_ = at_end ? list_.end() : other.out_;
- in_size_ = other.in_size_;
- in_pos_ = other.in_pos_;
- out_pos_ = other.out_pos_;
- out_end_ = other.out_end_;
- max_ = other.max_;
- other.in_size_ = 0;
- other.out_ = other.list_.end();
- other.in_pos_ = 0;
- other.out_pos_ = 0;
- other.out_end_ = 0;
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- move_assign(basic_multi_buffer& other, std::false_type)
- {
- if(this->get() != other.get())
- {
- copy_from(other);
- other.clear();
- other.shrink_to_fit();
- }
- else
- {
- move_assign(other, std::true_type{});
- }
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- copy_assign(
- basic_multi_buffer const& other, std::false_type)
- {
- copy_from(other);
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- copy_assign(
- basic_multi_buffer const& other, std::true_type)
- {
- clear();
- this->get() = other.get();
- copy_from(other);
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- swap(basic_multi_buffer& other) noexcept
- {
- swap(other, typename
- alloc_traits::propagate_on_container_swap{});
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- swap(basic_multi_buffer& other, std::true_type) noexcept
- {
- using std::swap;
- auto const at_end0 =
- out_ == list_.end();
- auto const at_end1 =
- other.out_ == other.list_.end();
- swap(this->get(), other.get());
- swap(list_, other.list_);
- swap(out_, other.out_);
- if(at_end1)
- out_ = list_.end();
- if(at_end0)
- other.out_ = other.list_.end();
- swap(in_size_, other.in_size_);
- swap(in_pos_, other.in_pos_);
- swap(out_pos_, other.out_pos_);
- swap(out_end_, other.out_end_);
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- swap(basic_multi_buffer& other, std::false_type) noexcept
- {
- BOOST_ASSERT(this->get() == other.get());
- using std::swap;
- auto const at_end0 =
- out_ == list_.end();
- auto const at_end1 =
- other.out_ == other.list_.end();
- swap(list_, other.list_);
- swap(out_, other.out_);
- if(at_end1)
- out_ = list_.end();
- if(at_end0)
- other.out_ = other.list_.end();
- swap(in_size_, other.in_size_);
- swap(in_pos_, other.in_pos_);
- swap(out_pos_, other.out_pos_);
- swap(out_end_, other.out_end_);
- }
- template<class Allocator>
- void
- swap(
- basic_multi_buffer<Allocator>& lhs,
- basic_multi_buffer<Allocator>& rhs) noexcept
- {
- lhs.swap(rhs);
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- destroy(list_type& list) noexcept
- {
- for(auto it = list.begin();
- it != list.end();)
- destroy(*it++);
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- destroy(const_iter it)
- {
- auto& e = list_.erase(it);
- destroy(e);
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- destroy(element& e)
- {
- auto a = rebind_type{this->get()};
- auto const n =
- (sizeof(element) + e.size() +
- sizeof(align_type) - 1) /
- sizeof(align_type);
- e.~element();
- alloc_traits::deallocate(a,
- reinterpret_cast<align_type*>(&e), n);
- }
- template<class Allocator>
- auto
- basic_multi_buffer<Allocator>::
- alloc(std::size_t size) ->
- element&
- {
- if(size > alloc_traits::max_size(this->get()))
- BOOST_THROW_EXCEPTION(std::length_error(
- "A basic_multi_buffer exceeded the allocator's maximum size"));
- auto a = rebind_type{this->get()};
- auto const p = alloc_traits::allocate(a,
- (sizeof(element) + size + sizeof(align_type) - 1) /
- sizeof(align_type));
- return *(::new(p) element(size));
- }
- template<class Allocator>
- void
- basic_multi_buffer<Allocator>::
- debug_check() const
- {
- #ifndef NDEBUG
- BOOST_ASSERT(buffer_bytes(data()) == in_size_);
- if(list_.empty())
- {
- BOOST_ASSERT(in_pos_ == 0);
- BOOST_ASSERT(in_size_ == 0);
- BOOST_ASSERT(out_pos_ == 0);
- BOOST_ASSERT(out_end_ == 0);
- BOOST_ASSERT(out_ == list_.end());
- return;
- }
- auto const& front = list_.front();
- BOOST_ASSERT(in_pos_ < front.size());
- if(out_ == list_.end())
- {
- BOOST_ASSERT(out_pos_ == 0);
- BOOST_ASSERT(out_end_ == 0);
- }
- else
- {
- auto const& out = *out_;
- auto const& back = list_.back();
- BOOST_ASSERT(out_end_ <= back.size());
- BOOST_ASSERT(out_pos_ < out.size());
- BOOST_ASSERT(&out != &front || out_pos_ >= in_pos_);
- BOOST_ASSERT(&out != &front || out_pos_ - in_pos_ == in_size_);
- BOOST_ASSERT(&out != &back || out_pos_ <= out_end_);
- }
- #endif
- }
- } // beast
- } // boost
- #endif
|