Skip to content

Available size is one less than the data array #2

@fleutot

Description

@fleutot

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions