forked from boostorg/beast
Update for Net-TS Asio (API Change):
fix #769 The following classes are removed: * handler_type * async_result * async_completion * is_dynamic_buffer * is_const_buffer_sequence * is_mutable_buffer_sequence * handler_alloc Actions Required: * Use BOOST_ASIO_HANDLER_TYPE instead of handler_type * Use BOOST_ASIO_INITFN_RESULT_TYPE instead of async_result * Use boost::asio::async_completion * Use boost::asio::is_dynamic_buffer * Use boost::asio::is_const_buffer_sequence * Use boost::asio::is_mutable_buffer_sequence * boost::asio::associated_allocator_t replaces handler_alloc
This commit is contained in:
@@ -10,21 +10,21 @@
|
||||
#ifndef BOOST_BEAST_EXAMPLE_COMMON_SESSION_ALLOC_HPP
|
||||
#define BOOST_BEAST_EXAMPLE_COMMON_SESSION_ALLOC_HPP
|
||||
|
||||
#include <boost/asio/handler_alloc_hook.hpp>
|
||||
#include <boost/asio/associated_allocator.hpp>
|
||||
#include <boost/asio/associated_executor.hpp>
|
||||
#include <boost/asio/handler_continuation_hook.hpp>
|
||||
#include <boost/asio/handler_invoke_hook.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/intrusive/list.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class Context>
|
||||
class session_alloc_base
|
||||
{
|
||||
template<class Handler>
|
||||
class wrapped_handler;
|
||||
|
||||
protected:
|
||||
class pool_t
|
||||
{
|
||||
using hook_type =
|
||||
@@ -78,23 +78,18 @@ class session_alloc_base
|
||||
boost::intrusive::constant_time_size<
|
||||
true>>::type;
|
||||
|
||||
Context* ctx_;
|
||||
std::size_t refs_ = 1; // shared count
|
||||
std::size_t high_ = 0; // highest used
|
||||
std::size_t size_ = 0; // size of buf_
|
||||
char* buf_ = nullptr; // a large block
|
||||
list_type list_; // list of allocations
|
||||
|
||||
explicit
|
||||
pool_t(Context* ctx)
|
||||
: ctx_(ctx)
|
||||
{
|
||||
}
|
||||
pool_t() = default;
|
||||
|
||||
public:
|
||||
static
|
||||
pool_t&
|
||||
construct(Context* ctx);
|
||||
construct();
|
||||
|
||||
~pool_t();
|
||||
|
||||
@@ -110,73 +105,178 @@ class session_alloc_base
|
||||
void
|
||||
dealloc(void* pv, std::size_t n);
|
||||
};
|
||||
|
||||
pool_t& pool_;
|
||||
|
||||
public:
|
||||
session_alloc_base& operator=(session_alloc_base const&) = delete;
|
||||
|
||||
~session_alloc_base()
|
||||
{
|
||||
pool_.release();
|
||||
}
|
||||
|
||||
session_alloc_base()
|
||||
: pool_(pool_t::construct(nullptr))
|
||||
{
|
||||
static_assert(std::is_same<Context, std::nullptr_t>::value,
|
||||
"Context requirements not met");
|
||||
}
|
||||
|
||||
session_alloc_base(session_alloc_base const& other)
|
||||
: pool_(other.pool_.addref())
|
||||
{
|
||||
}
|
||||
|
||||
template<class DeducedContext, class = typename
|
||||
std::enable_if<! std::is_same<
|
||||
session_alloc_base<Context>,
|
||||
typename std::decay<DeducedContext>::type
|
||||
>::value>::type>
|
||||
explicit
|
||||
session_alloc_base(DeducedContext& ctx)
|
||||
: pool_(pool_t::construct(std::addressof(ctx)))
|
||||
{
|
||||
static_assert(! std::is_same<Context, std::nullptr_t>::value,
|
||||
"Context requirements not met");
|
||||
}
|
||||
|
||||
template<class Handler>
|
||||
wrapped_handler<typename std::decay<Handler>::type>
|
||||
wrap(Handler&& handler)
|
||||
{
|
||||
return wrapped_handler<
|
||||
typename std::decay<Handler>::type>(
|
||||
std::forward<Handler>(handler), *this);
|
||||
}
|
||||
|
||||
protected:
|
||||
void*
|
||||
alloc(std::size_t n)
|
||||
{
|
||||
return pool_.alloc(n);
|
||||
}
|
||||
|
||||
void
|
||||
dealloc(void* p, std::size_t n)
|
||||
{
|
||||
pool_.dealloc(p, n);
|
||||
}
|
||||
};
|
||||
|
||||
template<class Context>
|
||||
auto
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
construct() ->
|
||||
pool_t&
|
||||
{
|
||||
return *(new pool_t);
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
~pool_t()
|
||||
{
|
||||
BOOST_ASSERT(list_.size() == 0);
|
||||
if(buf_)
|
||||
delete[] buf_;
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
auto
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
addref() ->
|
||||
pool_t&
|
||||
{
|
||||
++refs_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
void
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
release()
|
||||
{
|
||||
if(--refs_)
|
||||
return;
|
||||
delete this;
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
void*
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
alloc(std::size_t n)
|
||||
{
|
||||
if(list_.empty() && size_ < high_)
|
||||
{
|
||||
if(buf_)
|
||||
delete[] buf_;
|
||||
buf_ = new char[high_];
|
||||
size_ = high_;
|
||||
}
|
||||
if(buf_)
|
||||
{
|
||||
char* end;
|
||||
std::size_t used;
|
||||
if(list_.empty())
|
||||
{
|
||||
end = buf_;
|
||||
used = sizeof(element) + n;
|
||||
}
|
||||
else
|
||||
{
|
||||
end = list_.back().end();
|
||||
used = list_.back().used() +
|
||||
sizeof(element) + n;
|
||||
}
|
||||
if(end >= buf_ && end +
|
||||
sizeof(element) + n <= buf_ + size_)
|
||||
{
|
||||
auto& e = *new(end) element{n, used};
|
||||
list_.push_back(e);
|
||||
high_ = (std::max)(high_, used);
|
||||
return e.data();
|
||||
}
|
||||
}
|
||||
std::size_t const used =
|
||||
sizeof(element) + n + (
|
||||
buf_ && ! list_.empty() ?
|
||||
list_.back().used() : 0);
|
||||
auto& e = *new(new char[sizeof(element) + n]) element{n, used};
|
||||
list_.push_back(e);
|
||||
high_ = (std::max)(high_, used);
|
||||
return e.data();
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
void
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
dealloc(void* pv, std::size_t n)
|
||||
{
|
||||
auto& e = *(reinterpret_cast<element*>(pv) - 1);
|
||||
BOOST_ASSERT(e.size() == n);
|
||||
if( (e.end() > buf_ + size_) ||
|
||||
reinterpret_cast<char*>(&e) < buf_)
|
||||
{
|
||||
list_.erase(list_.iterator_to(e));
|
||||
e.~element();
|
||||
delete[] reinterpret_cast<char*>(&e);
|
||||
return;
|
||||
}
|
||||
list_.erase(list_.iterator_to(e));
|
||||
e.~element();
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class T, class Context = std::nullptr_t>
|
||||
class session_alloc : public session_alloc_base<Context>
|
||||
template<class T>
|
||||
class session_alloc
|
||||
: private detail::session_alloc_base<void>
|
||||
{
|
||||
template<class U, class C>
|
||||
template<class U>
|
||||
friend class session_alloc;
|
||||
|
||||
template<class Handler>
|
||||
class wrapped_handler
|
||||
{
|
||||
// Can't friend partial specializations,
|
||||
// so we just friend the whole thing.
|
||||
template<class T, class Executor>
|
||||
friend struct boost::asio::associated_executor;
|
||||
|
||||
Handler h_;
|
||||
session_alloc<char> alloc_;
|
||||
|
||||
public:
|
||||
wrapped_handler(wrapped_handler&&) = default;
|
||||
wrapped_handler(wrapped_handler const&) = default;
|
||||
|
||||
template<class DeducedHandler>
|
||||
wrapped_handler(
|
||||
DeducedHandler&& h,
|
||||
session_alloc const& alloc)
|
||||
: h_(std::forward<DeducedHandler>(h))
|
||||
, alloc_(alloc)
|
||||
{
|
||||
}
|
||||
|
||||
using allocator_type = session_alloc<char>;
|
||||
|
||||
allocator_type
|
||||
get_allocator() const noexcept;
|
||||
|
||||
template<class... Args>
|
||||
void
|
||||
operator()(Args&&... args) const
|
||||
{
|
||||
h_(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
friend
|
||||
bool
|
||||
asio_handler_is_continuation(wrapped_handler* w)
|
||||
{
|
||||
using boost::asio::asio_handler_is_continuation;
|
||||
return asio_handler_is_continuation(std::addressof(w->h_));
|
||||
}
|
||||
};
|
||||
|
||||
using pool_t = typename
|
||||
detail::session_alloc_base<void>::pool_t;
|
||||
|
||||
pool_t& pool_;
|
||||
|
||||
public:
|
||||
using value_type = T;
|
||||
using is_always_equal = std::false_type;
|
||||
@@ -193,21 +293,32 @@ public:
|
||||
using other = session_alloc<U>;
|
||||
};
|
||||
|
||||
session_alloc() = default;
|
||||
session_alloc(session_alloc const&) = default;
|
||||
session_alloc& operator=(session_alloc const&) = delete;
|
||||
|
||||
~session_alloc()
|
||||
{
|
||||
pool_.release();
|
||||
}
|
||||
|
||||
session_alloc()
|
||||
: pool_(pool_t::construct())
|
||||
{
|
||||
}
|
||||
|
||||
session_alloc(session_alloc const& other) noexcept
|
||||
: pool_(other.pool_.addref())
|
||||
{
|
||||
}
|
||||
|
||||
template<class U>
|
||||
session_alloc(session_alloc<U> const& other)
|
||||
: session_alloc_base<Context>(static_cast<
|
||||
session_alloc_base<Context> const&>(other))
|
||||
session_alloc(session_alloc<U> const& other) noexcept
|
||||
: pool_(other.pool_)
|
||||
{
|
||||
}
|
||||
|
||||
explicit
|
||||
session_alloc(Context& ctx)
|
||||
: session_alloc_base<Context>(ctx)
|
||||
{
|
||||
}
|
||||
template<class Handler>
|
||||
wrapped_handler<typename std::decay<Handler>::type>
|
||||
wrap(Handler&& handler);
|
||||
|
||||
value_type*
|
||||
allocate(size_type n)
|
||||
@@ -257,209 +368,67 @@ public:
|
||||
{
|
||||
return ! (lhs == rhs);
|
||||
}
|
||||
|
||||
protected:
|
||||
void*
|
||||
alloc(std::size_t n)
|
||||
{
|
||||
return pool_.alloc(n);
|
||||
}
|
||||
|
||||
void
|
||||
dealloc(void* p, std::size_t n)
|
||||
{
|
||||
pool_.dealloc(p, n);
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class Context>
|
||||
template<class T>
|
||||
template<class Handler>
|
||||
class session_alloc_base<Context>::wrapped_handler
|
||||
auto
|
||||
session_alloc<T>::
|
||||
wrapped_handler<Handler>::
|
||||
get_allocator() const noexcept ->
|
||||
allocator_type
|
||||
{
|
||||
Handler h_;
|
||||
session_alloc_base alloc_;
|
||||
|
||||
void*
|
||||
alloc(std::size_t size)
|
||||
{
|
||||
return alloc_.alloc(size);
|
||||
}
|
||||
|
||||
void
|
||||
dealloc(void* p, std::size_t size)
|
||||
{
|
||||
alloc_.dealloc(p, size);
|
||||
}
|
||||
|
||||
public:
|
||||
wrapped_handler(wrapped_handler&&) = default;
|
||||
wrapped_handler(wrapped_handler const&) = default;
|
||||
|
||||
template<class DeducedHandler>
|
||||
explicit
|
||||
wrapped_handler(DeducedHandler&& h,
|
||||
session_alloc_base const& alloc)
|
||||
: h_(std::forward<DeducedHandler>(h))
|
||||
, alloc_(alloc)
|
||||
{
|
||||
}
|
||||
|
||||
template<class... Args>
|
||||
void
|
||||
operator()(Args&&... args) const
|
||||
{
|
||||
h_(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
friend
|
||||
void*
|
||||
asio_handler_allocate(
|
||||
std::size_t size, wrapped_handler* w)
|
||||
{
|
||||
return w->alloc(size);
|
||||
}
|
||||
|
||||
friend
|
||||
void
|
||||
asio_handler_deallocate(
|
||||
void* p, std::size_t size, wrapped_handler* w)
|
||||
{
|
||||
w->dealloc(p, size);
|
||||
}
|
||||
|
||||
friend
|
||||
bool
|
||||
asio_handler_is_continuation(wrapped_handler* w)
|
||||
{
|
||||
using boost::asio::asio_handler_is_continuation;
|
||||
return asio_handler_is_continuation(std::addressof(w->h_));
|
||||
}
|
||||
|
||||
template<class F>
|
||||
friend
|
||||
void
|
||||
asio_handler_invoke(F&& f, wrapped_handler* w)
|
||||
{
|
||||
using boost::asio::asio_handler_invoke;
|
||||
asio_handler_invoke(
|
||||
f, std::addressof(w->h_));
|
||||
}
|
||||
};
|
||||
return alloc_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template<class Context>
|
||||
template<class T>
|
||||
template<class Handler>
|
||||
auto
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
construct(Context* ctx) ->
|
||||
pool_t&
|
||||
session_alloc<T>::
|
||||
wrap(Handler&& handler) ->
|
||||
wrapped_handler<typename std::decay<Handler>::type>
|
||||
{
|
||||
using boost::asio::asio_handler_allocate;
|
||||
return *new(asio_handler_allocate(
|
||||
sizeof(pool_t), ctx)) pool_t{ctx};
|
||||
return wrapped_handler<
|
||||
typename std::decay<Handler>::type>(
|
||||
std::forward<Handler>(handler), *this);
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
~pool_t()
|
||||
namespace boost {
|
||||
namespace asio {
|
||||
template<class T, class Handler, class Executor>
|
||||
struct associated_executor<
|
||||
session_alloc<T>::wrapped_handler<Handler>, Executor>
|
||||
{
|
||||
BOOST_ASSERT(list_.size() == 0);
|
||||
if(buf_)
|
||||
using type = typename
|
||||
associated_executor<Handler, Executor>::type;
|
||||
|
||||
static
|
||||
type
|
||||
get(session_alloc<T>::wrapped_handler<Handler> const& h,
|
||||
Executor const& ex = Executor()) noexcept
|
||||
{
|
||||
using boost::asio::asio_handler_deallocate;
|
||||
asio_handler_deallocate(buf_, size_, ctx_);
|
||||
return associated_executor<
|
||||
Handler, Executor>::get(h.h_, ex);
|
||||
}
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
auto
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
addref() ->
|
||||
pool_t&
|
||||
{
|
||||
++refs_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
void
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
release()
|
||||
{
|
||||
if(--refs_)
|
||||
return;
|
||||
this->~pool_t();
|
||||
using boost::asio::asio_handler_deallocate;
|
||||
asio_handler_deallocate(this, sizeof(*this), ctx_);
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
void*
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
alloc(std::size_t n)
|
||||
{
|
||||
if(list_.empty() && size_ < high_)
|
||||
{
|
||||
if(buf_)
|
||||
{
|
||||
using boost::asio::asio_handler_deallocate;
|
||||
asio_handler_deallocate(buf_, size_, ctx_);
|
||||
}
|
||||
using boost::asio::asio_handler_allocate;
|
||||
buf_ = reinterpret_cast<char*>(
|
||||
asio_handler_allocate(high_, ctx_));
|
||||
size_ = high_;
|
||||
}
|
||||
if(buf_)
|
||||
{
|
||||
char* end;
|
||||
std::size_t used;
|
||||
if(list_.empty())
|
||||
{
|
||||
end = buf_;
|
||||
used = sizeof(element) + n;
|
||||
}
|
||||
else
|
||||
{
|
||||
end = list_.back().end();
|
||||
used = list_.back().used() +
|
||||
sizeof(element) + n;
|
||||
}
|
||||
if(end >= buf_ && end +
|
||||
sizeof(element) + n <= buf_ + size_)
|
||||
{
|
||||
auto& e = *new(end) element{n, used};
|
||||
list_.push_back(e);
|
||||
high_ = (std::max)(high_, used);
|
||||
return e.data();
|
||||
}
|
||||
}
|
||||
std::size_t const used =
|
||||
sizeof(element) + n + (
|
||||
buf_ && ! list_.empty() ?
|
||||
list_.back().used() : 0);
|
||||
using boost::asio::asio_handler_allocate;
|
||||
auto& e = *new(asio_handler_allocate(
|
||||
sizeof(element) + n, ctx_)) element{n, used};
|
||||
list_.push_back(e);
|
||||
high_ = (std::max)(high_, used);
|
||||
return e.data();
|
||||
}
|
||||
|
||||
template<class Context>
|
||||
void
|
||||
session_alloc_base<Context>::
|
||||
pool_t::
|
||||
dealloc(void* pv, std::size_t n)
|
||||
{
|
||||
auto& e = *(reinterpret_cast<element*>(pv) - 1);
|
||||
BOOST_ASSERT(e.size() == n);
|
||||
if( (e.end() > buf_ + size_) ||
|
||||
reinterpret_cast<char*>(&e) < buf_)
|
||||
{
|
||||
list_.erase(list_.iterator_to(e));
|
||||
e.~element();
|
||||
using boost::asio::asio_handler_deallocate;
|
||||
asio_handler_deallocate(
|
||||
&e, sizeof(e) + n, ctx_);
|
||||
return;
|
||||
}
|
||||
list_.erase(list_.iterator_to(e));
|
||||
e.~element();
|
||||
}
|
||||
};
|
||||
} // asio
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user