pausation always allocates

This commit is contained in:
Vinnie Falco
2017-12-02 11:22:51 -08:00
parent eb0cc97e1a
commit 3361df142c
4 changed files with 78 additions and 144 deletions

View File

@ -2,6 +2,9 @@ Version 149:
* built-in r-value return values can't be assigned * built-in r-value return values can't be assigned
* Tidy up ssl_stream special members * Tidy up ssl_stream special members
* Fix CMakeLists.txt variable
* Protect calls from macros
* pausation always allocates
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -10,8 +13,6 @@ Version 148:
* Install codecov on codecov CI targets only * Install codecov on codecov CI targets only
* Update reports for hybrid assessment * Update reports for hybrid assessment
* Handle invalid deflate frames * Handle invalid deflate frames
* Fix CMakeLists.txt variable
* Protect calls from macros
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -10,13 +10,10 @@
#ifndef BOOST_BEAST_WEBSOCKET_DETAIL_PAUSATION_HPP #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_PAUSATION_HPP
#define BOOST_BEAST_WEBSOCKET_DETAIL_PAUSATION_HPP #define BOOST_BEAST_WEBSOCKET_DETAIL_PAUSATION_HPP
#include <boost/beast/core/handler_ptr.hpp> #include <boost/beast/core/detail/allocator.hpp>
#include <boost/asio/associated_allocator.hpp> #include <boost/asio/associated_allocator.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <array>
#include <memory> #include <memory>
#include <new>
#include <utility> #include <utility>
namespace boost { namespace boost {
@ -30,124 +27,59 @@ namespace detail {
// //
class pausation class pausation
{ {
struct base struct handler
{ {
base() = default; handler() = default;
base(base &&) = delete; handler(handler &&) = delete;
base(base const&) = delete; handler(handler const&) = delete;
virtual ~base() = default; virtual ~handler() = default;
virtual void operator()() = 0; virtual void destroy() = 0;
virtual void invoke() = 0;
}; };
template<class F> template<class Handler>
struct holder : base class impl : public handler
{ {
F f; Handler h_;
holder(holder&&) = default;
template<class U>
explicit
holder(U&& u)
: f(std::forward<U>(u))
{
}
void
operator()() override
{
F f_(std::move(f));
this->~holder();
// invocation of f_() can
// assign a new object to *this.
f_();
}
};
struct exemplar : boost::asio::coroutine
{
struct H
{
void operator()();
};
struct T
{
using handler_type = H;
};
handler_ptr<T, H> hp;
void operator()();
};
template<class Op>
class saved_op
{
Op* op_ = nullptr;
public: public:
~saved_op() template<class DeducedHandler>
impl(DeducedHandler&& h)
: h_(std::forward<DeducedHandler>(h))
{ {
if(op_)
{
Op op(std::move(*op_));
op_->~Op();
typename std::allocator_traits<
boost::asio::associated_allocator_t<Op>>::
template rebind_alloc<Op> alloc{
boost::asio::get_associated_allocator(op)};
std::allocator_traits<
decltype(alloc)>::deallocate(alloc, op_, 1);
}
}
saved_op(saved_op&& other)
: op_(other.op_)
{
other.op_ = nullptr;
}
saved_op& operator=(saved_op&& other)
{
BOOST_ASSERT(! op_);
op_ = other.op_;
other.op_ = 0;
return *this;
}
explicit
saved_op(Op&& op)
{
typename std::allocator_traits<
boost::asio::associated_allocator_t<Op>>::
template rebind_alloc<Op> alloc{
boost::asio::get_associated_allocator(op)};
auto const p = std::allocator_traits<
decltype(alloc)>::allocate(alloc, 1);
op_ = new(p) Op{std::move(op)};
} }
void void
operator()() destroy() override
{ {
BOOST_ASSERT(op_); Handler h(std::move(h_));
Op op{std::move(*op_)}; typename beast::detail::allocator_traits<
typename std::allocator_traits< boost::asio::associated_allocator_t<
boost::asio::associated_allocator_t<Op>>:: Handler>>::template rebind_alloc<impl> alloc{
template rebind_alloc<Op> alloc{ boost::asio::get_associated_allocator(h)};
boost::asio::get_associated_allocator(op)}; beast::detail::allocator_traits<
std::allocator_traits< decltype(alloc)>::destroy(alloc, this);
decltype(alloc)>::deallocate(alloc, op_, 1); beast::detail::allocator_traits<
op_ = nullptr; decltype(alloc)>::deallocate(alloc, this, 1);
op(); }
void
invoke() override
{
Handler h(std::move(h_));
typename beast::detail::allocator_traits<
boost::asio::associated_allocator_t<
Handler>>::template rebind_alloc<impl> alloc{
boost::asio::get_associated_allocator(h)};
beast::detail::allocator_traits<
decltype(alloc)>::destroy(alloc, this);
beast::detail::allocator_traits<
decltype(alloc)>::deallocate(alloc, this, 1);
h();
} }
}; };
using buf_type = char[sizeof(holder<exemplar>)]; handler* h_ = nullptr;
base* base_ = nullptr;
alignas(holder<exemplar>) buf_type buf_;
public: public:
pausation() = default; pausation() = default;
@ -156,69 +88,70 @@ public:
~pausation() ~pausation()
{ {
if(base_) if(h_)
base_->~base(); h_->destroy();
} }
pausation(pausation&& other) pausation(pausation&& other)
{ {
boost::ignore_unused(other); boost::ignore_unused(other);
BOOST_ASSERT(! other.base_); BOOST_ASSERT(! other.h_);
} }
pausation& pausation&
operator=(pausation&& other) operator=(pausation&& other)
{ {
boost::ignore_unused(other); boost::ignore_unused(other);
BOOST_ASSERT(! base_); BOOST_ASSERT(! h_);
BOOST_ASSERT(! other.base_); BOOST_ASSERT(! other.h_);
return *this; return *this;
} }
template<class F> template<class CompletionHandler>
void void
emplace(F&& f); emplace(CompletionHandler&& handler);
template<class F>
void
save(F&& f);
explicit explicit
operator bool() const operator bool() const
{ {
return base_ != nullptr; return h_ != nullptr;
} }
bool bool
maybe_invoke() maybe_invoke()
{ {
if(base_) if(h_)
{ {
auto const basep = base_; auto const h = h_;
base_ = nullptr; h_ = nullptr;
(*basep)(); h->invoke();
return true; return true;
} }
return false; return false;
} }
}; };
template<class F> template<class CompletionHandler>
void void
pausation::emplace(F&& f) pausation::emplace(CompletionHandler&& handler)
{ {
using type = holder<typename std::decay<F>::type>; BOOST_ASSERT(! h_);
static_assert(sizeof(buf_type) >= sizeof(type), typename beast::detail::allocator_traits<
"buffer too small"); boost::asio::associated_allocator_t<
BOOST_ASSERT(! base_); CompletionHandler>>::template rebind_alloc<
base_ = ::new(buf_) type{std::forward<F>(f)}; impl<CompletionHandler>> alloc{
} boost::asio::get_associated_allocator(handler)};
using A = decltype(alloc);
template<class F> auto const d =
void [&alloc](impl<CompletionHandler>* p)
pausation::save(F&& f) {
{ beast::detail::allocator_traits<A>::deallocate(alloc, p, 1);
emplace(saved_op<F>{std::move(f)}); };
std::unique_ptr<impl<CompletionHandler>, decltype(d)> p{
beast::detail::allocator_traits<A>::allocate(alloc, 1), d};
beast::detail::allocator_traits<A>::construct(
alloc, p.get(), std::forward<CompletionHandler>(handler));
h_ = p.release();
} }
} // detail } // detail

View File

@ -152,7 +152,7 @@ operator()(
// Suspend // Suspend
BOOST_ASSERT(ws_.rd_block_ != tok_); BOOST_ASSERT(ws_.rd_block_ != tok_);
BOOST_ASIO_CORO_YIELD BOOST_ASIO_CORO_YIELD
ws_.paused_r_rd_.save(std::move(*this)); ws_.paused_r_rd_.emplace(std::move(*this));
// Acquire the read block // Acquire the read block
BOOST_ASSERT(! ws_.rd_block_); BOOST_ASSERT(! ws_.rd_block_);
@ -275,7 +275,7 @@ operator()(
// Suspend // Suspend
BOOST_ASSERT(ws_.wr_block_ != tok_); BOOST_ASSERT(ws_.wr_block_ != tok_);
BOOST_ASIO_CORO_YIELD BOOST_ASIO_CORO_YIELD
ws_.paused_rd_.save(std::move(*this)); ws_.paused_rd_.emplace(std::move(*this));
// Acquire the write block // Acquire the write block
BOOST_ASSERT(! ws_.wr_block_); BOOST_ASSERT(! ws_.wr_block_);
@ -580,7 +580,7 @@ operator()(
// Suspend // Suspend
BOOST_ASSERT(ws_.wr_block_ != tok_); BOOST_ASSERT(ws_.wr_block_ != tok_);
BOOST_ASIO_CORO_YIELD BOOST_ASIO_CORO_YIELD
ws_.paused_rd_.save(std::move(*this)); ws_.paused_rd_.emplace(std::move(*this));
// Acquire the write block // Acquire the write block
BOOST_ASSERT(! ws_.wr_block_); BOOST_ASSERT(! ws_.wr_block_);

View File

@ -208,7 +208,7 @@ operator()(
// Suspend // Suspend
BOOST_ASSERT(ws_.wr_block_ != tok_); BOOST_ASSERT(ws_.wr_block_ != tok_);
BOOST_ASIO_CORO_YIELD BOOST_ASIO_CORO_YIELD
ws_.paused_wr_.save(std::move(*this)); ws_.paused_wr_.emplace(std::move(*this));
// Acquire the write block // Acquire the write block
BOOST_ASSERT(! ws_.wr_block_); BOOST_ASSERT(! ws_.wr_block_);