Mailing List archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vdr] Re: ringbuffer broken?



Ludwig Nussel wrote:
> 
> Hi,
> 
> cRingBufferLinear beheaves strangely. The attached test program
> creates a cRingBufferLinear of size 100 and margin 10 an then tries
> to fill it completely, get everything and fill it again. I'd expect
> to be able to put 100 bytes inside but that doesn't work. It fills
> up to 89 (i.e. size-margin-1 does that make any sense?). After
> beeing depleted and refilled it doesn't allow Get() anymore:
> 
> put 25 lost 0
> put 25 lost 0
> put 25 lost 0
> put 14 lost 11
> got 89 at 0x8077012
> got 0 at (nil)
> put 25 lost 0
> put 25 lost 0
> put 25 lost 0
> put 14 lost 11
> put 0 lost 25
> got 0 at (nil)
> 
> Shouldn't the buffer fill up to 100? I'd expect 'margin' to only
> affect Get(), i.e. block it until the specified number of bytes is
> available.

'Size' is the total size of the buffer, with 'Margin' counted against
that size. A buffer with n byte size (and no margin) can hold at most
n-1 actual bytes, because there has to be a difference between the write
and read pointer in order to be able to tell "empty" from "full".
So in your case it is expected behaviour to have the buffer only fill
up to 89 bytes. I'll change the comment in ringbufer.h to make this clear:

  cRingBufferLinear(int Size, int Margin = 0, bool Statistics = false);
    ///< Creates a linear ring buffer.
    ///< The buffer will be able to hold at most Size-Margin-1 bytes of data, and will
    ///< be guaranteed to return at least Margin bytes in one consecutive block.

As for the Get() failure, it looks like this line fixes it:

--- ringbuffer.c        2004/03/07 13:46:51     1.19
+++ ringbuffer.c        2004/06/19 12:27:56
@@ -181,6 +181,7 @@
      int t = margin - rest;
      memcpy(buffer + t, buffer + tail, rest);
      tail = t;
+     rest = head - tail;
      }
   int diff = head - tail;
   int cont = (diff >= 0) ? diff : Size() + diff - margin;

After relocating the part of the buffer that is smaller than Margin,
the 'rest' needs to be newly calculated.

Klaus




Home | Main Index | Thread Index