Remove the CONST_CAST hack

This commit is contained in:
Chris Robinson 2018-11-19 03:53:31 -08:00
parent e6c2c1f3b6
commit c01743fe5d
3 changed files with 33 additions and 53 deletions

View File

@ -36,8 +36,8 @@
* size or count is in 'elements', not bytes. Additionally, it only supports
* single-consumer/single-provider operation. */
struct ll_ringbuffer {
ATOMIC(size_t) write_ptr;
ATOMIC(size_t) read_ptr;
std::atomic<size_t> write_ptr;
std::atomic<size_t> read_ptr;
size_t size;
size_t size_mask;
size_t elem_size;
@ -83,23 +83,23 @@ void ll_ringbuffer_free(ll_ringbuffer_t *rb)
void ll_ringbuffer_reset(ll_ringbuffer_t *rb)
{
ATOMIC_STORE(&rb->write_ptr, static_cast<size_t>(0), almemory_order_release);
ATOMIC_STORE(&rb->read_ptr, static_cast<size_t>(0), almemory_order_release);
rb->write_ptr.store(0, std::memory_order_release);
rb->read_ptr.store(0, std::memory_order_release);
memset(rb->buf, 0, (rb->size_mask+1)*rb->elem_size);
}
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);
size_t w = rb->write_ptr.load(std::memory_order_acquire);
size_t r = rb->read_ptr.load(std::memory_order_acquire);
return (w-r) & rb->size_mask;
}
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);
size_t r = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->read_ptr, almemory_order_acquire);
size_t w = rb->write_ptr.load(std::memory_order_acquire);
size_t r = rb->read_ptr.load(std::memory_order_acquire);
w = (r-w-1) & rb->size_mask;
return (w > rb->size) ? rb->size : w;
}
@ -117,7 +117,7 @@ size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt)
if(free_cnt == 0) return 0;
to_read = (cnt > free_cnt) ? free_cnt : cnt;
read_ptr = ATOMIC_LOAD(&rb->read_ptr, almemory_order_relaxed) & rb->size_mask;
read_ptr = rb->read_ptr.load(std::memory_order_relaxed) & rb->size_mask;
cnt2 = read_ptr + to_read;
if(cnt2 > rb->size_mask+1)
@ -139,7 +139,7 @@ size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt)
n2*rb->elem_size);
read_ptr += n2;
}
ATOMIC_STORE(&rb->read_ptr, read_ptr, almemory_order_release);
rb->read_ptr.store(read_ptr, std::memory_order_release);
return to_read;
}
@ -155,7 +155,7 @@ size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt)
if(free_cnt == 0) return 0;
to_read = (cnt > free_cnt) ? free_cnt : cnt;
read_ptr = ATOMIC_LOAD(&rb->read_ptr, almemory_order_relaxed) & rb->size_mask;
read_ptr = rb->read_ptr.load(std::memory_order_relaxed) & rb->size_mask;
cnt2 = read_ptr + to_read;
if(cnt2 > rb->size_mask+1)
@ -191,7 +191,7 @@ size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt)
if(free_cnt == 0) return 0;
to_write = (cnt > free_cnt) ? free_cnt : cnt;
write_ptr = ATOMIC_LOAD(&rb->write_ptr, almemory_order_relaxed) & rb->size_mask;
write_ptr = rb->write_ptr.load(std::memory_order_relaxed) & rb->size_mask;
cnt2 = write_ptr + to_write;
if(cnt2 > rb->size_mask+1)
@ -213,19 +213,19 @@ size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt)
n2*rb->elem_size);
write_ptr += n2;
}
ATOMIC_STORE(&rb->write_ptr, write_ptr, almemory_order_release);
rb->write_ptr.store(write_ptr, std::memory_order_release);
return to_write;
}
void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt)
{
ATOMIC_ADD(&rb->read_ptr, cnt, almemory_order_acq_rel);
rb->read_ptr.fetch_add(cnt, std::memory_order_acq_rel);
}
void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt)
{
ATOMIC_ADD(&rb->write_ptr, cnt, almemory_order_acq_rel);
rb->write_ptr.fetch_add(cnt, std::memory_order_acq_rel);
}
@ -233,10 +233,9 @@ void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data
{
size_t free_cnt;
size_t cnt2;
size_t w, r;
w = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->write_ptr, almemory_order_acquire);
r = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->read_ptr, almemory_order_acquire);
size_t w = rb->write_ptr.load(std::memory_order_acquire);
size_t r = rb->read_ptr.load(std::memory_order_acquire);
w &= rb->size_mask;
r &= rb->size_mask;
free_cnt = (w-r) & rb->size_mask;
@ -246,17 +245,17 @@ void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data
{
/* Two part vector: the rest of the buffer after the current write ptr,
* plus some from the start of the buffer. */
vec[0].buf = (char*)&rb->buf[r*rb->elem_size];
vec[0].buf = const_cast<char*>(&rb->buf[r*rb->elem_size]);
vec[0].len = rb->size_mask+1 - r;
vec[1].buf = (char*)rb->buf;
vec[1].buf = const_cast<char*>(rb->buf);
vec[1].len = cnt2 & rb->size_mask;
}
else
{
/* Single part vector: just the rest of the buffer */
vec[0].buf = (char*)&rb->buf[r*rb->elem_size];
vec[0].buf = const_cast<char*>(&rb->buf[r*rb->elem_size]);
vec[0].len = free_cnt;
vec[1].buf = NULL;
vec[1].buf = nullptr;
vec[1].len = 0;
}
}
@ -265,10 +264,9 @@ void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_dat
{
size_t free_cnt;
size_t cnt2;
size_t w, r;
w = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->write_ptr, almemory_order_acquire);
r = ATOMIC_LOAD(&CONST_CAST(ll_ringbuffer_t*,rb)->read_ptr, almemory_order_acquire);
size_t w = rb->write_ptr.load(std::memory_order_acquire);
size_t r = rb->read_ptr.load(std::memory_order_acquire);
w &= rb->size_mask;
r &= rb->size_mask;
free_cnt = (r-w-1) & rb->size_mask;
@ -279,16 +277,16 @@ void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_dat
{
/* Two part vector: the rest of the buffer after the current write ptr,
* plus some from the start of the buffer. */
vec[0].buf = (char*)&rb->buf[w*rb->elem_size];
vec[0].buf = const_cast<char*>(&rb->buf[w*rb->elem_size]);
vec[0].len = rb->size_mask+1 - w;
vec[1].buf = (char*)rb->buf;
vec[1].buf = const_cast<char*>(rb->buf);
vec[1].len = cnt2 & rb->size_mask;
}
else
{
vec[0].buf = (char*)&rb->buf[w*rb->elem_size];
vec[0].buf = const_cast<char*>(&rb->buf[w*rb->elem_size]);
vec[0].len = free_cnt;
vec[1].buf = NULL;
vec[1].buf = nullptr;
vec[1].len = 0;
}
}

