diff --git a/asio/src/doc/overview/allocation.qbk b/asio/src/doc/overview/allocation.qbk index 91c17231ab..5735be1a8e 100644 --- a/asio/src/doc/overview/allocation.qbk +++ b/asio/src/doc/overview/allocation.qbk @@ -19,22 +19,53 @@ executing in parallel. Programs should be able to leverage this knowledge to reuse memory for all asynchronous operations in a chain. Given a copy of a user-defined `Handler` object `h`, if the implementation -needs to allocate memory associated with that handler it will execute the code: +needs to allocate memory associated with that handler it will obtain an +allocator using the `get_associated_allocator` function. For example: - void* pointer = asio_handler_allocate(size, &h); + asio::associated_allocator_t a = asio::get_associated_allocator(h); -Similarly, to deallocate the memory it will execute: +The associated allocator must satisfy the standard Allocator requirements. - asio_handler_deallocate(pointer, size, &h); +By default, handlers use the standard allocator (which is implemented in terms +of `::operator new()` and `::operator delete()`). The allocator may be +customised for a particular handler type by specifying a nested type +`allocator_type` and member function `get_allocator()`: -These functions are located using argument-dependent lookup. The implementation -provides default implementations of the above functions in the `asio` namespace: + class my_handler + { + public: + // Custom implementation of Allocator type requirements. + typedef my_allocator allocator_type; - void* asio_handler_allocate(size_t, ...); - void asio_handler_deallocate(void*, size_t, ...); + // Return a custom allocator implementation. + allocator_type get_allocator() const noexcept + { + return my_allocator(); + } -which are implemented in terms of `::operator new()` and `::operator delete()` -respectively. + void operator()() { ... } + }; + +In more complex cases, the `associated_allocator` template may be partially +specialised directly: + + namespace asio { + + template + struct associated_allocator + { + // Custom implementation of Allocator type requirements. + typedef my_allocator type; + + // Return a custom allocator implementation. + static type get(const my_handler&, + const Allocator& a = Allocator()) noexcept + { + return my_allocator(); + } + }; + + } // namespace asio The implementation guarantees that the deallocation will occur before the associated handler is invoked, which means the memory is ready to be reused for @@ -50,8 +81,8 @@ threads. [heading See Also] -[link asio.reference.asio_handler_allocate asio_handler_allocate], -[link asio.reference.asio_handler_deallocate asio_handler_deallocate], +[link asio.reference.associated_allocator associated_allocator], +[link asio.reference.get_associated_allocator get_associated_allocator], [link asio.examples.cpp03_examples.allocation custom memory allocation example (C++03)], [link asio.examples.cpp11_examples.allocation custom memory allocation example (C++11)]. diff --git a/asio/src/examples/cpp03/allocation/server.cpp b/asio/src/examples/cpp03/allocation/server.cpp index b10ee66037..5db3e5ca99 100644 --- a/asio/src/examples/cpp03/allocation/server.cpp +++ b/asio/src/examples/cpp03/allocation/server.cpp @@ -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 @@ class handler_allocator 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 +class handler_allocator +{ +public: + typedef T value_type; + + explicit handler_allocator(handler_memory& mem) + : memory_(mem) + { + } + + template + handler_allocator(const handler_allocator& other) + : memory_(other.memory_) + { + } + + template + struct rebind + { + typedef handler_allocator 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(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 class custom_alloc_handler { public: - custom_alloc_handler(handler_allocator& a, Handler h) - : allocator_(a), + typedef handler_allocator allocator_type; + + custom_alloc_handler(handler_memory& m, Handler h) + : memory_(m), handler_(h) { } + allocator_type get_allocator() const + { + return allocator_type(memory_); + } + template void operator()(Arg1 arg1) { @@ -91,29 +150,17 @@ class custom_alloc_handler handler_(arg1, arg2); } - friend void* asio_handler_allocate(std::size_t size, - custom_alloc_handler* this_handler) - { - return this_handler->allocator_.allocate(size); - } - - friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/, - custom_alloc_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 inline custom_alloc_handler make_custom_alloc_handler( - handler_allocator& a, Handler h) + handler_memory& m, Handler h) { - return custom_alloc_handler(a, h); + return custom_alloc_handler(m, h); } class session @@ -133,7 +180,7 @@ class session 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 @@ class session { 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 @@ class session 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 @@ class session // Buffer used to store data received from the client. boost::array 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_ptr; diff --git a/asio/src/examples/cpp11/allocation/server.cpp b/asio/src/examples/cpp11/allocation/server.cpp index 1eded41e44..0cb9e369d2 100644 --- a/asio/src/examples/cpp11/allocation/server.cpp +++ b/asio/src/examples/cpp11/allocation/server.cpp @@ -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 @@ class handler_allocator 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 +class handler_allocator +{ +public: + using value_type = T; + + explicit handler_allocator(handler_memory& mem) + : memory_(mem) + { + } + + template + handler_allocator(const handler_allocator& 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(memory_.allocate(sizeof(T) * n)); + } + + void deallocate(T* p, std::size_t /*n*/) const + { + return memory_.deallocate(p); + } + +private: + template 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 class custom_alloc_handler { public: - custom_alloc_handler(handler_allocator& a, Handler h) - : allocator_(a), - handler_(h) - { - } + using allocator_type = handler_allocator; - template - void operator()(Args&&... args) + custom_alloc_handler(handler_memory& m, Handler h) + : memory_(m), + handler_(h) { - handler_(std::forward(args)...); } - friend void* asio_handler_allocate(std::size_t size, - custom_alloc_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* this_handler) + template + void operator()(Args&&... args) { - this_handler->allocator_.deallocate(pointer); + handler_(std::forward(args)...); } private: - handler_allocator& allocator_; + handler_memory& memory_; Handler handler_; }; // Helper function to wrap a handler object to add custom allocation. template inline custom_alloc_handler make_custom_alloc_handler( - handler_allocator& a, Handler h) + handler_memory& m, Handler h) { - return custom_alloc_handler(a, h); + return custom_alloc_handler(m, h); } class session @@ -129,7 +171,7 @@ class session { 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 @@ class session { 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 @@ class session // Buffer used to store data received from the client. std::array 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