summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp11/allocation/server.cpp
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2021-11-15 04:15:13 +1100
committer Vas Crabb <vas@vastheman.com>2021-11-15 04:15:13 +1100
commit44ec6d2e0ee569202b75b73eea40972595866779 (patch)
treeb286443c264704198789514e0594dc24b3ca9f96 /3rdparty/asio/src/examples/cpp11/allocation/server.cpp
parent137f254b918f1f1ebee43dfbed86793b0dae73eb (diff)
3rdparty: Updated ASIO to version 1.20.0.
The doc folder isn't included as it's pretty big. This required include/asio/detail/win_iocp_socket_accept_op.hpp due to mismatched order in the member declarations and initialiser list for the win_iocp_socket_accept_op class. I reversed the declaration order so it matches win_iocp_socket_move_accept_op.
Diffstat (limited to '3rdparty/asio/src/examples/cpp11/allocation/server.cpp')
-rw-r--r--3rdparty/asio/src/examples/cpp11/allocation/server.cpp98
1 files changed, 70 insertions, 28 deletions
diff --git a/3rdparty/asio/src/examples/cpp11/allocation/server.cpp b/3rdparty/asio/src/examples/cpp11/allocation/server.cpp
index cbcbe76fe68..4d0d80e6875 100644
--- a/3rdparty/asio/src/examples/cpp11/allocation/server.cpp
+++ b/3rdparty/asio/src/examples/cpp11/allocation/server.cpp
@@ -2,7 +2,7 @@
// server.cpp
// ~~~~~~~~~~
//
-// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+// Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff 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)
@@ -22,16 +22,16 @@ using asio::ip::tcp;
// It contains a single block of memory which may be returned for allocation
// requests. If the memory is in use when an allocation request is made, the
// allocator delegates allocation to the global heap.
-class handler_allocator
+class handler_memory
{
public:
- handler_allocator()
+ handler_memory()
: in_use_(false)
{
}
- handler_allocator(const handler_allocator&) = delete;
- handler_allocator& operator=(const handler_allocator&) = delete;
+ handler_memory(const handler_memory&) = delete;
+ handler_memory& operator=(const handler_memory&) = delete;
void* allocate(std::size_t size)
{
@@ -66,48 +66,90 @@ private:
bool in_use_;
};
+// The allocator to be associated with the handler objects. This allocator only
+// needs to satisfy the C++11 minimal allocator requirements.
+template <typename T>
+class handler_allocator
+{
+public:
+ using value_type = T;
+
+ explicit handler_allocator(handler_memory& mem)
+ : memory_(mem)
+ {
+ }
+
+ template <typename U>
+ handler_allocator(const handler_allocator<U>& other) noexcept
+ : memory_(other.memory_)
+ {
+ }
+
+ bool operator==(const handler_allocator& other) const noexcept
+ {
+ return &memory_ == &other.memory_;
+ }
+
+ bool operator!=(const handler_allocator& other) const noexcept
+ {
+ return &memory_ != &other.memory_;
+ }
+
+ T* allocate(std::size_t n) const
+ {
+ return static_cast<T*>(memory_.allocate(sizeof(T) * n));
+ }
+
+ void deallocate(T* p, std::size_t /*n*/) const
+ {
+ return memory_.deallocate(p);
+ }
+
+private:
+ template <typename> friend class handler_allocator;
+
+ // The underlying memory.
+ handler_memory& memory_;
+};
+
// Wrapper class template for handler objects to allow handler memory
-// allocation to be customised. Calls to operator() are forwarded to the
-// encapsulated handler.
+// 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:
- custom_alloc_handler(handler_allocator& a, Handler h)
- : allocator_(a),
- handler_(h)
- {
- }
+ using allocator_type = handler_allocator<Handler>;
- template <typename ...Args>
- void operator()(Args&&... args)
+ custom_alloc_handler(handler_memory& m, Handler h)
+ : memory_(m),
+ handler_(h)
{
- handler_(std::forward<Args>(args)...);
}
- friend void* asio_handler_allocate(std::size_t size,
- custom_alloc_handler<Handler>* this_handler)
+ allocator_type get_allocator() const noexcept
{
- return this_handler->allocator_.allocate(size);
+ return allocator_type(memory_);
}
- friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/,
- custom_alloc_handler<Handler>* this_handler)
+ template <typename ...Args>
+ void operator()(Args&&... args)
{
- this_handler->allocator_.deallocate(pointer);
+ handler_(std::forward<Args>(args)...);
}
private:
- handler_allocator& allocator_;
+ 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_allocator& a, Handler h)
+ handler_memory& m, Handler h)
{
- return custom_alloc_handler<Handler>(a, h);
+ return custom_alloc_handler<Handler>(m, h);
}
class session
@@ -129,7 +171,7 @@ private:
{
auto self(shared_from_this());
socket_.async_read_some(asio::buffer(data_),
- make_custom_alloc_handler(allocator_,
+ make_custom_alloc_handler(handler_memory_,
[this, self](std::error_code ec, std::size_t length)
{
if (!ec)
@@ -143,7 +185,7 @@ private:
{
auto self(shared_from_this());
asio::async_write(socket_, asio::buffer(data_, length),
- make_custom_alloc_handler(allocator_,
+ make_custom_alloc_handler(handler_memory_,
[this, self](std::error_code ec, std::size_t /*length*/)
{
if (!ec)
@@ -159,8 +201,8 @@ private:
// Buffer used to store data received from the client.
std::array<char, 1024> data_;
- // The allocator to use for handler-based custom memory allocation.
- handler_allocator allocator_;
+ // The memory to use for handler-based custom memory allocation.
+ handler_memory handler_memory_;
};
class server