Short story
I need to add incoming messages data to a fixed length buffer.
If buffer is full, replace oldest message data (or oldest bytes in buffer) with new one. This is some kind of a queue.
How to implement this kind of queue?
Program is in C and I have little (more like none) knowledge of C.
Long Story
I have a program that is receiving messages from a device.
This messages might be of variable length and have an associated Id.
Currently the program only keeps the latest message received for each Id.
This messages are kept internally by the program and are only displayed to the user if there’s a configuration for it, or if he sends a command (in real time) to get the received message for a given Id.
Now there’s a requirement to keep the several received messages for a given Id (not just the last) and return them.
They don’t need to be returned separately. They can be returned all in one block (think in the several messages as a piece of memory data that the device is returning).
Currently there’s a fixed length buffer to keep the received message and a variable to keep track of the received message length.
So I was thinking of using the existing buffer and to just keep adding new messages to it.
And If another message comes and there’s not more space left on the buffer, then I should overwrite the oldest message data. Something like a queue.
Especially this last part of implementing this kind of a queue is where I’m needing more help.
The program is in C, was not done by me, and my knowledge of C is limited (not to say non existing 🙂
I really should be learning C first, but there’s no time for that now 🙂
I’ve done C#, Java, Javascript and mostly.
Any help on managing this king of queue?
Thanks
A circular buffer would help in this case, with some slight modification that allows you to kick out the oldest entry when the buffer runs full. Imagine you a have some
typedef struct msg {...} msgand an arraymessage[N]of this kind, then you manage two indices,int in, outthat are both initialized to 0.While
out != inyou may read messages from this array. You implement the incrementing of bothinandoutas wrapping around, using some form ofout = (++ out) % N;. So far this is not different than any other circular buffer.Now, let’s address your question of replacing the oldest message in case the buffer is full, i.e. when
(in + 1) % N == outat the time when you are about to perform an insertion. With a circular buffer, this now is quite straightforward. To make space,outis pushed forward by one, allowing you to incrementinanyways, then you insert:Pushing the reader’s index
outforward, allows this position to be used for the most recent message.