|
|
|
|
|
by mh7
1503 days ago
|
|
Why do so many people use the head==tail as full condition? You can use the entire buffer and not waste one slot by doing: push:
buf[writeidx % bufsize] = x; writeidx++; pop:
x = buf[readidx % bufsize]; readidx++; Unsigned integers, you get size by doing: writeidx - readidx;
Then you check size==0 or size==bufsize to detect empty/full. |
|
He has a companion post about a "magic" ring buffer that uses a virtual memory mapping to make a ring buffer where you can always address the entire sizes as contiguous memory, but I think it has a bit of minor bit rot in the linked implementations. https://fgiesen.wordpress.com/2012/07/21/the-magic-ring-buff...