summaryrefslogtreecommitdiff
path: root/libs/pbd/pbd/ringbuffer.h
blob: 58c463ecbe5373ea344f0a536a1c17930ffea39a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
    Copyright (C) 2000 Paul Davis & Benno Senoner

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef ringbuffer_h
#define ringbuffer_h

#include <cstring>
#include <glib.h>

#include "pbd/libpbd_visibility.h"

template<class T>
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 ();
	}

	virtual ~RingBuffer() {
		delete [] buf;
	}

	void reset () {
		/* !!! NOT THREAD SAFE !!! */
		g_atomic_int_set (&write_idx, 0);
		g_atomic_int_set (&read_idx, 0);
	}

	void set (guint r, guint w) {
		/* !!! NOT THREAD SAFE !!! */
		g_atomic_int_set (&write_idx, w);
		g_atomic_int_set (&read_idx, r);
	}

	guint read  (T *dest, guint cnt);
	guint write (T const * src,  guint cnt);

	struct rw_vector {
	    T *buf[2];
	    guint len[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 () 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 (r - w) - 1;
		} else {
			return size - 1;
		}
	}

	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 {
			return (w - r + size) & size_mask;
		}
	}

	T *buffer () { return buf; }
	guint get_write_idx () const { return g_atomic_int_get (&write_idx); }
	guint get_read_idx () const { return g_atomic_int_get (&read_idx); }
	guint bufsize () const { return size; }

  protected:
	T *buf;
	guint size;
	mutable gint write_idx;
	mutable gint read_idx;
	guint size_mask;
};

template<class T> /*LIBPBD_API*/ guint
RingBuffer<T>::read (T *dest, guint cnt)
{
        guint free_cnt;
        guint cnt2;
        guint to_read;
        guint n1, n2;
        guint priv_read_idx;

        priv_read_idx=g_atomic_int_get(&read_idx);

        if ((free_cnt = read_space ()) == 0) {
                return 0;
        }

        to_read = cnt > free_cnt ? free_cnt : cnt;

        cnt2 = priv_read_idx + to_read;

        if (cnt2 > size) {
                n1 = size - priv_read_idx;
                n2 = cnt2 & size_mask;
        } else {
                n1 = to_read;
                n2 = 0;
        }

        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));
                priv_read_idx = n2;
        }

        g_atomic_int_set(&read_idx, priv_read_idx);
        return to_read;
}

template<class T> /*LIBPBD_API*/ guint
RingBuffer<T>::write (T const *src, guint cnt)

{
        guint free_cnt;
        guint cnt2;
        guint to_write;
        guint n1, n2;
        guint priv_write_idx;

        priv_write_idx=g_atomic_int_get(&write_idx);

        if ((free_cnt = write_space ()) == 0) {
                return 0;
        }

        to_write = cnt > free_cnt ? free_cnt : cnt;

        cnt2 = priv_write_idx + to_write;

        if (cnt2 > size) {
                n1 = size - priv_write_idx;
                n2 = cnt2 & size_mask;
        } else {
                n1 = to_write;
                n2 = 0;
        }

        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));
                priv_write_idx = n2;
        }

        g_atomic_int_set(&write_idx, priv_write_idx);
        return to_write;
}

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 {
		free_cnt = (w - r + size) & size_mask;
	}

	cnt2 = r + free_cnt;

	if (cnt2 > size) {
		/* Two part vector: the rest of the buffer after the
		   current write ptr, plus some from the start of
		   the buffer.
		*/

		vec->buf[0] = &buf[r];
		vec->len[0] = size - r;
		vec->buf[1] = buf;
		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> /*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) {
		free_cnt = (r - w) - 1;
	} 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
		   the buffer.
		*/

		vec->buf[0] = &buf[w];
		vec->len[0] = size - w;
		vec->buf[1] = buf;
		vec->len[1] = cnt2 & size_mask;
	} else {
		vec->buf[0] = &buf[w];
		vec->len[0] = free_cnt;
		vec->len[1] = 0;
	}
}


#endif /* __ringbuffer_h__ */