[LAD] a *simple* ring buffer, comments pls?

Fons Adriaensen fons at linuxaudio.org
Fri Jul 8 19:53:12 UTC 2011


On Fri, Jul 08, 2011 at 09:05:42PM +0200, Arnold Krille wrote:
 
> What would happen? The ringbuffer in jack is explicitely only for one-reader-
> one-writer. So in this optimization, the only participant using the read_ptr 
> to do something possibly bad, is the reading participant which is currently 
> executing this code.
> The writing participant can access that read_ptr for example to check the 
> available space. But as the docs state (afair), the available sizes for 
> read/write are not strict functions, the only thing that counts is if you have 
> space for reading/writing. And that is fulfilled if read_ptr!=write_ptr...

Actually things are a little better: you can be sure that _at least_
the space indicated is available for reading or writing, not just that
you can read or write one item (byte in this case).

Looking at the way things are implemented, the right value would
be returned even if the unmasked 'other side' count is used.

I'm using a slightly different (C++) implementation: the stored
values equivalent to jack's rd_ptr and wr_ptr (wrong names, they
are not pointers but indices) are never masked. The 'size-1' mask
(with size = 2^N) is applied only when they are used to index the
buffer array.

This has two consequences:

- You can use all 2^n elements, there is no ambiguity between
  full and empty.

- The code is simplified a bit:

  n_avail_read  = wr_index - rd_index;
  n_avail_write = rd_index - wr_index + size;

  and no conditions, masking, or -1  are necessary.

Ciao,

-- 
FA




More information about the Linux-audio-dev mailing list