diff options
author | Chris Robinson <[email protected]> | 2010-06-04 00:24:23 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2010-06-04 00:24:23 -0700 |
commit | 244daba501fd5908dc5c2de598eed889cad83c70 (patch) | |
tree | cf492d6010c89abff1364a27222b88b04ec1f779 /Alc | |
parent | 43dadcd9e437968f2913f7f066e0516eee4cbde0 (diff) |
Don't overwrite data when the ring buffer overflows
Diffstat (limited to 'Alc')
-rw-r--r-- | Alc/alcRing.c | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/Alc/alcRing.c b/Alc/alcRing.c index 5328c942..3361eb6e 100644 --- a/Alc/alcRing.c +++ b/Alc/alcRing.c @@ -46,7 +46,7 @@ RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length) ring->frame_size = frame_size; ring->length = length+1; ring->write_pos = 1; - ring->mem = malloc((length+1)*frame_size); + ring->mem = malloc(ring->length * ring->frame_size); if(!ring->mem) { free(ring); @@ -85,20 +85,26 @@ void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len) EnterCriticalSection(&ring->cs); - remain = ring->length - ring->write_pos; - if((ring->read_pos-ring->write_pos+ring->length)%ring->length < len) - ring->read_pos = (ring->write_pos+len) % ring->length; + remain = (ring->read_pos-ring->write_pos+ring->length) % ring->length; + if(remain < len) len = remain; - if(remain < len) + if(len > 0) { - memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, remain*ring->frame_size); - memcpy(ring->mem, data+(remain*ring->frame_size), (len-remain)*ring->frame_size); - } - else - memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, len*ring->frame_size); + remain = ring->length - ring->write_pos; + if(remain < len) + { + memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, + remain*ring->frame_size); + memcpy(ring->mem, data+(remain*ring->frame_size), + (len-remain)*ring->frame_size); + } + else + memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, + len*ring->frame_size); - ring->write_pos += len; - ring->write_pos %= ring->length; + ring->write_pos += len; + ring->write_pos %= ring->length; + } LeaveCriticalSection(&ring->cs); } |