Update ChangeLog for the dither-depth config option
[openal-soft.git] / Alc / alcRing.c
blobd72b34f1ac0b8a3e1d00e7a4854cf0fda4291002
1 /**
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
21 #include "config.h"
23 #include <string.h>
24 #include <stdlib.h>
26 #include "alMain.h"
27 #include "threads.h"
28 #include "almalloc.h"
29 #include "compat.h"
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;
39 size_t size;
40 size_t size_mask;
41 size_t elem_size;
42 int mlocked;
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)
51 ll_ringbuffer_t *rb;
52 ALuint power_of_two;
54 power_of_two = NextPowerOf2(sz);
55 if(power_of_two < sz)
56 return NULL;
58 rb = al_malloc(16, sizeof(*rb) + power_of_two*elem_sz);
59 if(!rb) return NULL;
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;
66 rb->mlocked = 0;
67 return rb;
70 /* Free all data associated with the ringbuffer `rb'. */
71 void ll_ringbuffer_free(ll_ringbuffer_t *rb)
73 if(rb)
75 #ifdef USE_MLOCK
76 if(rb->mlocked)
77 munlock(rb, sizeof(*rb) + rb->size*rb->elem_size);
78 #endif /* USE_MLOCK */
79 al_free(rb);
83 /* Lock the data block of `rb' using the system call 'mlock'. */
84 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb)
86 #ifdef USE_MLOCK
87 if(!rb->mlocked && mlock(rb, sizeof(*rb) + rb->size*rb->elem_size))
88 return -1;
89 #endif /* USE_MLOCK */
90 rb->mlocked = 1;
91 return 0;
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(&CONST_CAST(ll_ringbuffer_t*,rb)->write_ptr, almemory_order_acquire);
107 size_t r = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->read_ptr, almemory_order_acquire);
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(&CONST_CAST(ll_ringbuffer_t*,rb)->write_ptr, almemory_order_acquire);
115 size_t r = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->read_ptr, almemory_order_acquire);
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)
123 size_t read_ptr;
124 size_t free_cnt;
125 size_t cnt2;
126 size_t to_read;
127 size_t n1, n2;
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;
136 if(cnt2 > rb->size)
138 n1 = rb->size - read_ptr;
139 n2 = cnt2 & rb->size_mask;
141 else
143 n1 = to_read;
144 n2 = 0;
147 memcpy(dest, &rb->buf[read_ptr*rb->elem_size], n1*rb->elem_size);
148 read_ptr += n1;
149 if(n2)
151 memcpy(dest + n1*rb->elem_size, &rb->buf[(read_ptr&rb->size_mask)*rb->elem_size],
152 n2*rb->elem_size);
153 read_ptr += n2;
155 ATOMIC_STORE(&rb->read_ptr, read_ptr, almemory_order_release);
156 return to_read;
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)
164 size_t free_cnt;
165 size_t cnt2;
166 size_t to_read;
167 size_t n1, n2;
168 size_t read_ptr;
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;
177 if(cnt2 > rb->size)
179 n1 = rb->size - read_ptr;
180 n2 = cnt2 & rb->size_mask;
182 else
184 n1 = to_read;
185 n2 = 0;
188 memcpy(dest, &rb->buf[read_ptr*rb->elem_size], n1*rb->elem_size);
189 if(n2)
191 read_ptr += n1;
192 memcpy(dest + n1*rb->elem_size, &rb->buf[(read_ptr&rb->size_mask)*rb->elem_size],
193 n2*rb->elem_size);
195 return to_read;
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)
202 size_t write_ptr;
203 size_t free_cnt;
204 size_t cnt2;
205 size_t to_write;
206 size_t n1, n2;
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;
215 if(cnt2 > rb->size)
217 n1 = rb->size - write_ptr;
218 n2 = cnt2 & rb->size_mask;
220 else
222 n1 = to_write;
223 n2 = 0;
226 memcpy(&rb->buf[write_ptr*rb->elem_size], src, n1*rb->elem_size);
227 write_ptr += n1;
228 if(n2)
230 memcpy(&rb->buf[(write_ptr&rb->size_mask)*rb->elem_size], src + n1*rb->elem_size,
231 n2*rb->elem_size);
232 write_ptr += n2;
234 ATOMIC_STORE(&rb->write_ptr, write_ptr, almemory_order_release);
235 return to_write;
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)
255 size_t free_cnt;
256 size_t cnt2;
257 size_t w, r;
259 w = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->write_ptr, almemory_order_acquire);
260 r = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->read_ptr, almemory_order_acquire);
261 w &= rb->size_mask;
262 r &= rb->size_mask;
263 free_cnt = (w-r) & rb->size_mask;
265 cnt2 = r + free_cnt;
266 if(cnt2 > rb->size)
268 /* Two part vector: the rest of the buffer after the current write ptr,
269 * plus some from the start of the buffer. */
270 vec[0].buf = (char*)&rb->buf[r*rb->elem_size];
271 vec[0].len = rb->size - r;
272 vec[1].buf = (char*)rb->buf;
273 vec[1].len = cnt2 & rb->size_mask;
275 else
277 /* Single part vector: just the rest of the buffer */
278 vec[0].buf = (char*)&rb->buf[r*rb->elem_size];
279 vec[0].len = free_cnt;
280 vec[1].buf = NULL;
281 vec[1].len = 0;
285 /* The non-copying data writer. `vec' is an array of two places. Set the values
286 * at `vec' to hold the current writeable data at `rb'. If the writeable data
287 * is in one segment the second segment has zero length. */
288 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec)
290 size_t free_cnt;
291 size_t cnt2;
292 size_t w, r;
294 w = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->write_ptr, almemory_order_acquire);
295 r = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->read_ptr, almemory_order_acquire);
296 w &= rb->size_mask;
297 r &= rb->size_mask;
298 free_cnt = (r-w-1) & rb->size_mask;
300 cnt2 = w + free_cnt;
301 if(cnt2 > rb->size)
303 /* Two part vector: the rest of the buffer after the current write ptr,
304 * plus some from the start of the buffer. */
305 vec[0].buf = (char*)&rb->buf[w*rb->elem_size];
306 vec[0].len = rb->size - w;
307 vec[1].buf = (char*)rb->buf;
308 vec[1].len = cnt2 & rb->size_mask;
310 else
312 vec[0].buf = (char*)&rb->buf[w*rb->elem_size];
313 vec[0].len = free_cnt;
314 vec[1].buf = NULL;
315 vec[1].len = 0;