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
32 /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
33 * to include an element size. Consequently, parameters and return values for a
34 * size or count is in 'elements', not bytes. Additionally, it only supports
35 * single-consumer/single-provider operation. */
36 struct ll_ringbuffer
{
37 ATOMIC(size_t) write_ptr
;
38 ATOMIC(size_t) read_ptr
;
44 alignas(16) char buf
[];
47 /* Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes.
48 * The number of elements is rounded up to the next power of two. */
49 ll_ringbuffer_t
*ll_ringbuffer_create(size_t sz
, size_t elem_sz
)
54 power_of_two
= NextPowerOf2(sz
);
58 rb
= al_malloc(16, sizeof(*rb
) + power_of_two
*elem_sz
);
61 ATOMIC_INIT(&rb
->write_ptr
, 0);
62 ATOMIC_INIT(&rb
->read_ptr
, 0);
63 rb
->size
= power_of_two
;
64 rb
->size_mask
= rb
->size
- 1;
65 rb
->elem_size
= elem_sz
;
70 /* Free all data associated with the ringbuffer `rb'. */
71 void ll_ringbuffer_free(ll_ringbuffer_t
*rb
)
77 munlock(rb
, sizeof(*rb
) + rb
->size
*rb
->elem_size
);
78 #endif /* USE_MLOCK */
83 /* Lock the data block of `rb' using the system call 'mlock'. */
84 int ll_ringbuffer_mlock(ll_ringbuffer_t
*rb
)
87 if(!rb
->mlocked
&& mlock(rb
, sizeof(*rb
) + rb
->size
*rb
->elem_size
))
89 #endif /* USE_MLOCK */
94 /* Reset the read and write pointers to zero. This is not thread safe. */
95 void ll_ringbuffer_reset(ll_ringbuffer_t
*rb
)
97 ATOMIC_STORE(&rb
->write_ptr
, 0, almemory_order_release
);
98 ATOMIC_STORE(&rb
->read_ptr
, 0, almemory_order_release
);
99 memset(rb
->buf
, 0, rb
->size
*rb
->elem_size
);
102 /* Return the number of elements available for reading. This is the number of
103 * elements in front of the read pointer and behind the write pointer. */
104 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t
*rb
)
106 size_t w
= ATOMIC_LOAD(&rb
->write_ptr
, almemory_order_acquire
) & rb
->size_mask
;
107 size_t r
= ATOMIC_LOAD(&rb
->read_ptr
, almemory_order_acquire
) & rb
->size_mask
;
108 return (w
-r
) & rb
->size_mask
;
110 /* Return the number of elements available for writing. This is the number of
111 * elements in front of the write pointer and behind the read pointer. */
112 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t
*rb
)
114 size_t w
= ATOMIC_LOAD(&rb
->write_ptr
, almemory_order_acquire
) & rb
->size_mask
;
115 size_t r
= ATOMIC_LOAD(&rb
->read_ptr
, almemory_order_acquire
) & rb
->size_mask
;
116 return (r
-w
-1) & rb
->size_mask
;
119 /* The copying data reader. Copy at most `cnt' elements from `rb' to `dest'.
120 * Returns the actual number of elements copied. */
121 size_t ll_ringbuffer_read(ll_ringbuffer_t
*rb
, char *dest
, size_t cnt
)
129 free_cnt
= ll_ringbuffer_read_space(rb
);
130 if(free_cnt
== 0) return 0;
132 to_read
= (cnt
> free_cnt
) ? free_cnt
: cnt
;
133 read_ptr
= ATOMIC_LOAD(&rb
->read_ptr
, almemory_order_relaxed
) & rb
->size_mask
;
135 cnt2
= read_ptr
+ to_read
;
138 n1
= rb
->size
- read_ptr
;
139 n2
= cnt2
& rb
->size_mask
;
147 memcpy(dest
, &rb
->buf
[read_ptr
*rb
->elem_size
], n1
*rb
->elem_size
);
151 memcpy(dest
+ n1
*rb
->elem_size
, &rb
->buf
[(read_ptr
&rb
->size_mask
)*rb
->elem_size
],
155 ATOMIC_STORE(&rb
->read_ptr
, read_ptr
, almemory_order_release
);
159 /* The copying data reader w/o read pointer advance. Copy at most `cnt'
160 * elements from `rb' to `dest'. Returns the actual number of elements copied.
162 size_t ll_ringbuffer_peek(ll_ringbuffer_t
*rb
, char *dest
, size_t cnt
)
170 free_cnt
= ll_ringbuffer_read_space(rb
);
171 if(free_cnt
== 0) return 0;
173 to_read
= (cnt
> free_cnt
) ? free_cnt
: cnt
;
174 read_ptr
= ATOMIC_LOAD(&rb
->read_ptr
, almemory_order_relaxed
) & rb
->size_mask
;
176 cnt2
= read_ptr
+ to_read
;
179 n1
= rb
->size
- read_ptr
;
180 n2
= cnt2
& rb
->size_mask
;
188 memcpy(dest
, &rb
->buf
[read_ptr
*rb
->elem_size
], n1
*rb
->elem_size
);
192 memcpy(dest
+ n1
*rb
->elem_size
, &rb
->buf
[(read_ptr
&rb
->size_mask
)*rb
->elem_size
],
198 /* The copying data writer. Copy at most `cnt' elements to `rb' from `src'.
199 * Returns the actual number of elements copied. */
200 size_t ll_ringbuffer_write(ll_ringbuffer_t
*rb
, const char *src
, size_t cnt
)
208 free_cnt
= ll_ringbuffer_write_space(rb
);
209 if(free_cnt
== 0) return 0;
211 to_write
= (cnt
> free_cnt
) ? free_cnt
: cnt
;
212 write_ptr
= ATOMIC_LOAD(&rb
->write_ptr
, almemory_order_relaxed
) & rb
->size_mask
;
214 cnt2
= write_ptr
+ to_write
;
217 n1
= rb
->size
- write_ptr
;
218 n2
= cnt2
& rb
->size_mask
;
226 memcpy(&rb
->buf
[write_ptr
*rb
->elem_size
], src
, n1
*rb
->elem_size
);
230 memcpy(&rb
->buf
[(write_ptr
&rb
->size_mask
)*rb
->elem_size
], src
+ n1
*rb
->elem_size
,
234 ATOMIC_STORE(&rb
->write_ptr
, write_ptr
, almemory_order_release
);
238 /* Advance the read pointer `cnt' places. */
239 void ll_ringbuffer_read_advance(ll_ringbuffer_t
*rb
, size_t cnt
)
241 ATOMIC_ADD(&rb
->read_ptr
, cnt
, almemory_order_acq_rel
);
244 /* Advance the write pointer `cnt' places. */
245 void ll_ringbuffer_write_advance(ll_ringbuffer_t
*rb
, size_t cnt
)
247 ATOMIC_ADD(&rb
->write_ptr
, cnt
, almemory_order_acq_rel
);
250 /* The non-copying data reader. `vec' is an array of two places. Set the values
251 * at `vec' to hold the current readable data at `rb'. If the readable data is
252 * in one segment the second segment has zero length. */
253 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t
*rb
, ll_ringbuffer_data_t
* vec
)
259 w
= ATOMIC_LOAD(&rb
->write_ptr
, almemory_order_acquire
) & rb
->size_mask
;
260 r
= ATOMIC_LOAD(&rb
->read_ptr
, almemory_order_acquire
) & rb
->size_mask
;
261 free_cnt
= (w
-r
) & rb
->size_mask
;
266 /* Two part vector: the rest of the buffer after the current write ptr,
267 * plus some from the start of the buffer. */
268 vec
[0].buf
= (char*)&rb
->buf
[r
*rb
->elem_size
];
269 vec
[0].len
= rb
->size
- r
;
270 vec
[1].buf
= (char*)rb
->buf
;
271 vec
[1].len
= cnt2
& rb
->size_mask
;
275 /* Single part vector: just the rest of the buffer */
276 vec
[0].buf
= (char*)&rb
->buf
[r
*rb
->elem_size
];
277 vec
[0].len
= free_cnt
;
283 /* The non-copying data writer. `vec' is an array of two places. Set the values
284 * at `vec' to hold the current writeable data at `rb'. If the writeable data
285 * is in one segment the second segment has zero length. */
286 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t
*rb
, ll_ringbuffer_data_t
*vec
)
292 w
= ATOMIC_LOAD(&rb
->write_ptr
, almemory_order_acquire
) & rb
->size_mask
;
293 r
= ATOMIC_LOAD(&rb
->read_ptr
, almemory_order_acquire
) & rb
->size_mask
;
294 free_cnt
= (r
-w
-1) & rb
->size_mask
;
299 /* Two part vector: the rest of the buffer after the current write ptr,
300 * plus some from the start of the buffer. */
301 vec
[0].buf
= (char*)&rb
->buf
[w
*rb
->elem_size
];
302 vec
[0].len
= rb
->size
- w
;
303 vec
[1].buf
= (char*)rb
->buf
;
304 vec
[1].len
= cnt2
& rb
->size_mask
;
308 vec
[0].buf
= (char*)&rb
->buf
[w
*rb
->elem_size
];
309 vec
[0].len
= free_cnt
;