2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-28 18:52:09 +00:00

Add bind_allocator.

This commit is contained in:
Christopher Kohlhoff
2022-03-02 21:27:28 +11:00
parent 9eb306115d
commit 3cd04eee90
8 changed files with 999 additions and 93 deletions

View File

@@ -112,46 +112,6 @@ private:
handler_memory& memory_;
};
// Wrapper class template for handler objects to allow handler memory
// allocation to be customised. The allocator_type type and get_allocator()
// member function are used by the asynchronous operations to obtain the
// allocator. Calls to operator() are forwarded to the encapsulated handler.
template <typename Handler>
class custom_alloc_handler
{
public:
using allocator_type = handler_allocator<Handler>;
custom_alloc_handler(handler_memory& m, Handler h)
: memory_(m),
handler_(h)
{
}
allocator_type get_allocator() const noexcept
{
return allocator_type(memory_);
}
template <typename ...Args>
void operator()(Args&&... args)
{
handler_(std::forward<Args>(args)...);
}
private:
handler_memory& memory_;
Handler handler_;
};
// Helper function to wrap a handler object to add custom allocation.
template <typename Handler>
inline custom_alloc_handler<Handler> make_custom_alloc_handler(
handler_memory& m, Handler h)
{
return custom_alloc_handler<Handler>(m, h);
}
class session
: public std::enable_shared_from_this<session>
{
@@ -171,7 +131,8 @@ private:
{
auto self(shared_from_this());
socket_.async_read_some(boost::asio::buffer(data_),
make_custom_alloc_handler(handler_memory_,
boost::asio::bind_allocator(
handler_allocator<int>(handler_memory_),
[this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
@@ -185,7 +146,8 @@ private:
{
auto self(shared_from_this());
boost::asio::async_write(socket_, boost::asio::buffer(data_, length),
make_custom_alloc_handler(handler_memory_,
boost::asio::bind_allocator(
handler_allocator<int>(handler_memory_),
[this, self](boost::system::error_code ec, std::size_t /*length*/)
{
if (!ec)