PrevNextUpHome SophiaFramework UNIVERSE 5.3

14.4. Ring Buffer Class

SFXRingBuffer is the class which represents a ring buffer.

Figure 14.1. Overview of the Ring Buffer

Overview of the Ring Buffer

Conceptually, the ring buffer is a chunk of memory arranged in the circular manner.

The ring buffer has the read pointer and the write pointer which advance clockwisely. When data are written into the ring buffer through the write pointer, the write pointer will advance. When data are read from the ring buffer through the read pointer, the read pointer will advance.

[Note] Note
The read pointer cannot go beyond the write pointer. Conversely, the write pointer cannot go beyond the read pointer.

Example 14.10. Method to use the SFXRingBuffer class

Byte data1[] = {'1', '2', '3', '4', '5', '6', '7', '8'};
Byte data2[] = {'9', 'a', 'b', 'c', 'd', 'e'};
SFXRingBuffer ring;
SFXBuffer buffer;

// set size of ring buffer to 12 bytes
ring.Allocate(12);

// write 8-bytes data
ring.Write(data1, sizeof(data1));

// reserve buffer for reading
buffer.SetSize(4);

// read 4 byte
ring.Read(&buffer); // buffer = "1234"

// write 6-bytes data
ring.Write(data2, sizeof(data2));

// reserve buffer for reading
buffer.SetSize(10);

// read 10 bytes
ring.Read(&buffer); // buffer = "56789abcde"