Skip to content

Comment on Show HN: Revring, a circular buffer with zero memory waste

Comments

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
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.