1 /* This file is part of Evoral.
2 * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
4 * Evoral is free software; you can redistribute it and/or modify it under the
5 * terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 2 of the License, or (at your option) any later
9 * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #ifndef EVORAL_RING_BUFFER_HPP
19 #define EVORAL_RING_BUFFER_HPP
28 /** A lock-free RingBuffer.
29 * Read/Write realtime safe.
30 * Single-reader Single-writer thread safe.
36 /** @param size Size in bytes.
38 RingBuffer(size_t size)
43 assert(read_space() == 0);
44 assert(write_space() == size - 1);
47 virtual ~RingBuffer() {
51 /** Reset(empty) the ringbuffer.
55 g_atomic_int_set(&_write_ptr, 0);
56 g_atomic_int_set(&_read_ptr, 0);
59 /** Calculate remaining space for writing
61 size_t write_space() const {
62 const size_t w = g_atomic_int_get(&_write_ptr);
63 const size_t r = g_atomic_int_get(&_read_ptr);
66 return ((r - w + _size) % _size) - 1;
74 /** Calculate how much still can be read
76 size_t read_space() const {
77 const size_t w = g_atomic_int_get(&_write_ptr);
78 const size_t r = g_atomic_int_get(&_read_ptr);
83 return (w - r + _size) % _size;
87 /** Report the buffers size
89 size_t capacity() const { return _size; }
91 /** Peek at the ringbuffer (read w/o advancing read pointer).
92 * @return how much has been peeked (read cannot exceed the end
95 * |-------------------------R=============================|
99 size_t peek(size_t size, T* dst);
101 /** Peek at the ringbuffer (read w/o advancing read pointer).
102 * @return how much has been peeked (wraps around if read exceeds
103 * the end of the buffer):
105 * |===========--------------R=============================|
109 bool full_peek(size_t size, T* dst);
111 /** Read from the ringbuffer. (advances read pointer)
112 * @return how much has been read (read cannot exceed the end
115 size_t read(size_t size, T* dst);
117 /** Read from the ringbuffer. (advances read pointer)
118 * @return how much has been peeked (wraps around if read exceeds
119 * the end of the buffer):
121 bool full_read(size_t size, T* dst);
123 /** Advance read pointer by size
125 bool skip(size_t size);
127 void write(size_t size, const T* src);
130 mutable int _write_ptr;
131 mutable int _read_ptr;
133 size_t _size; ///< Size (capacity) in bytes
134 T* _buf; ///< size, event, size, event...
138 /** Peek at the ringbuffer (read w/o advancing read pointer).
140 * Note that a full read may not be done if the data wraps around.
141 * Caller must check return value and call again if necessary, or use the
142 * full_peek method which does this automatically.
146 RingBuffer<T>::peek(size_t size, T* dst)
148 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
150 const size_t read_size = (priv_read_ptr + size < _size)
152 : _size - priv_read_ptr;
154 memcpy(dst, &_buf[priv_read_ptr], read_size);
162 RingBuffer<T>::full_peek(size_t size, T* dst)
164 if (read_space() < size) {
168 const size_t read_size = peek(size, dst);
170 if (read_size < size) {
171 peek(size - read_size, dst + read_size);
178 /** Read from the ringbuffer.
180 * Note that a full read may not be done if the data wraps around.
181 * Caller must check return value and call again if necessary, or use the
182 * full_read method which does this automatically.
186 RingBuffer<T>::read(size_t size, T* dst)
188 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
190 const size_t read_size = (priv_read_ptr + size < _size)
192 : _size - priv_read_ptr;
194 memcpy(dst, &_buf[priv_read_ptr], read_size);
196 g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);
204 RingBuffer<T>::full_read(size_t size, T* dst)
206 if (read_space() < size) {
210 const size_t read_size = read(size, dst);
212 if (read_size < size) {
213 read(size - read_size, dst + read_size);
222 RingBuffer<T>::skip(size_t size)
224 if (read_space() < size) {
225 std::cerr << "WARNING: Attempt to skip past end of MIDI ring buffer" << std::endl;
229 const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
230 g_atomic_int_set(&_read_ptr, (priv_read_ptr + size) % _size);
238 RingBuffer<T>::write(size_t size, const T* src)
240 const size_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
242 if (priv_write_ptr + size <= _size) {
243 memcpy(&_buf[priv_write_ptr], src, size);
244 g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size);
246 const size_t this_size = _size - priv_write_ptr;
247 assert(this_size < size);
248 assert(priv_write_ptr + this_size <= _size);
249 memcpy(&_buf[priv_write_ptr], src, this_size);
250 memcpy(&_buf[0], src+this_size, size - this_size);
251 g_atomic_int_set(&_write_ptr, size - this_size);
256 } // namespace Evoral
258 #endif // EVORAL_RING_BUFFER_HPP