]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/asio/example/cpp03/allocation/server.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / allocation / server.cpp
index 04c0e754c926e18301db2565164195025f924a77..cb4cbcd472437f90a820d0cf1d9a2ceb142d87d2 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)
@@ -24,11 +24,11 @@ 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
   : 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,37 +150,25 @@ 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
   : public boost::enable_shared_from_this<session>
 {
 public:
-  session(boost::asio::io_service& io_service)
-    : socket_(io_service)
+  session(boost::asio::io_context& io_context)
+    : socket_(io_context)
   {
   }
 
@@ -133,7 +180,7 @@ public:
   void start()
   {
     socket_.async_read_some(boost::asio::buffer(data_),
-        make_custom_alloc_handler(allocator_,
+        make_custom_alloc_handler(handler_memory_,
           boost::bind(&session::handle_read,
             shared_from_this(),
             boost::asio::placeholders::error,
@@ -147,7 +194,7 @@ public:
     {
       boost::asio::async_write(socket_,
           boost::asio::buffer(data_, bytes_transferred),
-          make_custom_alloc_handler(allocator_,
+          make_custom_alloc_handler(handler_memory_,
             boost::bind(&session::handle_write,
               shared_from_this(),
               boost::asio::placeholders::error)));
@@ -159,7 +206,7 @@ public:
     if (!error)
     {
       socket_.async_read_some(boost::asio::buffer(data_),
-          make_custom_alloc_handler(allocator_,
+          make_custom_alloc_handler(handler_memory_,
             boost::bind(&session::handle_read,
               shared_from_this(),
               boost::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;
@@ -183,11 +230,11 @@ typedef boost::shared_ptr<session> session_ptr;
 class server
 {
 public:
-  server(boost::asio::io_service& io_service, short port)
-    : io_service_(io_service),
-      acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
+  server(boost::asio::io_context& io_context, short port)
+    : io_context_(io_context),
+      acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
   {
-    session_ptr new_session(new session(io_service_));
+    session_ptr new_session(new session(io_context_));
     acceptor_.async_accept(new_session->socket(),
         boost::bind(&server::handle_accept, this, new_session,
           boost::asio::placeholders::error));
@@ -201,14 +248,14 @@ public:
       new_session->start();
     }
 
-    new_session.reset(new session(io_service_));
+    new_session.reset(new session(io_context_));
     acceptor_.async_accept(new_session->socket(),
         boost::bind(&server::handle_accept, this, new_session,
           boost::asio::placeholders::error));
   }
 
 private:
-  boost::asio::io_service& io_service_;
+  boost::asio::io_context& io_context_;
   tcp::acceptor acceptor_;
 };
 
@@ -222,12 +269,12 @@ int main(int argc, char* argv[])
       return 1;
     }
 
-    boost::asio::io_service io_service;
+    boost::asio::io_context io_context;
 
     using namespace std; // For atoi.
-    server s(io_service, atoi(argv[1]));
+    server s(io_context, atoi(argv[1]));
 
-    io_service.run();
+    io_context.run();
   }
   catch (std::exception& e)
   {