View File

@ -1438,8 +1438,7 @@ static ALboolean GetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp p
while(BufferList && BufferList != Current)
{
played += BufferList->num_buffers;
BufferList = ATOMIC_LOAD(&CONST_CAST(ALbufferlistitem*,BufferList)->next,
almemory_order_relaxed);
BufferList = BufferList->next.load(std::memory_order_relaxed);
}
*values = played;
}
@ -3294,8 +3293,7 @@ static ALint64 GetSourceSampleOffset(ALsource *Source, ALCcontext *context, ALui
while(BufferList && BufferList != Current)
{
readPos += (ALuint64)BufferList->max_samples << 32;
BufferList = ATOMIC_LOAD(&CONST_CAST(ALbufferlistitem*,BufferList)->next,
almemory_order_relaxed);
BufferList = BufferList->next.load(std::memory_order_relaxed);
}
readPos = minu64(readPos, U64(0x7fffffffffffffff));
}
@ -3347,8 +3345,7 @@ static ALdouble GetSourceSecOffset(ALsource *Source, ALCcontext *context, ALuint
while(!BufferFmt && i < BufferList->num_buffers)
BufferFmt = BufferList->buffers[i++];
readPos += (ALuint64)BufferList->max_samples << FRACTIONBITS;
BufferList = ATOMIC_LOAD(&CONST_CAST(ALbufferlistitem*,BufferList)->next,
almemory_order_relaxed);
BufferList = BufferList->next.load(std::memory_order_relaxed);
}
while(BufferList && !BufferFmt)
@ -3356,8 +3353,7 @@ static ALdouble GetSourceSecOffset(ALsource *Source, ALCcontext *context, ALuint
ALsizei i = 0;
while(!BufferFmt && i < BufferList->num_buffers)
BufferFmt = BufferList->buffers[i++];
BufferList = ATOMIC_LOAD(&CONST_CAST(ALbufferlistitem*,BufferList)->next,
almemory_order_relaxed);
BufferList = BufferList->next.load(std::memory_order_relaxed);
}
assert(BufferFmt != NULL);
@ -3418,8 +3414,7 @@ static ALdouble GetSourceOffset(ALsource *Source, ALenum name, ALCcontext *conte
totalBufferLen += BufferList->max_samples;
if(!readFin) readPos += BufferList->max_samples;
BufferList = ATOMIC_LOAD(&CONST_CAST(ALbufferlistitem*,BufferList)->next,
almemory_order_relaxed);
BufferList = BufferList->next.load(std::memory_order_relaxed);
}
assert(BufferFmt != NULL);
@ -3534,8 +3529,7 @@ static ALboolean GetSampleOffset(ALsource *Source, ALuint *offset, ALsizei *frac
for(i = 0;i < BufferList->num_buffers && !BufferFmt;i++)
BufferFmt = BufferList->buffers[i];
if(BufferFmt) break;
BufferList = ATOMIC_LOAD(&CONST_CAST(ALbufferlistitem*,BufferList)->next,
almemory_order_relaxed);
BufferList = BufferList->next.load(std::memory_order_relaxed);
}
if(!BufferFmt)
{

View File

@ -3,18 +3,6 @@
#include <atomic>
#ifdef __GNUC__
/* This helps cast away the const-ness of a pointer without accidentally
* changing the pointer type. This is necessary due to Clang's inability to use
* atomic_load on a const _Atomic variable.
*/
#define CONST_CAST(T, V) __extension__({ \
const T _tmp = (V); \
(T)_tmp; \
})
#else
#define CONST_CAST(T, V) ((T)(V))
#endif
#define almemory_order std::memory_order
#define almemory_order_relaxed std::memory_order_relaxed