Lightweight ring buffer, for fast insertion/deletion of bytes.
More...
Module Source Dependencies
The following files must be built with any user project that uses this module:
Module Description
Lightweight ring buffer, for fast insertion/deletion. Multiple buffers can be created of different sizes to suit different needs.
Note that for each buffer, insertion and removal operations may occur at the same time (via a multi-threaded ISR based system) however the same kind of operation (two or more insertions or deletions) must not overlap. If there is possibility of two or more of the same kind of operating occurring at the same point in time, atomic (mutex) locking should be used.
Example Usage
The following snippet is an example of how this module may be used within a typical application.
uint8_t BufferData[128];
printf("Buffer Length: %d, Buffer Data: \r\n", BufferCount);
while (BufferCount--)
static uint16_t RingBuffer_GetCount |
( |
RingBuffer_t *const |
Buffer | ) |
|
|
inlinestatic |
Retrieves the current number of bytes stored in a particular buffer. This value is computed by entering an atomic lock on the buffer, so that the buffer cannot be modified while the computation takes place. This value should be cached when reading out the contents of the buffer, so that as small a time as possible is spent in an atomic lock.
- Note
- The value returned by this function is guaranteed to only be the minimum number of bytes stored in the given buffer; this value may change as other threads write new data, thus the returned number should be used only to determine how many successive reads may safely be performed on the buffer.
- Parameters
-
[in] | Buffer | Pointer to a ring buffer structure whose count is to be computed. |
- Returns
- Number of bytes currently stored in the buffer.
static uint16_t RingBuffer_GetFreeCount |
( |
RingBuffer_t *const |
Buffer | ) |
|
|
inlinestatic |
Retrieves the free space in a particular buffer. This value is computed by entering an atomic lock on the buffer, so that the buffer cannot be modified while the computation takes place.
- Note
- The value returned by this function is guaranteed to only be the maximum number of bytes free in the given buffer; this value may change as other threads write new data, thus the returned number should be used only to determine how many successive writes may safely be performed on the buffer when there is a single writer thread.
- Parameters
-
[in] | Buffer | Pointer to a ring buffer structure whose free count is to be computed. |
- Returns
- Number of free bytes in the buffer.
static void RingBuffer_InitBuffer |
( |
RingBuffer_t * |
Buffer, |
|
|
uint8_t *const |
DataPtr, |
|
|
const uint16_t |
Size |
|
) |
| |
|
inlinestatic |
Initializes a ring buffer ready for use. Buffers must be initialized via this function before any operations are called upon them. Already initialized buffers may be reset by re-initializing them using this function.
- Parameters
-
[out] | Buffer | Pointer to a ring buffer structure to initialize. |
[out] | DataPtr | Pointer to a global array that will hold the data stored into the ring buffer. |
[out] | Size | Maximum number of bytes that can be stored in the underlying data array. |
static void RingBuffer_Insert |
( |
RingBuffer_t * |
Buffer, |
|
|
const uint8_t |
Data |
|
) |
| |
|
inlinestatic |
Inserts an element into the ring buffer.
- Warning
- Only one execution thread (main program thread or an ISR) may insert into a single buffer otherwise data corruption may occur. Insertion and removal may occur from different execution threads.
- Parameters
-
[in,out] | Buffer | Pointer to a ring buffer structure to insert into. |
[in] | Data | Data element to insert into the buffer. |
static bool RingBuffer_IsEmpty |
( |
RingBuffer_t *const |
Buffer | ) |
|
|
inlinestatic |
Atomically determines if the specified ring buffer contains any data. This should be tested before removing data from the buffer, to ensure that the buffer does not underflow.
If the data is to be removed in a loop, store the total number of bytes stored in the buffer (via a call to the RingBuffer_GetCount() function) in a temporary variable to reduce the time spent in atomicity locks.
- Parameters
-
[in,out] | Buffer | Pointer to a ring buffer structure to insert into. |
- Returns
- Boolean
true
if the buffer contains no free space, false
otherwise.
Atomically determines if the specified ring buffer contains any free space. This should be tested before storing data to the buffer, to ensure that no data is lost due to a buffer overrun.
- Parameters
-
[in,out] | Buffer | Pointer to a ring buffer structure to insert into. |
- Returns
- Boolean
true
if the buffer contains no free space, false
otherwise.
static uint8_t RingBuffer_Peek |
( |
RingBuffer_t *const |
Buffer | ) |
|
|
inlinestatic |
Returns the next element stored in the ring buffer, without removing it.
- Parameters
-
[in,out] | Buffer | Pointer to a ring buffer structure to retrieve from. |
- Returns
- Next data element stored in the buffer.
Removes an element from the ring buffer.
- Warning
- Only one execution thread (main program thread or an ISR) may remove from a single buffer otherwise data corruption may occur. Insertion and removal may occur from different execution threads.
- Parameters
-
[in,out] | Buffer | Pointer to a ring buffer structure to retrieve from. |
- Returns
- Next data element stored in the buffer.