Hacker News new | ask | show | jobs
by jhallenworld 4015 days ago
A conceptually simpler way to do this is to assign one (or more) extra bits in the head an tail pointers. For example, for a 512 entry ring, use 16-bit indices. Whenever you index the ring, and the index with 511 before performing the index.

  Write to ring: ring[511&(head++)] = data

  Read from ring: data = ring[511&(tail++)]

  Ring is empty: head == tail

  Ring is full: tail + 512 == head