Reputation: 103
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
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