Glossary · Automation software engineering and architecture
Circular buffer
Also known as: Ring buffer, Cyclic buffer
German: Ringpuffer
In software engineering, a circular buffer is a fixed-size data structure that stores elements in a continuous loop: when the end is reached, writing wraps around to the start, and, depending on the design, the oldest data is either overwritten or new data is rejected when the buffer is full.
- Software engineering
In one sentence
A circular buffer is a fixed-size buffer whose write position wraps around to the start, often overwriting the oldest data.
Example
A drive keeps the last 1,000 samples of current and speed in a circular buffer; when a fault occurs, the buffer is frozen and uploaded for analysis.
How it applies
- Engineering: Circular buffers need no dynamic memory allocation and have constant-time operations, which makes them popular in real-time and embedded software, for example for communication queues between an Interrupt service routine (ISR) and a task, or for trace recording.
- Design: Decide what happens when the buffer is full: overwrite the oldest entries (typical for traces and logs) or refuse new entries (typical for commands, where losing data is not acceptable). Shared buffers between tasks need a safe access scheme.
- Documentation: Diagnostic and service documentation should state the buffer size, what is recorded and when old data is overwritten. Service staff must know that fault history in a circular buffer may be lost if it is not saved in time.
Circular buffer vs. queue
A queue is the abstract first-in, first-out behavior; a circular buffer is one common way to implement it in fixed memory. An Event queue may be built on a circular buffer.