I’m writing a message queue meant to operate over a socket, and for various reasons I’d like have the queue memory live in user space and have a thread that drains queues into their respective sockets.
Messages are going to be small blobs of memory (between 4 and 4K bytes probably), so I think avoiding malloc()ing memory constantly is a must to avoid fragmentation.
The mode of operation would be that a user calls something like send(msg) and the message is then copied into the queue memory and is sent over the socket at a convenient time.
My question is, is there a “nice” way to store variable sized chunks of data in something like a std::queue or std::vector or am I going to have to go the route of putting together a memory pool and handling my own allocation out of that?
You can create a large circular buffer, copy data from the chunks into that buffer, and store pairs of
{start pointer, length}in your queue. Since the chunks are allocated in the same order that they are consumed, the math to check for overlaps should be relatively straightforward.Memory allocators have become quite good these days, so I would not be surprised if a solution based on a “plain” allocator exhibited a comparable performance.