A ring buffer, or circular buffer, is a widely used data structure in the embedded world. It works as a queue placed on top of a fixed-size memory array. The buffer can contain a fixed number of elements. A function that generates these elements puts them into the buffer sequentially, one by one. When the end of the buffer is reached, it switches to the start of the buffer, as if its first element follows the last element.
This design has proven to be remarkably efficient when it comes to organizing data exchange between data producers and consumers that are independent and cannot wait for each other, which is a common scenario in embedded development. For example, an interrupt service routine should quickly queue data coming from a device for further processing, while interrupts are disabled. It cannot wait for the function that processes...