I'm trying to find a way to hook a custom allocator when using boost::beast with asio::use_awaitable and C++ 20 coroutines. Given this function sketch:
asio::awaitable<void> process_request(asio::ip::tcp::socket sock)
{
// Prepare the beast http buffer and request stuff using the custom allocator
...
fmt::print(stdout, "Reading\n");
co_await http::async_read(sock, buffer, req, asio::use_awaitable);
fmt::print(stdout, "Done reading\n");
// Prepare the beast http buffer and response stuff using the custom allocator
...
fmt::print(stdout, "Writing\n");
co_await http::async_write(sock, res, asio::use_awaitable);
fmt::print(stdout, "Done!\n\n");
}
and overwriting the global operator new/delete I did some tracing of the allocations and hooked most of them to the custom allocator. Here are my questions:
- How to hook a custom allocator for the read/write handlers when using
use_awaitable
? I see that there is an allocation of 288 bytes in between the reading prints and 408 bytes allocation in between the writing prints. I suppose these are the allocations that asio internally does for the handlers. I've read here about how to provide custom allocator for callback hadnlers but I can't figure a way to do it for this kind of Completion Token. Asio seems to provide a way for hooking an allocator withuse_future
but not withuse_awaitable
. - How to give the custom allocator to the asio machinery so that it uses it for the coroutine frame allocation? As far as I know, I can overwrite operator new/delete for the coroutine promise type but this type is hidden inside the asio machinery. In addition to that I'm not able to see the allocation for the above coroutine in my printing. I've 1024 bytes stack buffer inside the above function and I don't see so big allocations in my printing.
Thanks.