RAUL  0.8.0
RingBuffer.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
3  *
4  * Raul 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
7  * version.
8  *
9  * Raul 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.
12  *
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
16  */
17 
18 #ifndef RAUL_RING_BUFFER_HPP
19 #define RAUL_RING_BUFFER_HPP
20 
21 #include <stdint.h>
22 
23 #include <cassert>
24 #include <cstdlib>
25 #include <cstring>
26 #include <iostream>
27 
28 #include <glib.h>
29 
30 #include "raul/log.hpp"
31 
32 namespace Raul {
33 
34 
40 class RingBuffer {
41 public:
44  explicit RingBuffer(uint32_t size)
45  : _buf(static_cast<char*>(malloc(size)))
46  , _size(size)
47  {
48  reset();
49  assert(read_space() == 0);
50  assert(write_space() == size - 1);
51  }
52 
53  virtual ~RingBuffer() {
54  free(_buf);
55  }
56 
60  void reset() {
61  g_atomic_int_set(&_write_ptr, 0);
62  g_atomic_int_set(&_read_ptr, 0);
63  }
64 
65  uint32_t write_space() const {
66  const uint32_t w = g_atomic_int_get(&_write_ptr);
67  const uint32_t r = g_atomic_int_get(&_read_ptr);
68 
69  if (w > r) {
70  return ((r - w + _size) % _size) - 1;
71  } else if (w < r) {
72  return (r - w) - 1;
73  } else {
74  return _size - 1;
75  }
76  }
77 
78  uint32_t read_space() const {
79  const uint32_t w = g_atomic_int_get(&_write_ptr);
80  const uint32_t r = g_atomic_int_get(&_read_ptr);
81 
82  if (w > r) {
83  return w - r;
84  } else {
85  return (w - r + _size) % _size;
86  }
87  }
88 
89  uint32_t capacity() const { return _size; }
90 
91  uint32_t peek(uint32_t size, void* dst);
92  bool full_peek(uint32_t size, void* dst);
93 
94  uint32_t read(uint32_t size, void* dst);
95  bool full_read(uint32_t size, void* dst);
96 
97  bool skip(uint32_t size);
98 
99  void write(uint32_t size, const void* src);
100 
101 protected:
102  mutable uint32_t _write_ptr;
103  mutable uint32_t _read_ptr;
104 
105  char* const _buf;
106  const uint32_t _size;
107 };
108 
109 
116 inline uint32_t
117 RingBuffer::peek(uint32_t size, void* dst)
118 {
119  const uint32_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
120 
121  const uint32_t read_size = (priv_read_ptr + size < _size)
122  ? size
123  : _size - priv_read_ptr;
124 
125  memcpy(dst, &_buf[priv_read_ptr], read_size);
126 
127  return read_size;
128 }
129 
130 
131 inline bool
132 RingBuffer::full_peek(uint32_t size, void* dst)
133 {
134  if (read_space() < size) {
135  return false;
136  }
137 
138  const uint32_t read_size = peek(size, dst);
139 
140  if (read_size < size) {
141  peek(size - read_size, (char*)dst + read_size);
142  }
143 
144  return true;
145 }
146 
147 
154 inline uint32_t
155 RingBuffer::read(uint32_t size, void* dst)
156 {
157  const uint32_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
158 
159  const uint32_t read_size = (priv_read_ptr + size < _size)
160  ? size
161  : _size - priv_read_ptr;
162 
163  memcpy(dst, &_buf[priv_read_ptr], read_size);
164 
165  g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size);
166 
167  return read_size;
168 }
169 
170 
171 inline bool
172 RingBuffer::full_read(uint32_t size, void* dst)
173 {
174  if (read_space() < size) {
175  return false;
176  }
177 
178  const uint32_t read_size = read(size, dst);
179 
180  if (read_size < size) {
181  read(size - read_size, (char*)dst + read_size);
182  }
183 
184  return true;
185 }
186 
187 
188 inline bool
189 RingBuffer::skip(uint32_t size)
190 {
191  if (read_space() < size) {
192  warn << "Attempt to skip past end of RingBuffer" << std::endl;
193  return false;
194  }
195 
196  const uint32_t priv_read_ptr = g_atomic_int_get(&_read_ptr);
197  g_atomic_int_set(&_read_ptr, (priv_read_ptr + size) % _size);
198 
199  return true;
200 }
201 
202 
203 inline void
204 RingBuffer::write(uint32_t size, const void* src)
205 {
206  const uint32_t priv_write_ptr = g_atomic_int_get(&_write_ptr);
207 
208  if (priv_write_ptr + size <= _size) {
209  memcpy(&_buf[priv_write_ptr], src, size);
210  g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size);
211  } else {
212  const uint32_t this_size = _size - priv_write_ptr;
213  assert(this_size < size);
214  assert(priv_write_ptr + this_size <= _size);
215  memcpy(&_buf[priv_write_ptr], src, this_size);
216  memcpy(&_buf[0], (char*)src + this_size, size - this_size);
217  g_atomic_int_set(&_write_ptr, size - this_size);
218  }
219 }
220 
221 
222 } // namespace Raul
223 
224 #endif // RAUL_RING_BUFFER_HPP
225 
A lock-free RingBuffer.
Definition: RingBuffer.hpp:40
char *const _buf
Contents.
Definition: RingBuffer.hpp:105
uint32_t peek(uint32_t size, void *dst)
Peek at the ringbuffer (read w/o advancing read pointer).
Definition: RingBuffer.hpp:117
void reset()
Reset(empty) the ringbuffer.
Definition: RingBuffer.hpp:60
uint32_t read(uint32_t size, void *dst)
Read from the ringbuffer.
Definition: RingBuffer.hpp:155
const uint32_t _size
Size (capacity) in bytes.
Definition: RingBuffer.hpp:106
RingBuffer(uint32_t size)
Definition: RingBuffer.hpp:44