toby_toby_toby
toby_toby_toby

Reputation: 103

boost asio buffer lazy allocation

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?

Upvotes: 3

Views: 2291

Answers (1)

Tanner Sansbury
Tanner Sansbury

Reputation: 51881

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));
  }
}

Upvotes: 9

Related Questions