Since buffer->tail == buffer->head means empty, and tail points at the first free position, the last slot before tail catching around to head is not actually usable.
If the data array is 64 bytes long, only 63 of these are actually usable at a time.
This reflects in the strange fact that ringBufferFreeSpace and ringBufferMaxSize using a mask as if it was a length. It checks out since the mask is the length minus one, but I think that this is hiding what the real culprit is: one slot is wasted.
Suggestions:
- use an actual -1 instead of mask to compute free space / max size
- document the fact that only len - 1 of
data is usable at any given time
- consider modifying the code to make
tail point at the last written slot. There might be more thinking into that, especially how to check for emptiness.
Since
buffer->tail == buffer->headmeans empty, andtailpoints at the first free position, the last slot beforetailcatching around toheadis not actually usable.If the
dataarray is 64 bytes long, only 63 of these are actually usable at a time.This reflects in the strange fact that
ringBufferFreeSpaceandringBufferMaxSizeusing a mask as if it was a length. It checks out since the mask is the length minus one, but I think that this is hiding what the real culprit is: one slot is wasted.Suggestions:
datais usable at any given timetailpoint at the last written slot. There might be more thinking into that, especially how to check for emptiness.