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.