2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
27 #include "ringbuffer.h"
34 /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
35 * to include an element size. Consequently, parameters and return values for a
36 * size or count is in 'elements', not bytes. Additionally, it only supports
37 * single-consumer/single-provider operation. */
38 struct ll_ringbuffer
{
39 std::atomic
<size_t> write_ptr
;
40 std::atomic
<size_t> read_ptr
;
45 alignas(16) char buf
[];
48 ll_ringbuffer_t
*ll_ringbuffer_create(size_t sz
, size_t elem_sz
, int limit_writes
)
51 size_t power_of_two
= 0;
56 power_of_two
|= power_of_two
>>1;
57 power_of_two
|= power_of_two
>>2;
58 power_of_two
|= power_of_two
>>4;
59 power_of_two
|= power_of_two
>>8;
60 power_of_two
|= power_of_two
>>16;
61 #if SIZE_MAX > UINT_MAX
62 power_of_two
|= power_of_two
>>32;
66 if(power_of_two
< sz
) return NULL
;
68 rb
= static_cast<ll_ringbuffer_t
*>(al_malloc(16, sizeof(*rb
) + power_of_two
*elem_sz
));
71 ATOMIC_INIT(&rb
->write_ptr
, static_cast<size_t>(0));
72 ATOMIC_INIT(&rb
->read_ptr
, static_cast<size_t>(0));
73 rb
->size
= limit_writes
? sz
: power_of_two
;
74 rb
->size_mask
= power_of_two
- 1;
75 rb
->elem_size
= elem_sz
;
79 void ll_ringbuffer_free(ll_ringbuffer_t
*rb
)
84 void ll_ringbuffer_reset(ll_ringbuffer_t
*rb
)
86 rb
->write_ptr
.store(0, std::memory_order_release
);
87 rb
->read_ptr
.store(0, std::memory_order_release
);
88 memset(rb
->buf
, 0, (rb
->size_mask
+1)*rb
->elem_size
);
92 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t
*rb
)
94 size_t w
= rb
->write_ptr
.load(std::memory_order_acquire
);
95 size_t r
= rb
->read_ptr
.load(std::memory_order_acquire
);
96 return (w
-r
) & rb
->size_mask
;
99 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t
*rb
)
101 size_t w
= rb
->write_ptr
.load(std::memory_order_acquire
);
102 size_t r
= rb
->read_ptr
.load(std::memory_order_acquire
);
103 w
= (r
-w
-1) & rb
->size_mask
;
104 return (w
> rb
->size
) ? rb
->size
: w
;
108 size_t ll_ringbuffer_read(ll_ringbuffer_t
*rb
, void *dest
, size_t cnt
)
116 free_cnt
= ll_ringbuffer_read_space(rb
);
117 if(free_cnt
== 0) return 0;
119 to_read
= (cnt
> free_cnt
) ? free_cnt
: cnt
;
120 read_ptr
= rb
->read_ptr
.load(std::memory_order_relaxed
) & rb
->size_mask
;
122 cnt2
= read_ptr
+ to_read
;
123 if(cnt2
> rb
->size_mask
+1)
125 n1
= rb
->size_mask
+1 - read_ptr
;
126 n2
= cnt2
& rb
->size_mask
;
134 memcpy(dest
, &rb
->buf
[read_ptr
*rb
->elem_size
], n1
*rb
->elem_size
);
138 memcpy(static_cast<char*>(dest
) + n1
*rb
->elem_size
,
139 &rb
->buf
[(read_ptr
&rb
->size_mask
)*rb
->elem_size
],
143 rb
->read_ptr
.store(read_ptr
, std::memory_order_release
);
147 size_t ll_ringbuffer_peek(ll_ringbuffer_t
*rb
, void *dest
, size_t cnt
)
155 free_cnt
= ll_ringbuffer_read_space(rb
);
156 if(free_cnt
== 0) return 0;
158 to_read
= (cnt
> free_cnt
) ? free_cnt
: cnt
;
159 read_ptr
= rb
->read_ptr
.load(std::memory_order_relaxed
) & rb
->size_mask
;
161 cnt2
= read_ptr
+ to_read
;
162 if(cnt2
> rb
->size_mask
+1)
164 n1
= rb
->size_mask
+1 - read_ptr
;
165 n2
= cnt2
& rb
->size_mask
;
173 memcpy(dest
, &rb
->buf
[read_ptr
*rb
->elem_size
], n1
*rb
->elem_size
);
177 memcpy(static_cast<char*>(dest
) + n1
*rb
->elem_size
,
178 &rb
->buf
[(read_ptr
&rb
->size_mask
)*rb
->elem_size
],
184 size_t ll_ringbuffer_write(ll_ringbuffer_t
*rb
, const void *src
, size_t cnt
)
192 free_cnt
= ll_ringbuffer_write_space(rb
);
193 if(free_cnt
== 0) return 0;
195 to_write
= (cnt
> free_cnt
) ? free_cnt
: cnt
;
196 write_ptr
= rb
->write_ptr
.load(std::memory_order_relaxed
) & rb
->size_mask
;
198 cnt2
= write_ptr
+ to_write
;
199 if(cnt2
> rb
->size_mask
+1)
201 n1
= rb
->size_mask
+1 - write_ptr
;
202 n2
= cnt2
& rb
->size_mask
;
210 memcpy(&rb
->buf
[write_ptr
*rb
->elem_size
], src
, n1
*rb
->elem_size
);
214 memcpy(&rb
->buf
[(write_ptr
&rb
->size_mask
)*rb
->elem_size
],
215 static_cast<const char*>(src
) + n1
*rb
->elem_size
,
219 rb
->write_ptr
.store(write_ptr
, std::memory_order_release
);
224 void ll_ringbuffer_read_advance(ll_ringbuffer_t
*rb
, size_t cnt
)
226 rb
->read_ptr
.fetch_add(cnt
, std::memory_order_acq_rel
);
229 void ll_ringbuffer_write_advance(ll_ringbuffer_t
*rb
, size_t cnt
)
231 rb
->write_ptr
.fetch_add(cnt
, std::memory_order_acq_rel
);
235 ll_ringbuffer_data_pair
ll_ringbuffer_get_read_vector(const ll_ringbuffer_t
*rb
)
237 ll_ringbuffer_data_pair ret
;
241 size_t w
= rb
->write_ptr
.load(std::memory_order_acquire
);
242 size_t r
= rb
->read_ptr
.load(std::memory_order_acquire
);
245 free_cnt
= (w
-r
) & rb
->size_mask
;
248 if(cnt2
> rb
->size_mask
+1)
250 /* Two part vector: the rest of the buffer after the current write ptr,
251 * plus some from the start of the buffer. */
252 ret
.first
.buf
= const_cast<char*>(&rb
->buf
[r
*rb
->elem_size
]);
253 ret
.first
.len
= rb
->size_mask
+1 - r
;
254 ret
.second
.buf
= const_cast<char*>(rb
->buf
);
255 ret
.second
.len
= cnt2
& rb
->size_mask
;
259 /* Single part vector: just the rest of the buffer */
260 ret
.first
.buf
= const_cast<char*>(&rb
->buf
[r
*rb
->elem_size
]);
261 ret
.first
.len
= free_cnt
;
262 ret
.second
.buf
= nullptr;
269 ll_ringbuffer_data_pair
ll_ringbuffer_get_write_vector(const ll_ringbuffer_t
*rb
)
271 ll_ringbuffer_data_pair ret
;
275 size_t w
= rb
->write_ptr
.load(std::memory_order_acquire
);
276 size_t r
= rb
->read_ptr
.load(std::memory_order_acquire
);
279 free_cnt
= (r
-w
-1) & rb
->size_mask
;
280 if(free_cnt
> rb
->size
) free_cnt
= rb
->size
;
283 if(cnt2
> rb
->size_mask
+1)
285 /* Two part vector: the rest of the buffer after the current write ptr,
286 * plus some from the start of the buffer. */
287 ret
.first
.buf
= const_cast<char*>(&rb
->buf
[w
*rb
->elem_size
]);
288 ret
.first
.len
= rb
->size_mask
+1 - w
;
289 ret
.second
.buf
= const_cast<char*>(rb
->buf
);
290 ret
.second
.len
= cnt2
& rb
->size_mask
;
294 ret
.first
.buf
= const_cast<char*>(&rb
->buf
[w
*rb
->elem_size
]);
295 ret
.first
.len
= free_cnt
;
296 ret
.second
.buf
= nullptr;