3

Async operations.

Now I pass preallocated byte buffer, for example:

s.async_receive_from(
    boost::asio::buffer( preallocated_pointer, preallocated_size ),
    _remote_endpoint,
    boost::bind(...)
    );

Is it possible to make lazy allocation for this and other calls?

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
toby_toby_toby
  • 103
  • 1
  • 10

1 Answers1

9

Lazy allocation, or allocating when the resource is needed, can be accomplished using boost::asio::null_buffers. null_buffers can be used to obtain reactor-style operations within Boost.Asio. This can be useful for integrating with third party libraries, using shared memory pools, etc. The Boost.Asio documentation provides some information and the following example code:

ip::tcp::socket socket(my_io_service);
...
socket.non_blocking(true);
...
socket.async_read_some(null_buffers(), read_handler);
...
void read_handler(boost::system::error_code ec)
{
  if (!ec)
  {
    std::vector<char> buf(socket.available());
    socket.read_some(buffer(buf));
  }
}
Community
  • 1
  • 1
Tanner Sansbury
  • 51,153
  • 9
  • 112
  • 169
  • nice answer - I'll be able to use this myself :) – Caribou Jan 08 '13 at 11:12
  • this answer has been really helpful. Thank you. – Cengiz Kandemir Aug 20 '16 at 16:48
  • 2
    `null_buffers` has been deprecated since Boost 1.66.0: [_"(Deprecated: Use the socket/descriptor wait() and async\_wait() member functions.)"_](http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/null_buffers.html) – sehe Feb 15 '18 at 14:01