#include <cstring>
#include <glib.h>
+#include "pbd/libpbd_visibility.h"
+
template<class T>
-class RingBuffer
+class /*LIBPBD_API*/ RingBuffer
{
public:
RingBuffer (guint sz) {
// size = ffs(sz); /* find first [bit] set is a single inlined assembly instruction. But it looks like the API rounds up so... */
guint power_of_two;
- for (power_of_two = 1; 1U<<power_of_two < sz; power_of_two++);
- size = 1<<power_of_two;
- size_mask = size;
- size_mask -= 1;
- buf = new T[size];
- reset ();
+ for (power_of_two = 1; 1U<<power_of_two < sz; power_of_two++) {}
+ size = 1<<power_of_two;
+ size_mask = size;
+ size_mask -= 1;
+ buf = new T[size];
+ reset ();
+ }
- };
-
virtual ~RingBuffer() {
delete [] buf;
}
g_atomic_int_set (&write_idx, w);
g_atomic_int_set (&read_idx, r);
}
-
+
guint read (T *dest, guint cnt);
- guint write (T *src, guint cnt);
+ guint write (T const * src, guint cnt);
struct rw_vector {
T *buf[2];
void get_read_vector (rw_vector *);
void get_write_vector (rw_vector *);
-
+
void decrement_read_idx (guint cnt) {
g_atomic_int_set (&read_idx, (g_atomic_int_get(&read_idx) - cnt) & size_mask);
- }
+ }
void increment_read_idx (guint cnt) {
g_atomic_int_set (&read_idx, (g_atomic_int_get(&read_idx) + cnt) & size_mask);
- }
+ }
void increment_write_idx (guint cnt) {
g_atomic_int_set (&write_idx, (g_atomic_int_get(&write_idx) + cnt) & size_mask);
- }
+ }
- guint write_space () {
+ guint write_space () const {
guint w, r;
-
+
w = g_atomic_int_get (&write_idx);
r = g_atomic_int_get (&read_idx);
-
+
if (w > r) {
return ((r - w + size) & size_mask) - 1;
} else if (w < r) {
return size - 1;
}
}
-
- guint read_space () {
+
+ guint read_space () const {
guint w, r;
-
+
w = g_atomic_int_get (&write_idx);
r = g_atomic_int_get (&read_idx);
-
+
if (w > r) {
return w - r;
} else {
guint size_mask;
};
-template<class T> guint
+template<class T> /*LIBPBD_API*/ guint
RingBuffer<T>::read (T *dest, guint cnt)
{
guint free_cnt;
}
to_read = cnt > free_cnt ? free_cnt : cnt;
-
+
cnt2 = priv_read_idx + to_read;
if (cnt2 > size) {
n1 = to_read;
n2 = 0;
}
-
+
memcpy (dest, &buf[priv_read_idx], n1 * sizeof (T));
priv_read_idx = (priv_read_idx + n1) & size_mask;
return to_read;
}
-template<class T> guint
-RingBuffer<T>::write (T *src, guint cnt)
+template<class T> /*LIBPBD_API*/ guint
+RingBuffer<T>::write (T const *src, guint cnt)
{
guint free_cnt;
}
to_write = cnt > free_cnt ? free_cnt : cnt;
-
+
cnt2 = priv_write_idx + to_write;
if (cnt2 > size) {
return to_write;
}
-template<class T> void
-RingBuffer<T>::get_read_vector (RingBuffer<T>::rw_vector *vec)
+template<class T> /*LIBPBD_API*/ void
+RingBuffer<T>::get_read_vector (typename RingBuffer<T>::rw_vector *vec)
{
guint free_cnt;
guint cnt2;
guint w, r;
-
+
w = g_atomic_int_get (&write_idx);
r = g_atomic_int_get (&read_idx);
-
+
if (w > r) {
free_cnt = w - r;
} else {
if (cnt2 > size) {
/* Two part vector: the rest of the buffer after the
- current write ptr, plus some from the start of
+ current write ptr, plus some from the start of
the buffer.
*/
vec->len[1] = cnt2 & size_mask;
} else {
-
+
/* Single part vector: just the rest of the buffer */
-
+
vec->buf[0] = &buf[r];
vec->len[0] = free_cnt;
+ vec->buf[1] = 0;
vec->len[1] = 0;
}
}
-template<class T> void
-RingBuffer<T>::get_write_vector (RingBuffer<T>::rw_vector *vec)
+template<class T> /*LIBPBD_API*/ void
+RingBuffer<T>::get_write_vector (typename RingBuffer<T>::rw_vector *vec)
{
guint free_cnt;
guint cnt2;
guint w, r;
-
+
w = g_atomic_int_get (&write_idx);
r = g_atomic_int_get (&read_idx);
-
+
if (w > r) {
free_cnt = ((r - w + size) & size_mask) - 1;
} else if (w < r) {
} else {
free_cnt = size - 1;
}
-
+
cnt2 = w + free_cnt;
if (cnt2 > size) {
-
+
/* Two part vector: the rest of the buffer after the
- current write ptr, plus some from the start of
+ current write ptr, plus some from the start of
the buffer.
*/