Util: Fix wrapping edge cases in RingFIFO
Vicki Pfau vi@endrift.com
Sun, 23 Sep 2018 13:26:32 -0700
2 files changed,
7 insertions(+),
6 deletions(-)
M
CHANGES
→
CHANGES
@@ -62,6 +62,7 @@ - GB Audio: Fix channel 3 reset value
- GB Audio: Fix channel 4 initial LFSR - GB, GBA Video: Don't call finishFrame twice in thread proxy - GB Audio: Fix channel 1, 2 and 4 reset timing + - Util: Fix wrapping edge cases in RingFIFO Misc: - GBA Timer: Use global cycles for timers - GBA: Extend oddly-sized ROMs to full address space (fixes mgba.io/i/722)
M
src/util/ring-fifo.c
→
src/util/ring-fifo.c
@@ -33,8 +33,8 @@ void* end;
ATOMIC_LOAD(end, buffer->readPtr); // Wrap around if we can't fit enough in here - if ((intptr_t) data - (intptr_t) buffer->data + length >= buffer->capacity) { - if (end == buffer->data) { + if ((uintptr_t) data - (uintptr_t) buffer->data + length >= buffer->capacity) { + if (end == buffer->data || end > data) { // Oops! If we wrap now, it'll appear empty return 0; }@@ -65,8 +65,8 @@ void* end;
ATOMIC_LOAD(end, buffer->writePtr); // Wrap around if we can't fit enough in here - if ((intptr_t) data - (intptr_t) buffer->data + length >= buffer->capacity) { - if (end == data) { + if ((uintptr_t) data - (uintptr_t) buffer->data + length >= buffer->capacity) { + if (end >= data) { // Oops! If we wrap now, it'll appear full return 0; }@@ -78,7 +78,7 @@ if (data > end) {
uintptr_t bufferEnd = (uintptr_t) buffer->data + buffer->capacity; remaining = bufferEnd - (uintptr_t) data; } else { - remaining = (intptr_t) end - (intptr_t) data; + remaining = (uintptr_t) end - (uintptr_t) data; } // If the pointers touch, it's empty if (remaining < length) {@@ -87,6 +87,6 @@ }
if (output) { memcpy(output, data, length); } - ATOMIC_STORE(buffer->readPtr, (void*) ((intptr_t) data + length)); + ATOMIC_STORE(buffer->readPtr, (void*) ((uintptr_t) data + length)); return length; }