
Hi Ion, --- Ion GaztaƱaga <igaztanaga@gmail.com> wrote:
What does the custom allocator allocate? async_server_receive_handlers?
It allocates objects of type Allocator::value_type.
Because the function is templatized:
template <typename Allocator> static typename Allocator::pointer allocate( async_server_receive_handler& h, Allocator& allocator, typename Allocator::size_type count) { return reinterpret_cast<typename Allocator::pointer>( h.this_p_->operation_buffer); }
so what's "Allocator &allocator" object, and what does it allocate?
The Allocator type is rebound from the allocator template parameter on the public asio types. By default this is std::allocator<void>, so the Allocator type would be std::allocator<some_internal_asio_type>. The default implementation of the handler_alloc_hook::allocate function looks like: return allocator.allocate(count); and the default implementation of deallocate is: allocator.deallocate(pointer, count);
Can't we know at compile time what type are we going to manage?
Allocator::value_type is an implementation-defined type, e.g. it could be a win_iocp_socket_service::receive_operation object. But you do "know" this type at compile time since it's a template.
Also, should we define different allocators for read, write, accept, and other events?
That's up to you :) I expect judicious use of function object wrapping combined with partial specialisation could be useful to use the same allocation for difference operations. E.g.: template <typename Function> tagged_handler<Function> tag_handler(Function f); template <typename Function> class asio::handler_alloc_hook<tagged_handler<Function> > { ... custom allocation ... }; async_read(s, bufs, tag_handler(boost::bind(...))); Cheers, Chris