Microcontroller programming 101.01: Ring Buffers
Work in progresspattern
head, tail, MAX, buf[MAX] (i.e. head/tail go from 0 to MAX-1; tail points to first byte about to go out, head points to first empty byte into which new byte will come in)
occup | free | |
---|---|---|
head >= tail | head - tail | MAX - 1 - (head - tail) = MAX + tail - head - 1 |
head < tail | MAX + head - tail | MAX - 1 - (MAX + head - tail) = tail - head - 1 |
Atomicity up and down