Sub topic 6
Three Ways to Tell Full from Empty
This chapter assumes you know the head and tail indices and the modulo wraparound from the earlier chapters. You should also be comfortable with unsigned integer arithmetic in C or Python.
The ambiguity
A ring buffer reads at head and writes at tail. An empty buffer has head == tail. A buffer where tail has wrapped around and caught up with head also has head == tail. The two indices alone cannot tell you which state you are in, so you need extra information. There are three common ways to get it.
Strategy 1: a count field
Store the number of items next to the indices. The buffer is empty when count == 0 and full when count == capacity. The RingBuffer class in this tutorial uses this approach. The tail is computed as (head + count) % capacity, so you only keep head and count.
- Uses all slots.
- Costs one extra integer.
- Both the producer and the consumer modify
count, which makes it a poor fit for lock-free code.
Strategy 2: a spare slot
Keep head and tail and never fill the last slot. The buffer is empty when head == tail. It is full when advancing tail by one would land on head.
def is_empty(head, tail):
return head == tail
def is_full(head, tail, n):
return (tail + 1) % n == head
n = 4
print(is_empty(0, 0)) # True
print(is_full(0, 3, n)) # True: 3 items stored, 1 slot left unused
print(is_full(0, 2, n)) # False- Needs no extra variable.
- Wastes one slot, so an array of size
nholds at mostn - 1items. - Each index has a single writer, which suits concurrent use.
Strategy 3: free-running indices
Let head and tail count upward without ever wrapping them yourself. Reduce them modulo the capacity only when you touch the array. The item count is tail - head. Empty is a count of 0 and full is a count equal to CAP.
#include <stdint.h>
#include <stdio.h>
#define CAP 8u
int main(void) {
uint32_t head = UINT32_MAX - 1u; /* about to overflow */
uint32_t tail = head;
for (int i = 0; i < 8; i++) {
tail++; /* tail wraps past zero */
}
printf("count = %u\n", (unsigned)(tail - head)); /* count = 8 */
printf("full = %d\n", (tail - head) == CAP); /* full = 1 */
return 0;
}Here tail has wrapped to 6 while head is 4294967294. The subtraction still yields 8, because unsigned arithmetic is performed modulo 2^32.
Choosing a strategy
| Strategy | Usable slots | Extra state | Best for |
|---|---|---|---|
| Count field | `n` | `count` | Simple single-threaded code |
| Spare slot | `n - 1` | None | Small buffers where one slot is cheap to give up |
| Free-running indices | `n` | None | Power-of-two buffers and lock-free queues |
Free-running indices use every slot and need no shared counter. Their cost is the power-of-two restriction. If your capacity is fixed by an outside requirement, such as exactly 100 samples, use a count field or a spare slot.
Exercise
A ring buffer uses the spare-slot strategy with an array of 16 elements. How many items can it hold, and what is the condition for full when head is 5?
Show answer
It holds at most 15 items. It is full when (tail + 1) % 16 == head, so when tail is 4.
Exercise
Why does tail - head still give the correct count when tail has overflowed past 2^32 but head has not?
Show answer
Unsigned subtraction is performed modulo 2^32, so the difference equals the true number of items as long as that number is at most CAP. This is safe only when CAP is a power of two that divides 2^32.