diff options
author | 2021-11-15 04:15:13 +1100 | |
---|---|---|
committer | 2021-11-15 04:15:13 +1100 | |
commit | 44ec6d2e0ee569202b75b73eea40972595866779 (patch) | |
tree | b286443c264704198789514e0594dc24b3ca9f96 /3rdparty/asio/src/examples/cpp03/allocation/server.cpp | |
parent | 137f254b918f1f1ebee43dfbed86793b0dae73eb (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/cpp03/allocation/server.cpp')
-rw-r--r-- | 3rdparty/asio/src/examples/cpp03/allocation/server.cpp | 103 |
1 files changed, 75 insertions, 28 deletions
diff --git a/3rdparty/asio/src/examples/cpp03/allocation/server.cpp b/3rdparty/asio/src/examples/cpp03/allocation/server.cpp index e26eb564918..ca69460ff14 100644 --- a/3rdparty/asio/src/examples/cpp03/allocation/server.cpp +++ b/3rdparty/asio/src/examples/cpp03/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) @@ -12,7 +12,7 @@ #include <iostream> #include <boost/aligned_storage.hpp> #include <boost/array.hpp> -#include <boost/bind.hpp> +#include <boost/bind/bind.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/noncopyable.hpp> #include <boost/shared_ptr.hpp> @@ -24,11 +24,11 @@ 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 : private boost::noncopyable { public: - handler_allocator() + handler_memory() : in_use_(false) { } @@ -66,19 +66,78 @@ 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, plus rebind when +// targeting C++03. +template <typename T> +class handler_allocator +{ +public: + typedef T value_type; + + explicit handler_allocator(handler_memory& mem) + : memory_(mem) + { + } + + template <typename U> + handler_allocator(const handler_allocator<U>& other) + : memory_(other.memory_) + { + } + + template <typename U> + struct rebind + { + typedef handler_allocator<U> other; + }; + + bool operator==(const handler_allocator& other) const + { + return &memory_ == &other.memory_; + } + + bool operator!=(const handler_allocator& other) const + { + 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: + // 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 typedef 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), + typedef handler_allocator<Handler> allocator_type; + + custom_alloc_handler(handler_memory& m, Handler h) + : memory_(m), handler_(h) { } + allocator_type get_allocator() const + { + return allocator_type(memory_); + } + template <typename Arg1> void operator()(Arg1 arg1) { @@ -91,29 +150,17 @@ public: handler_(arg1, arg2); } - friend void* asio_handler_allocate(std::size_t size, - custom_alloc_handler<Handler>* this_handler) - { - return this_handler->allocator_.allocate(size); - } - - friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/, - custom_alloc_handler<Handler>* this_handler) - { - this_handler->allocator_.deallocate(pointer); - } - 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 @@ -133,7 +180,7 @@ public: void start() { socket_.async_read_some(asio::buffer(data_), - make_custom_alloc_handler(allocator_, + make_custom_alloc_handler(handler_memory_, boost::bind(&session::handle_read, shared_from_this(), asio::placeholders::error, @@ -147,7 +194,7 @@ public: { asio::async_write(socket_, asio::buffer(data_, bytes_transferred), - make_custom_alloc_handler(allocator_, + make_custom_alloc_handler(handler_memory_, boost::bind(&session::handle_write, shared_from_this(), asio::placeholders::error))); @@ -159,7 +206,7 @@ public: if (!error) { socket_.async_read_some(asio::buffer(data_), - make_custom_alloc_handler(allocator_, + make_custom_alloc_handler(handler_memory_, boost::bind(&session::handle_read, shared_from_this(), asio::placeholders::error, @@ -174,8 +221,8 @@ private: // Buffer used to store data received from the client. boost::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_; }; typedef boost::shared_ptr<session> session_ptr; |