pausation always allocates
This commit is contained in:
parent
eb0cc97e1a
commit
3361df142c
@ -2,6 +2,9 @@ Version 149:
|
||||
|
||||
* built-in r-value return values can't be assigned
|
||||
* 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
|
||||
* Update reports for hybrid assessment
|
||||
* Handle invalid deflate frames
|
||||
* Fix CMakeLists.txt variable
|
||||
* Protect calls from macros
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
@ -10,13 +10,10 @@
|
||||
#ifndef 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/coroutine.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <utility>
|
||||
|
||||
namespace boost {
|
||||
@ -30,124 +27,59 @@ namespace detail {
|
||||
//
|
||||
class pausation
|
||||
{
|
||||
struct base
|
||||
struct handler
|
||||
{
|
||||
base() = default;
|
||||
base(base &&) = delete;
|
||||
base(base const&) = delete;
|
||||
virtual ~base() = default;
|
||||
virtual void operator()() = 0;
|
||||
handler() = default;
|
||||
handler(handler &&) = delete;
|
||||
handler(handler const&) = delete;
|
||||
virtual ~handler() = default;
|
||||
virtual void destroy() = 0;
|
||||
virtual void invoke() = 0;
|
||||
};
|
||||
|
||||
template<class F>
|
||||
struct holder : base
|
||||
template<class Handler>
|
||||
class impl : public handler
|
||||
{
|
||||
F f;
|
||||
|
||||
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;
|
||||
Handler h_;
|
||||
|
||||
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
|
||||
operator()()
|
||||
destroy() override
|
||||
{
|
||||
BOOST_ASSERT(op_);
|
||||
Op op{std::move(*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);
|
||||
op_ = nullptr;
|
||||
op();
|
||||
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);
|
||||
}
|
||||
|
||||
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>)];
|
||||
|
||||
base* base_ = nullptr;
|
||||
alignas(holder<exemplar>) buf_type buf_;
|
||||
handler* h_ = nullptr;
|
||||
|
||||
public:
|
||||
pausation() = default;
|
||||
@ -156,69 +88,70 @@ public:
|
||||
|
||||
~pausation()
|
||||
{
|
||||
if(base_)
|
||||
base_->~base();
|
||||
if(h_)
|
||||
h_->destroy();
|
||||
}
|
||||
|
||||
pausation(pausation&& other)
|
||||
{
|
||||
boost::ignore_unused(other);
|
||||
BOOST_ASSERT(! other.base_);
|
||||
BOOST_ASSERT(! other.h_);
|
||||
}
|
||||
|
||||
pausation&
|
||||
operator=(pausation&& other)
|
||||
{
|
||||
boost::ignore_unused(other);
|
||||
BOOST_ASSERT(! base_);
|
||||
BOOST_ASSERT(! other.base_);
|
||||
BOOST_ASSERT(! h_);
|
||||
BOOST_ASSERT(! other.h_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class F>
|
||||
template<class CompletionHandler>
|
||||
void
|
||||
emplace(F&& f);
|
||||
|
||||
template<class F>
|
||||
void
|
||||
save(F&& f);
|
||||
emplace(CompletionHandler&& handler);
|
||||
|
||||
explicit
|
||||
operator bool() const
|
||||
{
|
||||
return base_ != nullptr;
|
||||
return h_ != nullptr;
|
||||
}
|
||||
|
||||
bool
|
||||
maybe_invoke()
|
||||
{
|
||||
if(base_)
|
||||
if(h_)
|
||||
{
|
||||
auto const basep = base_;
|
||||
base_ = nullptr;
|
||||
(*basep)();
|
||||
auto const h = h_;
|
||||
h_ = nullptr;
|
||||
h->invoke();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template<class F>
|
||||
template<class CompletionHandler>
|
||||
void
|
||||
pausation::emplace(F&& f)
|
||||
pausation::emplace(CompletionHandler&& handler)
|
||||
{
|
||||
using type = holder<typename std::decay<F>::type>;
|
||||
static_assert(sizeof(buf_type) >= sizeof(type),
|
||||
"buffer too small");
|
||||
BOOST_ASSERT(! base_);
|
||||
base_ = ::new(buf_) type{std::forward<F>(f)};
|
||||
}
|
||||
|
||||
template<class F>
|
||||
void
|
||||
pausation::save(F&& f)
|
||||
{
|
||||
emplace(saved_op<F>{std::move(f)});
|
||||
BOOST_ASSERT(! h_);
|
||||
typename beast::detail::allocator_traits<
|
||||
boost::asio::associated_allocator_t<
|
||||
CompletionHandler>>::template rebind_alloc<
|
||||
impl<CompletionHandler>> alloc{
|
||||
boost::asio::get_associated_allocator(handler)};
|
||||
using A = decltype(alloc);
|
||||
auto const d =
|
||||
[&alloc](impl<CompletionHandler>* p)
|
||||
{
|
||||
beast::detail::allocator_traits<A>::deallocate(alloc, p, 1);
|
||||
};
|
||||
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
|
||||
|
@ -152,7 +152,7 @@ operator()(
|
||||
// Suspend
|
||||
BOOST_ASSERT(ws_.rd_block_ != tok_);
|
||||
BOOST_ASIO_CORO_YIELD
|
||||
ws_.paused_r_rd_.save(std::move(*this));
|
||||
ws_.paused_r_rd_.emplace(std::move(*this));
|
||||
|
||||
// Acquire the read block
|
||||
BOOST_ASSERT(! ws_.rd_block_);
|
||||
@ -275,7 +275,7 @@ operator()(
|
||||
// Suspend
|
||||
BOOST_ASSERT(ws_.wr_block_ != tok_);
|
||||
BOOST_ASIO_CORO_YIELD
|
||||
ws_.paused_rd_.save(std::move(*this));
|
||||
ws_.paused_rd_.emplace(std::move(*this));
|
||||
|
||||
// Acquire the write block
|
||||
BOOST_ASSERT(! ws_.wr_block_);
|
||||
@ -580,7 +580,7 @@ operator()(
|
||||
// Suspend
|
||||
BOOST_ASSERT(ws_.wr_block_ != tok_);
|
||||
BOOST_ASIO_CORO_YIELD
|
||||
ws_.paused_rd_.save(std::move(*this));
|
||||
ws_.paused_rd_.emplace(std::move(*this));
|
||||
|
||||
// Acquire the write block
|
||||
BOOST_ASSERT(! ws_.wr_block_);
|
||||
|
@ -208,7 +208,7 @@ operator()(
|
||||
// Suspend
|
||||
BOOST_ASSERT(ws_.wr_block_ != tok_);
|
||||
BOOST_ASIO_CORO_YIELD
|
||||
ws_.paused_wr_.save(std::move(*this));
|
||||
ws_.paused_wr_.emplace(std::move(*this));
|
||||
|
||||
// Acquire the write block
|
||||
BOOST_ASSERT(! ws_.wr_block_);
|
||||
|
Loading…
Reference in New Issue
Block a user