Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix RingBuffer::getReadableDataSize, add a few methods
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Aug 27, 2023
1 parent 227a701 commit c11f93d
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions distrho/extra/RingBuffer.hpp
Expand Up @@ -200,14 +200,22 @@ class RingBufferControl
return (buffer->buf == nullptr || buffer->head == buffer->tail);
}

/*
* Get the full ringbuffer size.
*/
uint32_t getSize() const noexcept
{
return buffer != nullptr ? buffer->size : 0;
}

/*
* Get the size of the data available to read.
*/
uint32_t getReadableDataSize() const noexcept
{
DISTRHO_SAFE_ASSERT_RETURN(buffer != nullptr, 0);

const uint32_t wrap = buffer->head > buffer->tail ? 0 : buffer->size;
const uint32_t wrap = buffer->head >= buffer->tail ? 0 : buffer->size;

return wrap + buffer->head - buffer->tail;
}
Expand All @@ -219,7 +227,7 @@ class RingBufferControl
{
DISTRHO_SAFE_ASSERT_RETURN(buffer != nullptr, 0);

const uint32_t wrap = (buffer->tail > buffer->wrtn) ? 0 : buffer->size;
const uint32_t wrap = buffer->tail > buffer->wrtn ? 0 : buffer->size;

return wrap + buffer->tail - buffer->wrtn;
}
Expand All @@ -243,6 +251,20 @@ class RingBufferControl
std::memset(buffer->buf, 0, buffer->size);
}

/*
* Reset the ring buffer read and write positions, marking the buffer as empty.
* Requires a buffer struct tied to this class.
*/
void flush() noexcept
{
DISTRHO_SAFE_ASSERT_RETURN(buffer != nullptr,);

buffer->head = buffer->tail = buffer->wrtn = 0;
buffer->invalidateCommit = false;

errorWriting = false;
}

// -------------------------------------------------------------------
// read operations

Expand Down

0 comments on commit c11f93d

Please sign in to comment.