2 Copyright (C) 2000 Paul Davis & Benno Senoner
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "pbd/libpbd_visibility.h"
29 class /*LIBPBD_API*/ RingBuffer
32 RingBuffer (guint sz) {
33 // size = ffs(sz); /* find first [bit] set is a single inlined assembly instruction. But it looks like the API rounds up so... */
35 for (power_of_two = 1; 1U<<power_of_two < sz; power_of_two++) {}
36 size = 1<<power_of_two;
43 virtual ~RingBuffer() {
48 /* !!! NOT THREAD SAFE !!! */
49 g_atomic_int_set (&write_idx, 0);
50 g_atomic_int_set (&read_idx, 0);
53 void set (guint r, guint w) {
54 /* !!! NOT THREAD SAFE !!! */
55 g_atomic_int_set (&write_idx, w);
56 g_atomic_int_set (&read_idx, r);
59 guint read (T *dest, guint cnt);
60 guint write (T const * src, guint cnt);
67 void get_read_vector (rw_vector *);
68 void get_write_vector (rw_vector *);
70 void decrement_read_idx (guint cnt) {
71 g_atomic_int_set (&read_idx, (g_atomic_int_get(&read_idx) - cnt) & size_mask);
74 void increment_read_idx (guint cnt) {
75 g_atomic_int_set (&read_idx, (g_atomic_int_get(&read_idx) + cnt) & size_mask);
78 void increment_write_idx (guint cnt) {
79 g_atomic_int_set (&write_idx, (g_atomic_int_get(&write_idx) + cnt) & size_mask);
82 guint write_space () const {
85 w = g_atomic_int_get (&write_idx);
86 r = g_atomic_int_get (&read_idx);
89 return ((r - w + size) & size_mask) - 1;
97 guint read_space () const {
100 w = g_atomic_int_get (&write_idx);
101 r = g_atomic_int_get (&read_idx);
106 return (w - r + size) & size_mask;
110 T *buffer () { return buf; }
111 guint get_write_idx () const { return g_atomic_int_get (&write_idx); }
112 guint get_read_idx () const { return g_atomic_int_get (&read_idx); }
113 guint bufsize () const { return size; }
118 mutable gint write_idx;
119 mutable gint read_idx;
123 template<class T> /*LIBPBD_API*/ guint
124 RingBuffer<T>::read (T *dest, guint cnt)
132 priv_read_idx=g_atomic_int_get(&read_idx);
134 if ((free_cnt = read_space ()) == 0) {
138 to_read = cnt > free_cnt ? free_cnt : cnt;
140 cnt2 = priv_read_idx + to_read;
143 n1 = size - priv_read_idx;
144 n2 = cnt2 & size_mask;
150 memcpy (dest, &buf[priv_read_idx], n1 * sizeof (T));
151 priv_read_idx = (priv_read_idx + n1) & size_mask;
154 memcpy (dest+n1, buf, n2 * sizeof (T));
158 g_atomic_int_set(&read_idx, priv_read_idx);
162 template<class T> /*LIBPBD_API*/ guint
163 RingBuffer<T>::write (T const *src, guint cnt)
170 guint priv_write_idx;
172 priv_write_idx=g_atomic_int_get(&write_idx);
174 if ((free_cnt = write_space ()) == 0) {
178 to_write = cnt > free_cnt ? free_cnt : cnt;
180 cnt2 = priv_write_idx + to_write;
183 n1 = size - priv_write_idx;
184 n2 = cnt2 & size_mask;
190 memcpy (&buf[priv_write_idx], src, n1 * sizeof (T));
191 priv_write_idx = (priv_write_idx + n1) & size_mask;
194 memcpy (buf, src+n1, n2 * sizeof (T));
198 g_atomic_int_set(&write_idx, priv_write_idx);
202 template<class T> /*LIBPBD_API*/ void
203 RingBuffer<T>::get_read_vector (typename RingBuffer<T>::rw_vector *vec)
210 w = g_atomic_int_get (&write_idx);
211 r = g_atomic_int_get (&read_idx);
216 free_cnt = (w - r + size) & size_mask;
222 /* Two part vector: the rest of the buffer after the
223 current write ptr, plus some from the start of
227 vec->buf[0] = &buf[r];
228 vec->len[0] = size - r;
230 vec->len[1] = cnt2 & size_mask;
234 /* Single part vector: just the rest of the buffer */
236 vec->buf[0] = &buf[r];
237 vec->len[0] = free_cnt;
243 template<class T> /*LIBPBD_API*/ void
244 RingBuffer<T>::get_write_vector (typename RingBuffer<T>::rw_vector *vec)
251 w = g_atomic_int_get (&write_idx);
252 r = g_atomic_int_get (&read_idx);
255 free_cnt = ((r - w + size) & size_mask) - 1;
257 free_cnt = (r - w) - 1;
266 /* Two part vector: the rest of the buffer after the
267 current write ptr, plus some from the start of
271 vec->buf[0] = &buf[w];
272 vec->len[0] = size - w;
274 vec->len[1] = cnt2 & size_mask;
276 vec->buf[0] = &buf[w];
277 vec->len[0] = free_cnt;
283 #endif /* __ringbuffer_h__ */