summaryrefslogtreecommitdiff
path: root/libs/evoral/evoral/RingBuffer.hpp
blob: 3e47042679e7355b047f5a69e4bb4b856566d188 (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
/* This file is part of Evoral.
 * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
 * 
 * Evoral 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.
 * 
 * Evoral 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 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.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef EVORAL_RING_BUFFER_HPP
#define EVORAL_RING_BUFFER_HPP

#include <cassert>
#include <iostream>
#include <glib.h>

namespace Evoral {


/** A lock-free RingBuffer.
 * Read/Write realtime safe.
 * Single-reader Single-writer thread safe.
 */
template <typename Time>
class RingBuffer {
public:

	/** @param size Size in bytes.
	 */
	RingBuffer(size_t size)
		: _size(size)
		, _buf(new Time[size])
	{
		reset();
		assert(read_space() == 0);
		assert(write_space() == size - 1);
	}
	
	virtual ~RingBuffer() {
		delete[] _buf;
	}

	/** Reset(empty) the ringbuffer.
	 * NOT thread safe.
	 */
	void reset() {
		g_atomic_int_set(&_write_ptr, 0);
		g_atomic_int_set(&_read_ptr, 0);
	}

	/** Calculate remaining space for writing
	 */
	size_t write_space() const {
		const size_t w = g_atomic_int_get(&_write_ptr);
		const size_t r = g_atomic_int_get(&_read_ptr);
		
		if (w > r) {
			return ((r - w + _size) % _size) - 1;
		} else if (w < r) {
			return (r - w) - 1;
		} else {
			return _size - 1;
		}
	}
	
	/** Calculate how much still can be read
	 */
	size_t read_space() const {
		const size_t w = g_atomic_int_get(&_write_ptr);
		const size_t r = g_atomic_int_get(&_read_ptr);
		
		if (w > r) {
			return w - r;
		} else {
			return (w - r + _size) % _size;
		}
	}

	/** Report the buffers size
	 */
	size_t capacity() const { return _size; }

	/** Peek at the ringbuffer (read w/o advancing read pointer).
	 * @return how much has been peeked (read cannot exceed the end 
	 * of the buffer):
	 * <pre>
	 * |-------------------------R=============================|
	 *            read-pointer---^
	 * </pre>
	 */
	size_t peek(size_t size, Time* dst);

	/** Peek at the ringbuffer (read w/o advancing read pointer).
	 * @return how much has been peeked (wraps around if read exceeds
	 * the end of the buffer):
	 * <pre>
	 * |===========--------------R=============================|
	 *            read-pointer---^
	 * </pre>
	 */
	bool   full_peek(size_t size, Time* dst);

	/** Read from the ringbuffer. (advances read pointer)
	 * @return how much has been read (read cannot exceed the end 
	 * of the buffer):
	 */
	size_t read(size_t size, Time* dst);

	/** Read from the ringbuffer. (advances read pointer)
	 * @return how much has been peeked (wraps around if read exceeds
	 * the end of the buffer):
	 */
	bool   full_read(size_t size, Time* dst);

	/** Advance read pointer by size
	 */
	bool   skip(size_t size);
	
	void   write(size_t size, const Time* src);

protected:
	mutable int _write_ptr;
	mutable int _read_ptr;
	
	size_t _size; ///< Size (capacity) in bytes
	Time*  _buf;  ///< size, event, size, event...
};


/** Peek at the ringbuffer (read w/o advancing read pointer).
 *
 * Note that a full read may not be done if the data wraps around.
 * Caller must check return value and call again if necessary, or use the 
 * full_peek method which does this automatically.
 */
template<typename Time>
size_t
RingBuffer<Time>::peek(size_t size, Time* dst)
{
	const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);

	const size_t read_size = (priv_read_ptr + size < _size)
			? size
			: _size - priv_read_ptr;
	
	memcpy(dst, &_buf[priv_read_ptr], read_size);

	return read_size;
}


template<typename Time>
bool
RingBuffer<Time>::full_peek(size_t size, Time* dst)
{
	if (read_space() < size) {
		return false;
	}

	const size_t read_size = peek(size, dst);
	
	if (read_size < size) {
		peek(size - read_size, dst + read_size);
	}

	return true;
}


/** Read from the ringbuffer.
 *
 * Note that a full read may not be done if the data wraps around.
 * Caller must check return value and call again if necessary, or use the 
 * full_read method which does this automatically.
 */
template<typename Time>
size_t
RingBuffer<Time>::read(size_t size, Time* dst)
{
	const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);

	const size_t read_size = (priv_read_ptr + size < _size)
			? size
			: _size - priv_read_ptr;
	
	memcpy(dst, &_buf[priv_read_ptr], read_size);
        
	g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);

	return read_size;
}


template<typename Time>
bool
RingBuffer<Time>::full_read(size_t size, Time* dst)
{
	if (read_space() < size) {
		return false;
	}

	const size_t read_size = read(size, dst);
	
	if (read_size < size) {
		read(size - read_size, dst + read_size);
	}

	return true;
}


template<typename Time>
bool
RingBuffer<Time>::skip(size_t size)
{
	if (read_space() < size) {
		std::cerr << "WARNING: Attempt to skip past end of MIDI ring buffer" << std::endl;
		return false;
	}
	
	const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
	g_atomic_int_set(&_read_ptr, (priv_read_ptr + size) % _size);

	return true;
}


template<typename Time>
inline void
RingBuffer<Time>::write(size_t size, const Time* src)
{
	const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
	
	if (priv_write_ptr + size <= _size) {
		memcpy(&_buf[priv_write_ptr], src, size);
		g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size);
	} else {
		const size_t this_size = _size - priv_write_ptr;
		assert(this_size < size);
		assert(priv_write_ptr + this_size <= _size);
		memcpy(&_buf[priv_write_ptr], src, this_size);
		memcpy(&_buf[0], src+this_size, size - this_size);
		g_atomic_int_set(&_write_ptr, size - this_size);
	}
}

	
} // namespace Evoral

#endif // EVORAL_RING_BUFFER_HPP