globally remove all trailing whitespace from ardour code base.
[ardour.git] / libs / pbd / pbd / ringbuffer.h
1 /*
2     Copyright (C) 2000 Paul Davis & Benno Senoner
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef ringbuffer_h
21 #define ringbuffer_h
22
23 #include <cstring>
24 #include <glib.h>
25
26 #include "pbd/libpbd_visibility.h"
27
28 template<class T>
29 class /*LIBPBD_API*/ RingBuffer
30 {
31   public:
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... */
34         guint power_of_two;
35         for (power_of_two = 1; 1U<<power_of_two < sz; power_of_two++) {}
36         size = 1<<power_of_two;
37         size_mask = size;
38         size_mask -= 1;
39         buf = new T[size];
40         reset ();
41         }
42         
43         virtual ~RingBuffer() {
44                 delete [] buf;
45         }
46
47         void reset () {
48                 /* !!! NOT THREAD SAFE !!! */
49                 g_atomic_int_set (&write_idx, 0);
50                 g_atomic_int_set (&read_idx, 0);
51         }
52
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);
57         }
58         
59         guint read  (T *dest, guint cnt);
60         guint write (T const * src,  guint cnt);
61
62         struct rw_vector {
63             T *buf[2];
64             guint len[2];
65         };
66
67         void get_read_vector (rw_vector *);
68         void get_write_vector (rw_vector *);
69         
70         void decrement_read_idx (guint cnt) {
71                 g_atomic_int_set (&read_idx, (g_atomic_int_get(&read_idx) - cnt) & size_mask);
72         }
73
74         void increment_read_idx (guint cnt) {
75                 g_atomic_int_set (&read_idx, (g_atomic_int_get(&read_idx) + cnt) & size_mask);
76         }
77
78         void increment_write_idx (guint cnt) {
79                 g_atomic_int_set (&write_idx,  (g_atomic_int_get(&write_idx) + cnt) & size_mask);
80         }
81
82         guint write_space () const {
83                 guint w, r;
84                 
85                 w = g_atomic_int_get (&write_idx);
86                 r = g_atomic_int_get (&read_idx);
87                 
88                 if (w > r) {
89                         return ((r - w + size) & size_mask) - 1;
90                 } else if (w < r) {
91                         return (r - w) - 1;
92                 } else {
93                         return size - 1;
94                 }
95         }
96         
97         guint read_space () const {
98                 guint w, r;
99                 
100                 w = g_atomic_int_get (&write_idx);
101                 r = g_atomic_int_get (&read_idx);
102                 
103                 if (w > r) {
104                         return w - r;
105                 } else {
106                         return (w - r + size) & size_mask;
107                 }
108         }
109
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; }
114
115   protected:
116         T *buf;
117         guint size;
118         mutable gint write_idx;
119         mutable gint read_idx;
120         guint size_mask;
121 };
122
123 template<class T> /*LIBPBD_API*/ guint
124 RingBuffer<T>::read (T *dest, guint cnt)
125 {
126         guint free_cnt;
127         guint cnt2;
128         guint to_read;
129         guint n1, n2;
130         guint priv_read_idx;
131
132         priv_read_idx=g_atomic_int_get(&read_idx);
133
134         if ((free_cnt = read_space ()) == 0) {
135                 return 0;
136         }
137
138         to_read = cnt > free_cnt ? free_cnt : cnt;
139
140         cnt2 = priv_read_idx + to_read;
141
142         if (cnt2 > size) {
143                 n1 = size - priv_read_idx;
144                 n2 = cnt2 & size_mask;
145         } else {
146                 n1 = to_read;
147                 n2 = 0;
148         }
149
150         memcpy (dest, &buf[priv_read_idx], n1 * sizeof (T));
151         priv_read_idx = (priv_read_idx + n1) & size_mask;
152
153         if (n2) {
154                 memcpy (dest+n1, buf, n2 * sizeof (T));
155                 priv_read_idx = n2;
156         }
157
158         g_atomic_int_set(&read_idx, priv_read_idx);
159         return to_read;
160 }
161
162 template<class T> /*LIBPBD_API*/ guint
163 RingBuffer<T>::write (T const *src, guint cnt)
164
165 {
166         guint free_cnt;
167         guint cnt2;
168         guint to_write;
169         guint n1, n2;
170         guint priv_write_idx;
171
172         priv_write_idx=g_atomic_int_get(&write_idx);
173
174         if ((free_cnt = write_space ()) == 0) {
175                 return 0;
176         }
177
178         to_write = cnt > free_cnt ? free_cnt : cnt;
179
180         cnt2 = priv_write_idx + to_write;
181
182         if (cnt2 > size) {
183                 n1 = size - priv_write_idx;
184                 n2 = cnt2 & size_mask;
185         } else {
186                 n1 = to_write;
187                 n2 = 0;
188         }
189
190         memcpy (&buf[priv_write_idx], src, n1 * sizeof (T));
191         priv_write_idx = (priv_write_idx + n1) & size_mask;
192
193         if (n2) {
194                 memcpy (buf, src+n1, n2 * sizeof (T));
195                 priv_write_idx = n2;
196         }
197
198         g_atomic_int_set(&write_idx, priv_write_idx);
199         return to_write;
200 }
201
202 template<class T> /*LIBPBD_API*/ void
203 RingBuffer<T>::get_read_vector (typename RingBuffer<T>::rw_vector *vec)
204
205 {
206         guint free_cnt;
207         guint cnt2;
208         guint w, r;
209         
210         w = g_atomic_int_get (&write_idx);
211         r = g_atomic_int_get (&read_idx);
212         
213         if (w > r) {
214                 free_cnt = w - r;
215         } else {
216                 free_cnt = (w - r + size) & size_mask;
217         }
218
219         cnt2 = r + free_cnt;
220
221         if (cnt2 > size) {
222                 /* Two part vector: the rest of the buffer after the
223                    current write ptr, plus some from the start of
224                    the buffer.
225                 */
226
227                 vec->buf[0] = &buf[r];
228                 vec->len[0] = size - r;
229                 vec->buf[1] = buf;
230                 vec->len[1] = cnt2 & size_mask;
231
232         } else {
233                 
234                 /* Single part vector: just the rest of the buffer */
235                 
236                 vec->buf[0] = &buf[r];
237                 vec->len[0] = free_cnt;
238                 vec->buf[1] = 0;
239                 vec->len[1] = 0;
240         }
241 }
242
243 template<class T> /*LIBPBD_API*/ void
244 RingBuffer<T>::get_write_vector (typename RingBuffer<T>::rw_vector *vec)
245
246 {
247         guint free_cnt;
248         guint cnt2;
249         guint w, r;
250         
251         w = g_atomic_int_get (&write_idx);
252         r = g_atomic_int_get (&read_idx);
253         
254         if (w > r) {
255                 free_cnt = ((r - w + size) & size_mask) - 1;
256         } else if (w < r) {
257                 free_cnt = (r - w) - 1;
258         } else {
259                 free_cnt = size - 1;
260         }
261         
262         cnt2 = w + free_cnt;
263
264         if (cnt2 > size) {
265                 
266                 /* Two part vector: the rest of the buffer after the
267                    current write ptr, plus some from the start of
268                    the buffer.
269                 */
270
271                 vec->buf[0] = &buf[w];
272                 vec->len[0] = size - w;
273                 vec->buf[1] = buf;
274                 vec->len[1] = cnt2 & size_mask;
275         } else {
276                 vec->buf[0] = &buf[w];
277                 vec->len[0] = free_cnt;
278                 vec->len[1] = 0;
279         }
280 }
281
282
283 #endif /* __ringbuffer_h__ */