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
43 RingBuffer
*CreateRingBuffer(ALsizei frame_size
, ALsizei length
)
45 RingBuffer
*ring
= calloc(1, sizeof(*ring
) + ((length
+1) * frame_size
));
48 ring
->mem
= (ALubyte
*)(ring
+1);
50 ring
->frame_size
= frame_size
;
51 ring
->length
= length
+1;
55 almtx_init(&ring
->mtx
, almtx_plain
);
60 void DestroyRingBuffer(RingBuffer
*ring
)
64 almtx_destroy(&ring
->mtx
);
69 ALsizei
RingBufferSize(RingBuffer
*ring
)
73 almtx_lock(&ring
->mtx
);
74 s
= (ring
->write_pos
-ring
->read_pos
+ring
->length
) % ring
->length
;
75 almtx_unlock(&ring
->mtx
);
80 void WriteRingBuffer(RingBuffer
*ring
, const ALubyte
*data
, ALsizei len
)
84 almtx_lock(&ring
->mtx
);
86 remain
= (ring
->read_pos
-ring
->write_pos
-1+ring
->length
) % ring
->length
;
87 if(remain
< len
) len
= remain
;
91 remain
= ring
->length
- ring
->write_pos
;
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
);
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
)
114 almtx_lock(&ring
->mtx
);
116 remain
= ring
->length
- ring
->read_pos
;
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
);
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
;
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
)
154 power_of_two
= NextPowerOf2(sz
);
155 if(power_of_two
< sz
)
158 rb
= al_malloc(16, sizeof(*rb
) + power_of_two
*elem_sz
);
161 rb
->size
= power_of_two
;
162 rb
->size_mask
= rb
->size
- 1;
163 rb
->elem_size
= elem_sz
;
170 /* Free all data associated with the ringbuffer `rb'. */
171 void ll_ringbuffer_free(ll_ringbuffer_t
*rb
)
177 munlock(rb
, sizeof(*rb
) + rb
->size
*rb
->elem_size
);
178 #endif /* USE_MLOCK */
183 /* Lock the data block of `rb' using the system call 'mlock'. */
184 int ll_ringbuffer_mlock(ll_ringbuffer_t
*rb
)
187 if(!rb
->locked
&& mlock(rb
, sizeof(*rb
) + rb
->size
*rb
->elem_size
))
189 #endif /* USE_MLOCK */
194 /* Reset the read and write pointers to zero. This is not thread safe. */
195 void ll_ringbuffer_reset(ll_ringbuffer_t
*rb
)
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
)
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
;
235 n1
= rb
->size
- rb
->read_ptr
;
236 n2
= cnt2
& rb
->size_mask
;
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
;
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
;
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
)
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
;
273 n1
= rb
->size
- tmp_read_ptr
;
274 n2
= cnt2
& rb
->size_mask
;
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
;
285 memcpy(dest
+ n1
*rb
->elem_size
, &(rb
->buf
[tmp_read_ptr
*rb
->elem_size
]), n2
*rb
->elem_size
);
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
)
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
;
305 n1
= rb
->size
- rb
->write_ptr
;
306 n2
= cnt2
& rb
->size_mask
;
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
;
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
;
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
;
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
;
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
)
349 free_cnt
= (rb
->size
+w
-r
) & rb
->size_mask
;
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
;
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
;
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
)
382 free_cnt
= (rb
->size
+r
-w
-1) & rb
->size_mask
;
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
;
396 vec
[0].buf
= (char*)&(rb
->buf
[w
*rb
->elem_size
]);
397 vec
[0].len
= free_cnt
;