Ring
Specifies the ring structure. This structure works as a circular buffer. When the buffer is in its maximum capacity, and a new element need be pushed into the buffer, the oldest element is replaced by the new element.
- Author
Matheus T. dos Santos (tenoriomatheus0@gmail.com)
- Version
0.1.0
- Date
19/09/2021
- Copyright
Matheus T. dos Santos todos os direitos reservados (c) 2021
Defines
-
BP_RING_INVALID_INDEX
Value representing an invalid index
-
BP_RING_INIT(array_)
Macro to initialize a bp_ring
- Parameters
-
array_ – Buffer where the elements will be stored.
Functions
-
void *bp_ring_get(bp_ring_t *ring, usize idx)
Get an element from the ring buffer, based on its position.
- Parameters
-
ring – Reference to bp_ring.
idx – Element index.
- Returns
A reference to the desired element
- Returns
NULL if the index is out of range or the ‘ring’ argument is NULL.
-
void *bp_ring_peek(bp_ring_t *ring)
Get the oldest element (at tail) in the ring buffer.
- Parameters
ring – Reference to bp_ring.
- Returns
A reference to the oldest element.
- Returns
NULL if the ‘ring’ argument is NULL or if the ring is empty.
-
int bp_ring_pop(bp_ring_t *ring, void *el)
Remove the oldest element (at tail) of the ring buffer and put it in el argument variable.
- Parameters
-
ring – Reference to bp_ring.
el – [out] Reference to a variable where the removed element will be put.
- Returns
0 on success.
- Returns
-ENODEV if the ‘ring’ argument is NULL.
- Returns
-ENOENT if the ring is empty.
-
int bp_ring_push(bp_ring_t *ring, void *el)
Push an element at the end (at head) of ring buffer.
- Parameters
-
ring – Reference to bp_ring.
el – Reference to the element to be pushed.
- Returns
0 on success.
- Returns
-ENODEV if the ‘ring’ or the ‘el’ argument is NULL.
-
usize bp_ring_find_idx(bp_ring_t *ring, void *param, bool (*cmp)(void *el, void *param))
Find the index of an element, based at some parameter related to the element. This parameter could be the element itself, or some field of its type. The match will be done based on cmp function pointer. If the cmp function pointer argument is null, then the elements will be compared with the parameter byte by byte.
- Parameters
-
ring – Reference to bp_ring.
param – Reference to the parameter used to compare elements.
cmp – Function to compare an element with the parameter passed at argument param.
- Returns
The index of found element.
- Returns
BP_RING_INVALID_INDEX if the element wasn’t found or if the ‘ring’ or the ‘param’ argument is NULL.
-
void *bp_ring_find(bp_ring_t *ring, void *param, bool (*cmp)(void *el, void *param))
Find the element in the ring buffer, based at some parameter related to the element. This parameter could be the element itself, or some field of its type. The match will be done based on cmp function pointer. If the cmp function point argument is null, then the elements will be compared with the parameter byte by byte.
- Parameters
-
ring – Reference to bp_ring.
param – Reference to the parameter used to compare elements.
cmp – Function to compare an element with the parameter passed at argument param.
- Returns
A reference to the found element.
- Returns
NULL if the element wasn’t found or if the ‘ring’ or the ‘param’ argument is NULL.
-
int bp_ring_clear(bp_ring_t *ring)
Drop all elements in the ring buffer.
Warning
After this function the ring buffer size is zero, but the elements stay in the buffer.
- Parameters
ring – Reference to bp_ring.
- Returns
0 on success.
- Returns
-ENODEV if the ‘ring’ argument is NULL.
-
usize bp_ring_size(bp_ring_t *ring)
Get the ring buffer size.
- Parameters
ring – Reference to bp_ring.
- Returns
The size of ring buffer.
- Returns
0 if the ‘ring’ argument is null.
-
bp_iter_t bp_ring_iter(bp_ring_t *ring)
Get a iterator to walk through the bp_ring.
Warning
This function doesn’t check if the ring argument is null. So if this argument is null, a crash will occur. That check must be done outside the function.
- Parameters
ring – Reference to bp_ring.
- Returns
A new iterator instance for the bp_ring.
-
struct bp_ring_t
- #include <bp_ring.h>
Struct with metadata about the ring buffer.
Public Members
-
u8_t *_array
Reference to the buffer itself.
-
usize _element_size
Size (in bytes) of a single element in the array.
-
usize _capacity
Maximum number of elements in the ring buffer.
-
usize _size
Current number of elements in the ring buffer.
-
usize _head
Index of the head of the ring buffer.
-
usize _tail
Index of the tail of the ring buffer.
-
u8_t *_array