VCV Rack API v2
Loading...
Searching...
No Matches
rack::dsp::RingBuffer< T, S > Struct Template Reference

Lock-free queue with fixed size and no allocations. More...

#include <ringbuffer.hpp>

Public Member Functions

void push (T t)
 Adds an element to the end of the buffer.
 
void pushBuffer (const T *t, int n)
 Copies an array to the end of the buffer.
 
shift ()
 Removes and returns an element from the start of the buffer.
 
void shiftBuffer (T *t, size_t n)
 Removes and copies an array from the start of the buffer.
 
void clear ()
 
bool empty () const
 
bool full () const
 
size_t size () const
 
size_t capacity () const
 

Public Attributes

std::atomic< size_t > start {0}
 
std::atomic< size_t > end {0}
 
data [S]
 

Detailed Description

template<typename T, size_t S>
struct rack::dsp::RingBuffer< T, S >

Lock-free queue with fixed size and no allocations.

If S is not a power of 2, performance might be reduced, and the index could overflow in a thousand years, but it should usually be fine for your purposes.

Supports only a single producer and consumer. To my knowledge, nobody has invented a 100% correct multiple producer/consumer lock-free ring buffer for x86_64.

Member Function Documentation

◆ push()

template<typename T , size_t S>
void rack::dsp::RingBuffer< T, S >::push ( T t)
inline

Adds an element to the end of the buffer.

◆ pushBuffer()

template<typename T , size_t S>
void rack::dsp::RingBuffer< T, S >::pushBuffer ( const T * t,
int n )
inline

Copies an array to the end of the buffer.

n must be at most S.

◆ shift()

template<typename T , size_t S>
T rack::dsp::RingBuffer< T, S >::shift ( )
inline

Removes and returns an element from the start of the buffer.

◆ shiftBuffer()

template<typename T , size_t S>
void rack::dsp::RingBuffer< T, S >::shiftBuffer ( T * t,
size_t n )
inline

Removes and copies an array from the start of the buffer.

n must be at most S.

◆ clear()

template<typename T , size_t S>
void rack::dsp::RingBuffer< T, S >::clear ( )
inline

◆ empty()

template<typename T , size_t S>
bool rack::dsp::RingBuffer< T, S >::empty ( ) const
inline

◆ full()

template<typename T , size_t S>
bool rack::dsp::RingBuffer< T, S >::full ( ) const
inline

◆ size()

template<typename T , size_t S>
size_t rack::dsp::RingBuffer< T, S >::size ( ) const
inline

◆ capacity()

template<typename T , size_t S>
size_t rack::dsp::RingBuffer< T, S >::capacity ( ) const
inline

Member Data Documentation

◆ start

template<typename T , size_t S>
std::atomic<size_t> rack::dsp::RingBuffer< T, S >::start {0}

◆ end

template<typename T , size_t S>
std::atomic<size_t> rack::dsp::RingBuffer< T, S >::end {0}

◆ data

template<typename T , size_t S>
T rack::dsp::RingBuffer< T, S >::data[S]

The documentation for this struct was generated from the following file: