Circular buffer full and empty states

ID: circular-buffer-full-and-empty-states

With front-plus-count storage, empty means and full means ; every allocated slot is usable. With only the two wrapped array indices , the equality describes both possibilities. Indeed , and the two allowed counts have the same remainder. Recovering the count as therefore loses the full state. A count, a separate full flag, or a different index representation must resolve that ambiguity.
Another convention reserves one slot: for a physical array of length , empty means and full means . Its usable capacity is , and now recovers its count because . A five-slot array then stores at most four items.
This is the convention of the kernel's documented CIRC_* helpers, which additionally require power-of-two array lengths. It is not a universal property of circular buffers; our capacity-five count-based example uses all five slots and does not use those helpers.[ref]
The smallest count-based case, , makes the issue vivid: both and are always zero, but still distinguishes one live item from none. Capacity zero is excluded from our formulas because remainder modulo zero is undefined.

New to topics? Read the docs here!