circular buffer

(idea) by hobyrne Fri Oct 26 2001 at 23:23:00

From FOLDOC, the Free On-Line Dictionary Of Computing (http://foldoc.doc.ic.ac.uk/foldoc/):

<programming> An area of memory used to store a continuous stream of data by starting again at the beginning of the buffer after reaching the end. A circular buffer is usually written by one process and read by another. Separate read and write pointers are maintained. These are not allowed to pass each other otherwise either unread data would be overwritten or invalid data would be read.

For example, to initialise a circular buffer (a.k.a. rolling buffer), which is 20 words long starting at address 0x3210, pseudo-code might look like this:

#define BufferBase (0x3210)
#define BufferSize (20)
ReadPointer := 0
WritePointer := 0
Empty := true

To add to the buffer:

if ((WritePointer = ReadPointer) && (Empty = false)) {
  ERROR ("Buffer overflow")
}
*(BufferBase + WritePointer) := WriteData
WritePointer := ((WritePointer + 1) mod BufferSize)
Empty := false

To read from the buffer:

if (Empty = true) {
  ERROR ("Buffer underflow")
}
ReadData := *(BufferBase + ReadPointer)
ReadPointer := ((ReadPointer + 1) mod BufferSize)
if (ReadPointer = WritePointer) {
  Empty := true
}
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.