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.
30 RingBuffer (guint sz) {
31 // size = ffs(sz); /* find first [bit] set is a single inlined assembly instruction. But it looks like the API rounds up so... */
33 for (power_of_two = 1; 1U<<power_of_two < sz; power_of_two++) {}
34 size = 1<<power_of_two;
41 virtual ~RingBuffer() {
46 /* !!! NOT THREAD SAFE !!! */
47 g_atomic_int_set (&write_idx, 0);
48 g_atomic_int_set (&read_idx, 0);
51 void set (guint r, guint w) {
52 /* !!! NOT THREAD SAFE !!! */
53 g_atomic_int_set (&write_idx, w);
54 g_atomic_int_set (&read_idx, r);
57 guint read (T *dest, guint cnt);
58 guint write (T const * src, guint cnt);
65 void get_read_vector (rw_vector *);
66 void get_write_vector (rw_vector *);
68 void decrement_read_idx (guint cnt) {
69 g_atomic_int_set (&read_idx, (g_atomic_int_get(&read_idx) - cnt) & size_mask);
72 void increment_read_idx (guint cnt) {
73 g_atomic_int_set (&read_idx, (g_atomic_int_get(&read_idx) + cnt) & size_mask);
76 void increment_write_idx (guint cnt) {
77 g_atomic_int_set (&write_idx, (g_atomic_int_get(&write_idx) + cnt) & size_mask);
80 guint write_space () {
83 w = g_atomic_int_get (&write_idx);
84 r = g_atomic_int_get (&read_idx);
87 return ((r - w + size) & size_mask) - 1;
98 w = g_atomic_int_get (&write_idx);
99 r = g_atomic_int_get (&read_idx);
104 return (w - r + size) & size_mask;
108 T *buffer () { return buf; }
109 guint get_write_idx () const { return g_atomic_int_get (&write_idx); }
110 guint get_read_idx () const { return g_atomic_int_get (&read_idx); }
111 guint bufsize () const { return size; }
116 mutable gint write_idx;
117 mutable gint read_idx;
121 template<class T> guint
122 RingBuffer<T>::read (T *dest, guint cnt)
130 priv_read_idx=g_atomic_int_get(&read_idx);
132 if ((free_cnt = read_space ()) == 0) {
136 to_read = cnt > free_cnt ? free_cnt : cnt;
138 cnt2 = priv_read_idx + to_read;
141 n1 = size - priv_read_idx;
142 n2 = cnt2 & size_mask;
148 memcpy (dest, &buf[priv_read_idx], n1 * sizeof (T));
149 priv_read_idx = (priv_read_idx + n1) & size_mask;
152 memcpy (dest+n1, buf, n2 * sizeof (T));
156 g_atomic_int_set(&read_idx, priv_read_idx);
160 template<class T> guint
161 RingBuffer<T>::write (T const *src, guint cnt)
168 guint priv_write_idx;
170 priv_write_idx=g_atomic_int_get(&write_idx);
172 if ((free_cnt = write_space ()) == 0) {
176 to_write = cnt > free_cnt ? free_cnt : cnt;
178 cnt2 = priv_write_idx + to_write;
181 n1 = size - priv_write_idx;
182 n2 = cnt2 & size_mask;
188 memcpy (&buf[priv_write_idx], src, n1 * sizeof (T));
189 priv_write_idx = (priv_write_idx + n1) & size_mask;
192 memcpy (buf, src+n1, n2 * sizeof (T));
196 g_atomic_int_set(&write_idx, priv_write_idx);
200 template<class T> void
201 RingBuffer<T>::get_read_vector (RingBuffer<T>::rw_vector *vec)
208 w = g_atomic_int_get (&write_idx);
209 r = g_atomic_int_get (&read_idx);
214 free_cnt = (w - r + size) & size_mask;
220 /* Two part vector: the rest of the buffer after the
221 current write ptr, plus some from the start of
225 vec->buf[0] = &buf[r];
226 vec->len[0] = size - r;
228 vec->len[1] = cnt2 & size_mask;
232 /* Single part vector: just the rest of the buffer */
234 vec->buf[0] = &buf[r];
235 vec->len[0] = free_cnt;
240 template<class T> void
241 RingBuffer<T>::get_write_vector (RingBuffer<T>::rw_vector *vec)
248 w = g_atomic_int_get (&write_idx);
249 r = g_atomic_int_get (&read_idx);
252 free_cnt = ((r - w + size) & size_mask) - 1;
254 free_cnt = (r - w) - 1;
263 /* Two part vector: the rest of the buffer after the
264 current write ptr, plus some from the start of
268 vec->buf[0] = &buf[w];
269 vec->len[0] = size - w;
271 vec->len[1] = cnt2 & size_mask;
273 vec->buf[0] = &buf[w];
274 vec->len[0] = free_cnt;
280 #endif /* __ringbuffer_h__ */