Util: Fix wrapping edge cases in RingFIFO

This commit is contained in:
Vicki Pfau 2018-09-23 13:26:32 -07:00
parent ca9448cd35
commit de9bff4a29
2 changed files with 7 additions and 6 deletions

View File

@ -62,6 +62,7 @@ Bugfixes:
- 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)

View File

@ -33,8 +33,8 @@ size_t RingFIFOWrite(struct RingFIFO* buffer, const void* value, size_t length)
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 @@ size_t RingFIFORead(struct RingFIFO* buffer, void* output, size_t length) {
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 @@ size_t RingFIFORead(struct RingFIFO* buffer, void* output, size_t length) {
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 @@ size_t RingFIFORead(struct RingFIFO* buffer, void* output, size_t length) {
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;
}