projects
/
ardour.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
fix merge conflicts with master
[ardour.git]
/
libs
/
pbd
/
pbd
/
ringbuffer.h
diff --git
a/libs/pbd/pbd/ringbuffer.h
b/libs/pbd/pbd/ringbuffer.h
index 1d9c9b04e333990d2ba610e8cab6dcb170506d55..bb5485a8f372dce62e6dc962c600026a72c22ea7 100644
(file)
--- a/
libs/pbd/pbd/ringbuffer.h
+++ b/
libs/pbd/pbd/ringbuffer.h
@@
-15,32
+15,28
@@
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id$
*/
#ifndef ringbuffer_h
#define ringbuffer_h
*/
#ifndef ringbuffer_h
#define ringbuffer_h
-//#include <sys/mman.h>
-
+#include <cstring>
#include <glib.h>
template<class T>
class RingBuffer
{
public:
#include <glib.h>
template<class T>
class RingBuffer
{
public:
- RingBuffer (size_t sz) {
- size_t 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 ();
-
- };
+ 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 ();
+ }
virtual ~RingBuffer() {
delete [] buf;
virtual ~RingBuffer() {
delete [] buf;
@@
-48,44
+44,44
@@
class RingBuffer
void reset () {
/* !!! NOT THREAD SAFE !!! */
void reset () {
/* !!! NOT THREAD SAFE !!! */
- g_atomic_int_set (&write_
ptr
, 0);
- g_atomic_int_set (&read_
ptr
, 0);
+ g_atomic_int_set (&write_
idx
, 0);
+ g_atomic_int_set (&read_
idx
, 0);
}
}
- void set (
size_t r, size_
t w) {
+ void set (
guint r, guin
t w) {
/* !!! NOT THREAD SAFE !!! */
/* !!! NOT THREAD SAFE !!! */
- g_atomic_int_set (&write_
ptr
, w);
- g_atomic_int_set (&read_
ptr
, r);
+ g_atomic_int_set (&write_
idx
, w);
+ g_atomic_int_set (&read_
idx
, r);
}
}
-
size_t read (T *dest, size_
t cnt);
-
size_t write (T *src, size_
t cnt);
+
guint read (T *dest, guin
t cnt);
+
guint write (T const * src, guin
t cnt);
struct rw_vector {
T *buf[2];
struct rw_vector {
T *buf[2];
-
size_
t len[2];
+
guin
t len[2];
};
void get_read_vector (rw_vector *);
void get_write_vector (rw_vector *);
};
void get_read_vector (rw_vector *);
void get_write_vector (rw_vector *);
- void decrement_read_
ptr (size_
t cnt) {
- g_atomic_int_set (&read_
ptr, (g_atomic_int_get(&read_ptr
) - cnt) & size_mask);
+ void decrement_read_
idx (guin
t cnt) {
+ g_atomic_int_set (&read_
idx, (g_atomic_int_get(&read_idx
) - cnt) & size_mask);
}
}
- void increment_read_
ptr (size_
t cnt) {
- g_atomic_int_set (&read_
ptr, (g_atomic_int_get(&read_ptr
) + cnt) & size_mask);
+ void increment_read_
idx (guin
t cnt) {
+ g_atomic_int_set (&read_
idx, (g_atomic_int_get(&read_idx
) + cnt) & size_mask);
}
}
- void increment_write_
ptr (size_
t cnt) {
- g_atomic_int_set (&write_
ptr, (g_atomic_int_get(&write_ptr
) + cnt) & size_mask);
+ void increment_write_
idx (guin
t cnt) {
+ g_atomic_int_set (&write_
idx, (g_atomic_int_get(&write_idx
) + cnt) & size_mask);
}
}
-
size_
t write_space () {
-
size_
t w, r;
+
guin
t write_space () {
+
guin
t w, r;
- w = g_atomic_int_get (&write_
ptr
);
- r = g_atomic_int_get (&read_
ptr
);
+ w = g_atomic_int_get (&write_
idx
);
+ r = g_atomic_int_get (&read_
idx
);
if (w > r) {
return ((r - w + size) & size_mask) - 1;
if (w > r) {
return ((r - w + size) & size_mask) - 1;
@@
-96,11
+92,11
@@
class RingBuffer
}
}
}
}
-
size_
t read_space () {
-
size_
t w, r;
+
guin
t read_space () {
+
guin
t w, r;
- w = g_atomic_int_get (&write_
ptr
);
- r = g_atomic_int_get (&read_
ptr
);
+ w = g_atomic_int_get (&write_
idx
);
+ r = g_atomic_int_get (&read_
idx
);
if (w > r) {
return w - r;
if (w > r) {
return w - r;
@@
-110,28
+106,28
@@
class RingBuffer
}
T *buffer () { return buf; }
}
T *buffer () { return buf; }
-
size_t get_write_ptr () const { return g_atomic_int_get (&write_ptr
); }
-
size_t get_read_ptr () const { return g_atomic_int_get (&read_ptr
); }
-
size_
t bufsize () const { return size; }
+
guint get_write_idx () const { return g_atomic_int_get (&write_idx
); }
+
guint get_read_idx () const { return g_atomic_int_get (&read_idx
); }
+
guin
t bufsize () const { return size; }
protected:
T *buf;
protected:
T *buf;
-
size_
t size;
- mutable gint write_
ptr
;
- mutable gint read_
ptr
;
-
size_
t size_mask;
+
guin
t size;
+ mutable gint write_
idx
;
+ mutable gint read_
idx
;
+
guin
t size_mask;
};
};
-template<class T>
size_t
-RingBuffer<T>::read (T *dest,
size_
t cnt)
+template<class T>
guint
+RingBuffer<T>::read (T *dest,
guin
t cnt)
{
{
-
size_
t free_cnt;
-
size_
t cnt2;
-
size_
t to_read;
-
size_
t n1, n2;
-
size_t priv_read_ptr
;
+
guin
t free_cnt;
+
guin
t cnt2;
+
guin
t to_read;
+
guin
t n1, n2;
+
guint priv_read_idx
;
- priv_read_
ptr=g_atomic_int_get(&read_ptr
);
+ priv_read_
idx=g_atomic_int_get(&read_idx
);
if ((free_cnt = read_space ()) == 0) {
return 0;
if ((free_cnt = read_space ()) == 0) {
return 0;
@@
-139,39
+135,39
@@
RingBuffer<T>::read (T *dest, size_t cnt)
to_read = cnt > free_cnt ? free_cnt : cnt;
to_read = cnt > free_cnt ? free_cnt : cnt;
- cnt2 = priv_read_
ptr
+ to_read;
+ cnt2 = priv_read_
idx
+ to_read;
if (cnt2 > size) {
if (cnt2 > size) {
- n1 = size - priv_read_
ptr
;
+ n1 = size - priv_read_
idx
;
n2 = cnt2 & size_mask;
} else {
n1 = to_read;
n2 = 0;
}
n2 = cnt2 & size_mask;
} else {
n1 = to_read;
n2 = 0;
}
- memcpy (dest, &buf[priv_read_
ptr
], n1 * sizeof (T));
- priv_read_
ptr = (priv_read_ptr
+ n1) & size_mask;
+ memcpy (dest, &buf[priv_read_
idx
], n1 * sizeof (T));
+ priv_read_
idx = (priv_read_idx
+ n1) & size_mask;
if (n2) {
memcpy (dest+n1, buf, n2 * sizeof (T));
if (n2) {
memcpy (dest+n1, buf, n2 * sizeof (T));
- priv_read_
ptr
= n2;
+ priv_read_
idx
= n2;
}
}
- g_atomic_int_set(&read_
ptr, priv_read_ptr
);
+ g_atomic_int_set(&read_
idx, priv_read_idx
);
return to_read;
}
return to_read;
}
-template<class T>
size_
t
-RingBuffer<T>::write (T
*src, size_
t cnt)
+template<class T>
guin
t
+RingBuffer<T>::write (T
const *src, guin
t cnt)
{
{
-
size_
t free_cnt;
-
size_
t cnt2;
-
size_
t to_write;
-
size_
t n1, n2;
-
size_t priv_write_ptr
;
+
guin
t free_cnt;
+
guin
t cnt2;
+
guin
t to_write;
+
guin
t n1, n2;
+
guint priv_write_idx
;
- priv_write_
ptr=g_atomic_int_get(&write_ptr
);
+ priv_write_
idx=g_atomic_int_get(&write_idx
);
if ((free_cnt = write_space ()) == 0) {
return 0;
if ((free_cnt = write_space ()) == 0) {
return 0;
@@
-179,38
+175,38
@@
RingBuffer<T>::write (T *src, size_t cnt)
to_write = cnt > free_cnt ? free_cnt : cnt;
to_write = cnt > free_cnt ? free_cnt : cnt;
- cnt2 = priv_write_
ptr
+ to_write;
+ cnt2 = priv_write_
idx
+ to_write;
if (cnt2 > size) {
if (cnt2 > size) {
- n1 = size - priv_write_
ptr
;
+ n1 = size - priv_write_
idx
;
n2 = cnt2 & size_mask;
} else {
n1 = to_write;
n2 = 0;
}
n2 = cnt2 & size_mask;
} else {
n1 = to_write;
n2 = 0;
}
- memcpy (&buf[priv_write_
ptr
], src, n1 * sizeof (T));
- priv_write_
ptr = (priv_write_ptr
+ n1) & size_mask;
+ memcpy (&buf[priv_write_
idx
], src, n1 * sizeof (T));
+ priv_write_
idx = (priv_write_idx
+ n1) & size_mask;
if (n2) {
memcpy (buf, src+n1, n2 * sizeof (T));
if (n2) {
memcpy (buf, src+n1, n2 * sizeof (T));
- priv_write_
ptr
= n2;
+ priv_write_
idx
= n2;
}
}
- g_atomic_int_set(&write_
ptr, priv_write_ptr
);
+ g_atomic_int_set(&write_
idx, priv_write_idx
);
return to_write;
}
template<class T> void
return to_write;
}
template<class T> void
-RingBuffer<T>::get_read_vector (RingBuffer<T>::rw_vector *vec)
+RingBuffer<T>::get_read_vector (
typename
RingBuffer<T>::rw_vector *vec)
{
{
-
size_
t free_cnt;
-
size_
t cnt2;
-
size_
t w, r;
+
guin
t free_cnt;
+
guin
t cnt2;
+
guin
t w, r;
- w = g_atomic_int_get (&write_
ptr
);
- r = g_atomic_int_get (&read_
ptr
);
+ w = g_atomic_int_get (&write_
idx
);
+ r = g_atomic_int_get (&read_
idx
);
if (w > r) {
free_cnt = w - r;
if (w > r) {
free_cnt = w - r;
@@
-237,20
+233,21
@@
RingBuffer<T>::get_read_vector (RingBuffer<T>::rw_vector *vec)
vec->buf[0] = &buf[r];
vec->len[0] = free_cnt;
vec->buf[0] = &buf[r];
vec->len[0] = free_cnt;
+ vec->buf[1] = 0;
vec->len[1] = 0;
}
}
template<class T> void
vec->len[1] = 0;
}
}
template<class T> void
-RingBuffer<T>::get_write_vector (RingBuffer<T>::rw_vector *vec)
+RingBuffer<T>::get_write_vector (
typename
RingBuffer<T>::rw_vector *vec)
{
{
-
size_
t free_cnt;
-
size_
t cnt2;
-
size_
t w, r;
+
guin
t free_cnt;
+
guin
t cnt2;
+
guin
t w, r;
- w = g_atomic_int_get (&write_
ptr
);
- r = g_atomic_int_get (&read_
ptr
);
+ 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;
if (w > r) {
free_cnt = ((r - w + size) & size_mask) - 1;