Re:I've been writing ring buffers wrong all these years
by falcolas on Wednesday December 14, 2016 @11:47AM
(#13177372)
I'm answering the specific problem posed by the OP, given his other comments in this thread.
[EDIT] Resolved internal concerns about size calculations.
Re:I've been writing ring buffers wrong all these years
by dllthomas on Wednesday December 14, 2016 @12:35PM
(#13177857)
You might have misread the OP. He's not asking why using & for the mask requires a 2^n sized buffer. He's asking why bother using & when it imposes these additional constraints on us. A part of the answer is that the constraints are already there with this approach, even if you pick a different function for masking than f(i,n) = i & (n - 1) -- which point OP only recently understood due to a misreading of the article.
You are assuming still using &, which is very obviously incorrect with a very obvious fix (%5) which is still wrong because of behavior at overflow.