diff options
author | Chris Robinson <[email protected]> | 2018-03-02 12:46:31 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-03-02 13:01:11 -0800 |
commit | 03274a5b95146675c05b5b6a0340f45a7b122c50 (patch) | |
tree | 02c6bec8667f888742c79a5538995e82797e2fd2 /Alc/ringbuffer.c | |
parent | 6f62fed65c4dcdf999a612e258cca59a22355f3c (diff) |
Ensure at least the specified ringbuffer size is writable
Previously, all but one of the specified size could be written (so for a size
of n, only n-1 was guaranteed writable). All users pretty much compensated for
this, but it makes more sense to fix it at the source.
Diffstat (limited to 'Alc/ringbuffer.c')
-rw-r--r-- | Alc/ringbuffer.c | 34 |
1 files changed, 8 insertions, 26 deletions
diff --git a/Alc/ringbuffer.c b/Alc/ringbuffer.c index 02fa857c..6c419cf8 100644 --- a/Alc/ringbuffer.c +++ b/Alc/ringbuffer.c @@ -46,8 +46,6 @@ struct ll_ringbuffer { alignas(16) char buf[]; }; -/* Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes. - * The number of elements is rounded up to the next power of two. */ ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz, int limit_writes) { ll_ringbuffer_t *rb; @@ -55,7 +53,7 @@ ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz, int limit_write if(sz > 0) { - power_of_two = sz - 1; + power_of_two = sz; power_of_two |= power_of_two>>1; power_of_two |= power_of_two>>2; power_of_two |= power_of_two>>4; @@ -79,13 +77,11 @@ ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz, int limit_write return rb; } -/* Free all data associated with the ringbuffer `rb'. */ void ll_ringbuffer_free(ll_ringbuffer_t *rb) { al_free(rb); } -/* Reset the read and write pointers to zero. This is not thread safe. */ void ll_ringbuffer_reset(ll_ringbuffer_t *rb) { ATOMIC_STORE(&rb->write_ptr, 0, almemory_order_release); @@ -93,16 +89,14 @@ void ll_ringbuffer_reset(ll_ringbuffer_t *rb) memset(rb->buf, 0, (rb->size_mask+1)*rb->elem_size); } -/* Return the number of elements available for reading. This is the number of - * elements in front of the read pointer and behind the write pointer. */ + size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb) { size_t w = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->write_ptr, almemory_order_acquire); size_t r = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->read_ptr, almemory_order_acquire); return (w-r) & rb->size_mask; } -/* Return the number of elements available for writing. This is the number of - * elements in front of the write pointer and behind the read pointer. */ + size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb) { size_t w = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->write_ptr, almemory_order_acquire); @@ -111,8 +105,7 @@ size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb) return (w > rb->size) ? rb->size : w; } -/* The copying data reader. Copy at most `cnt' elements from `rb' to `dest'. - * Returns the actual number of elements copied. */ + size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt) { size_t read_ptr; @@ -151,9 +144,6 @@ size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt) return to_read; } -/* The copying data reader w/o read pointer advance. Copy at most `cnt' - * elements from `rb' to `dest'. Returns the actual number of elements copied. - */ size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt) { size_t free_cnt; @@ -190,8 +180,6 @@ size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt) return to_read; } -/* The copying data writer. Copy at most `cnt' elements to `rb' from `src'. - * Returns the actual number of elements copied. */ size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt) { size_t write_ptr; @@ -230,22 +218,19 @@ size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt) return to_write; } -/* Advance the read pointer `cnt' places. */ + void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt) { ATOMIC_ADD(&rb->read_ptr, cnt, almemory_order_acq_rel); } -/* Advance the write pointer `cnt' places. */ void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt) { ATOMIC_ADD(&rb->write_ptr, cnt, almemory_order_acq_rel); } -/* The non-copying data reader. `vec' is an array of two places. Set the values - * at `vec' to hold the current readable data at `rb'. If the readable data is - * in one segment the second segment has zero length. */ -void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t * vec) + +void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]) { size_t free_cnt; size_t cnt2; @@ -277,10 +262,7 @@ void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data } } -/* The non-copying data writer. `vec' is an array of two places. Set the values - * at `vec' to hold the current writeable data at `rb'. If the writeable data - * is in one segment the second segment has zero length. */ -void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec) +void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]) { size_t free_cnt; size_t cnt2; |