]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/asio/example/cpp11/allocation/server.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp11 / allocation / server.cpp
index 6084ae80fdd70ffb18c31c6338584f5f6f640d58..5be7508dcc6e0cbd4aa54a7e9e8bc8b68abe9a25 100644 (file)
@@ -2,7 +2,7 @@
 // server.cpp
 // ~~~~~~~~~~
 //
-// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+// Copyright (c) 2003-2017 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 boost::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(boost::asio::buffer(data_),
-        make_custom_alloc_handler(allocator_,
+        make_custom_alloc_handler(handler_memory_,
           [this, self](boost::system::error_code ec, std::size_t length)
           {
             if (!ec)
@@ -143,7 +185,7 @@ private:
   {
     auto self(shared_from_this());
     boost::asio::async_write(socket_, boost::asio::buffer(data_, length),
-        make_custom_alloc_handler(allocator_,
+        make_custom_alloc_handler(handler_memory_,
           [this, self](boost::system::error_code ec, std::size_t /*length*/)
           {
             if (!ec)
@@ -159,16 +201,15 @@ 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
 {
 public:
-  server(boost::asio::io_service& io_service, short port)
-    : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
-      socket_(io_service)
+  server(boost::asio::io_context& io_context, short port)
+    : acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
   {
     do_accept();
   }
@@ -176,12 +217,12 @@ public:
 private:
   void do_accept()
   {
-    acceptor_.async_accept(socket_,
-        [this](boost::system::error_code ec)
+    acceptor_.async_accept(
+        [this](boost::system::error_code ec, tcp::socket socket)
         {
           if (!ec)
           {
-            std::make_shared<session>(std::move(socket_))->start();
+            std::make_shared<session>(std::move(socket))->start();
           }
 
           do_accept();
@@ -189,7 +230,6 @@ private:
   }
 
   tcp::acceptor acceptor_;
-  tcp::socket socket_;
 };
 
 int main(int argc, char* argv[])
@@ -202,9 +242,9 @@ int main(int argc, char* argv[])
       return 1;
     }
 
-    boost::asio::io_service io_service;
-    server s(io_service, std::atoi(argv[1]));
-    io_service.run();
+    boost::asio::io_context io_context;
+    server s(io_context, std::atoi(argv[1]));
+    io_context.run();
   }
   catch (std::exception& e)
   {