Question

I have just tried to understand the boost::asio handler allocation example (http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/example/allocation/server.cpp).

I know this is an oversimplified example for educational purposes but there is something I cannot understand: is there any case in which the class handler_allocator make use of the heap (in_use_ == true) because I cannot see it.

Every session has his own allocator and every time is like this: read and then write but you do not read until the write finishes. And also you can read in the documentation:

The implementation guarantees that the deallocation will occur before the associated handler is invoked, which means the memory is ready to be reused for any new asynchronous operations started by the handler.

My question is: can you tell me an example where the allocation on the heap (in this example) occurs?

Extra: any one nows a little bit more complete example of handler custom allocations?

Was it helpful?

Solution

In short, Boost.Asio does not guarantee that asio_handler_allocate() will only be invoked once for a single asynchronous operation. Therefore, in the example, handler_allocator will allocate from the free store via operator new() if Boost.Asio decides it needs additional storage. The Asynchronous Operations documentation states:

If the implementation needs to allocate storage for an asynchronous operation, the implementation will perform asio_handler_allocate(size, &h), [...]. Multiple storage blocks may be allocated for a single asynchronous operation.

The criteria for which the implementation uses to determine it needs to allocate storage is unspecified. Additionally requirements for asio_handler_*() functions can be found in the Handler documentation.

With previous versions of Boost.Asio, the example may have performed multiple allocations per asynchronous operation. A brief glance at the revision history indicates effort has been spent to reduce the amount of handler copies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top