diff options
Diffstat (limited to '3rdparty/asio/src/examples/cpp11/allocation')
-rw-r--r-- | 3rdparty/asio/src/examples/cpp11/allocation/.gitignore | 10 | ||||
-rw-r--r-- | 3rdparty/asio/src/examples/cpp11/allocation/server.cpp | 76 |
2 files changed, 40 insertions, 46 deletions
diff --git a/3rdparty/asio/src/examples/cpp11/allocation/.gitignore b/3rdparty/asio/src/examples/cpp11/allocation/.gitignore deleted file mode 100644 index 180ba886e58..00000000000 --- a/3rdparty/asio/src/examples/cpp11/allocation/.gitignore +++ /dev/null @@ -1,10 +0,0 @@ -.deps -.dirstamp -*.o -*.obj -*.exe -server -*.ilk -*.manifest -*.pdb -*.tds diff --git a/3rdparty/asio/src/examples/cpp11/allocation/server.cpp b/3rdparty/asio/src/examples/cpp11/allocation/server.cpp index cbcbe76fe68..48bc119c1c5 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-2024 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,49 +66,51 @@ private: bool in_use_; }; -// Wrapper class template for handler objects to allow handler memory -// allocation to be customised. Calls to operator() are forwarded to the -// encapsulated handler. -template <typename Handler> -class custom_alloc_handler +// 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: - custom_alloc_handler(handler_allocator& a, Handler h) - : allocator_(a), - handler_(h) + using value_type = T; + + explicit handler_allocator(handler_memory& mem) + : memory_(mem) { } - template <typename ...Args> - void operator()(Args&&... args) + template <typename U> + handler_allocator(const handler_allocator<U>& other) noexcept + : memory_(other.memory_) { - handler_(std::forward<Args>(args)...); } - friend void* asio_handler_allocate(std::size_t size, - custom_alloc_handler<Handler>* this_handler) + bool operator==(const handler_allocator& other) const noexcept { - return this_handler->allocator_.allocate(size); + return &memory_ == &other.memory_; } - friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/, - custom_alloc_handler<Handler>* this_handler) + bool operator!=(const handler_allocator& other) const noexcept { - this_handler->allocator_.deallocate(pointer); + 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: - handler_allocator& allocator_; - Handler handler_; -}; + template <typename> friend class handler_allocator; -// 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) -{ - return custom_alloc_handler<Handler>(a, h); -} + // The underlying memory. + handler_memory& memory_; +}; class session : public std::enable_shared_from_this<session> @@ -129,7 +131,8 @@ private: { auto self(shared_from_this()); socket_.async_read_some(asio::buffer(data_), - make_custom_alloc_handler(allocator_, + asio::bind_allocator( + handler_allocator<int>(handler_memory_), [this, self](std::error_code ec, std::size_t length) { if (!ec) @@ -143,7 +146,8 @@ private: { auto self(shared_from_this()); asio::async_write(socket_, asio::buffer(data_, length), - make_custom_alloc_handler(allocator_, + asio::bind_allocator( + handler_allocator<int>(handler_memory_), [this, self](std::error_code ec, std::size_t /*length*/) { if (!ec) @@ -159,8 +163,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 |