Update a couple comments
[openal-soft.git] / Alc / alcRing.c
blobe9a40a12014cdf873ae3d4ae22303f0e73f4c1ef
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 "compat.h"
31 struct RingBuffer {
32 ALubyte *mem;
34 ALsizei frame_size;
35 ALsizei length;
36 ALint read_pos;
37 ALint write_pos;
39 almtx_t mtx;
43 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
45 RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size));
46 if(ring)
48 ring->mem = (ALubyte*)(ring+1);
50 ring->frame_size = frame_size;
51 ring->length = length+1;
52 ring->read_pos = 0;
53 ring->write_pos = 0;
55 almtx_init(&ring->mtx, almtx_plain);
57 return ring;
60 void DestroyRingBuffer(RingBuffer *ring)
62 if(ring)
64 almtx_destroy(&ring->mtx);
65 free(ring);
69 ALsizei RingBufferSize(RingBuffer *ring)
71 ALsizei s;
73 almtx_lock(&ring->mtx);
74 s = (ring->write_pos-ring->read_pos+ring->length) % ring->length;
75 almtx_unlock(&ring->mtx);
77 return s;
80 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
82 int remain;
84 almtx_lock(&ring->mtx);
86 remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length;
87 if(remain < len) len = remain;
89 if(len > 0)
91 remain = ring->length - ring->write_pos;
92 if(remain < len)
94 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
95 remain*ring->frame_size);
96 memcpy(ring->mem, data+(remain*ring->frame_size),
97 (len-remain)*ring->frame_size);
99 else
100 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
101 len*ring->frame_size);
103 ring->write_pos += len;
104 ring->write_pos %= ring->length;
107 almtx_unlock(&ring->mtx);
110 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len)
112 int remain;
114 almtx_lock(&ring->mtx);
116 remain = ring->length - ring->read_pos;
117 if(remain < len)
119 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size);
120 memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size);
122 else
123 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size);
125 ring->read_pos += len;
126 ring->read_pos %= ring->length;
128 almtx_unlock(&ring->mtx);
132 /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
133 * to include an element size. Consequently, parameters and return values for a
134 * size or count is in 'elements', not bytes. Additionally, it only supports
135 * single-consumer/single-provider operation. */
136 struct ll_ringbuffer {
137 volatile size_t write_ptr;
138 volatile size_t read_ptr;
139 size_t size;
140 size_t size_mask;
141 size_t elem_size;
142 int mlocked;
144 alignas(16) char buf[];
147 /* Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes.
148 * The number of elements is rounded up to the next power of two. */
149 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz)
151 ll_ringbuffer_t *rb;
152 ALuint power_of_two;
154 power_of_two = NextPowerOf2(sz);
155 if(power_of_two < sz)
156 return NULL;
158 rb = al_malloc(16, sizeof(*rb) + power_of_two*elem_sz);
159 if(!rb) return NULL;
161 rb->size = power_of_two;
162 rb->size_mask = rb->size - 1;
163 rb->elem_size = elem_sz;
164 rb->write_ptr = 0;
165 rb->read_ptr = 0;
166 rb->mlocked = 0;
167 return rb;
170 /* Free all data associated with the ringbuffer `rb'. */
171 void ll_ringbuffer_free(ll_ringbuffer_t *rb)
173 if(rb)
175 #ifdef USE_MLOCK
176 if(rb->mlocked)
177 munlock(rb, sizeof(*rb) + rb->size*rb->elem_size);
178 #endif /* USE_MLOCK */
179 al_free(rb);
183 /* Lock the data block of `rb' using the system call 'mlock'. */
184 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb)
186 #ifdef USE_MLOCK
187 if(!rb->locked && mlock(rb, sizeof(*rb) + rb->size*rb->elem_size))
188 return -1;
189 #endif /* USE_MLOCK */
190 rb->mlocked = 1;
191 return 0;
194 /* Reset the read and write pointers to zero. This is not thread safe. */
195 void ll_ringbuffer_reset(ll_ringbuffer_t *rb)
197 rb->read_ptr = 0;
198 rb->write_ptr = 0;
199 memset(rb->buf, 0, rb->size*rb->elem_size);
202 /* Return the number of elements available for reading. This is the number of
203 * elements in front of the read pointer and behind the write pointer. */
204 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb)
206 size_t w = rb->write_ptr;
207 size_t r = rb->read_ptr;
208 return (rb->size+w-r) & rb->size_mask;
210 /* Return the number of elements available for writing. This is the number of
211 * elements in front of the write pointer and behind the read pointer. */
212 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb)
214 size_t w = rb->write_ptr;
215 size_t r = rb->read_ptr;
216 return (rb->size+r-w-1) & rb->size_mask;
219 /* The copying data reader. Copy at most `cnt' elements from `rb' to `dest'.
220 * Returns the actual number of elements copied. */
221 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt)
223 size_t free_cnt;
224 size_t cnt2;
225 size_t to_read;
226 size_t n1, n2;
228 free_cnt = ll_ringbuffer_read_space(rb);
229 if(free_cnt == 0) return 0;
231 to_read = (cnt > free_cnt) ? free_cnt : cnt;
232 cnt2 = rb->read_ptr + to_read;
233 if(cnt2 > rb->size)
235 n1 = rb->size - rb->read_ptr;
236 n2 = cnt2 & rb->size_mask;
238 else
240 n1 = to_read;
241 n2 = 0;
244 memcpy(dest, &(rb->buf[rb->read_ptr*rb->elem_size]), n1*rb->elem_size);
245 rb->read_ptr = (rb->read_ptr + n1) & rb->size_mask;
246 if(n2)
248 memcpy(dest + n1*rb->elem_size, &(rb->buf[rb->read_ptr*rb->elem_size]), n2*rb->elem_size);
249 rb->read_ptr = (rb->read_ptr + n2) & rb->size_mask;
251 return to_read;
254 /* The copying data reader w/o read pointer advance. Copy at most `cnt'
255 * elements from `rb' to `dest'. Returns the actual number of elements copied.
257 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt)
259 size_t free_cnt;
260 size_t cnt2;
261 size_t to_read;
262 size_t n1, n2;
263 size_t tmp_read_ptr;
265 tmp_read_ptr = rb->read_ptr;
266 free_cnt = ll_ringbuffer_read_space(rb);
267 if(free_cnt == 0) return 0;
269 to_read = (cnt > free_cnt) ? free_cnt : cnt;
270 cnt2 = tmp_read_ptr + to_read;
271 if(cnt2 > rb->size)
273 n1 = rb->size - tmp_read_ptr;
274 n2 = cnt2 & rb->size_mask;
276 else
278 n1 = to_read;
279 n2 = 0;
282 memcpy(dest, &(rb->buf[tmp_read_ptr*rb->elem_size]), n1*rb->elem_size);
283 tmp_read_ptr = (tmp_read_ptr + n1) & rb->size_mask;
284 if(n2)
285 memcpy(dest + n1*rb->elem_size, &(rb->buf[tmp_read_ptr*rb->elem_size]), n2*rb->elem_size);
286 return to_read;
289 /* The copying data writer. Copy at most `cnt' elements to `rb' from `src'.
290 * Returns the actual number of elements copied. */
291 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt)
293 size_t free_cnt;
294 size_t cnt2;
295 size_t to_write;
296 size_t n1, n2;
298 free_cnt = ll_ringbuffer_write_space(rb);
299 if(free_cnt == 0) return 0;
301 to_write = (cnt > free_cnt) ? free_cnt : cnt;
302 cnt2 = rb->write_ptr + to_write;
303 if(cnt2 > rb->size)
305 n1 = rb->size - rb->write_ptr;
306 n2 = cnt2 & rb->size_mask;
308 else
310 n1 = to_write;
311 n2 = 0;
314 memcpy(&(rb->buf[rb->write_ptr*rb->elem_size]), src, n1*rb->elem_size);
315 rb->write_ptr = (rb->write_ptr + n1) & rb->size_mask;
316 if(n2)
318 memcpy(&(rb->buf[rb->write_ptr*rb->elem_size]), src + n1*rb->elem_size, n2*rb->elem_size);
319 rb->write_ptr = (rb->write_ptr + n2) & rb->size_mask;
321 return to_write;
324 /* Advance the read pointer `cnt' places. */
325 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt)
327 size_t tmp = (rb->read_ptr + cnt) & rb->size_mask;
328 rb->read_ptr = tmp;
331 /* Advance the write pointer `cnt' places. */
332 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt)
334 size_t tmp = (rb->write_ptr + cnt) & rb->size_mask;
335 rb->write_ptr = tmp;
338 /* The non-copying data reader. `vec' is an array of two places. Set the values
339 * at `vec' to hold the current readable data at `rb'. If the readable data is
340 * in one segment the second segment has zero length. */
341 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t * vec)
343 size_t free_cnt;
344 size_t cnt2;
345 size_t w, r;
347 w = rb->write_ptr;
348 r = rb->read_ptr;
349 free_cnt = (rb->size+w-r) & rb->size_mask;
351 cnt2 = r + free_cnt;
352 if(cnt2 > rb->size)
354 /* Two part vector: the rest of the buffer after the current write ptr,
355 * plus some from the start of the buffer. */
356 vec[0].buf = (char*)&(rb->buf[r*rb->elem_size]);
357 vec[0].len = rb->size - r;
358 vec[1].buf = (char*)rb->buf;
359 vec[1].len = cnt2 & rb->size_mask;
361 else
363 /* Single part vector: just the rest of the buffer */
364 vec[0].buf = (char*)&(rb->buf[r*rb->elem_size]);
365 vec[0].len = free_cnt;
366 vec[1].buf = NULL;
367 vec[1].len = 0;
371 /* The non-copying data writer. `vec' is an array of two places. Set the values
372 * at `vec' to hold the current writeable data at `rb'. If the writeable data
373 * is in one segment the second segment has zero length. */
374 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec)
376 size_t free_cnt;
377 size_t cnt2;
378 size_t w, r;
380 w = rb->write_ptr;
381 r = rb->read_ptr;
382 free_cnt = (rb->size+r-w-1) & rb->size_mask;
384 cnt2 = w + free_cnt;
385 if(cnt2 > rb->size)
387 /* Two part vector: the rest of the buffer after the current write ptr,
388 * plus some from the start of the buffer. */
389 vec[0].buf = (char*)&(rb->buf[w*rb->elem_size]);
390 vec[0].len = rb->size - w;
391 vec[1].buf = (char*)rb->buf;
392 vec[1].len = cnt2 & rb->size_mask;
394 else
396 vec[0].buf = (char*)&(rb->buf[w*rb->elem_size]);
397 vec[0].len = free_cnt;
398 vec[1].buf = NULL;
399 vec[1].len = 0;