aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/alcRing.c
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2011-06-27 14:30:45 -0700
committerChris Robinson <[email protected]>2011-06-27 14:30:45 -0700
commitea83608ee46c45e3826ff197276a659c5f95a09f (patch)
treec0934a58eeffbbea4dfe7929a82017549f4ccc98 /Alc/alcRing.c
parent8cae1aee80ae579b5950685afe859dfb6892f2c9 (diff)
Avoid a junk sample at the start of the ring buffer, and allocate the buffer
memory at the end of the ring buffer itself.
Diffstat (limited to 'Alc/alcRing.c')
-rw-r--r--Alc/alcRing.c18
1 files changed, 7 insertions, 11 deletions
diff --git a/Alc/alcRing.c b/Alc/alcRing.c
index 3361eb6e..2d9d5069 100644
--- a/Alc/alcRing.c
+++ b/Alc/alcRing.c
@@ -40,18 +40,15 @@ struct RingBuffer {
RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
{
- RingBuffer *ring = calloc(1, sizeof(*ring));
+ RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size));
if(ring)
{
+ ring->mem = (ALubyte*)(ring+1);
+
ring->frame_size = frame_size;
ring->length = length+1;
- ring->write_pos = 1;
- ring->mem = malloc(ring->length * ring->frame_size);
- if(!ring->mem)
- {
- free(ring);
- ring = NULL;
- }
+ ring->read_pos = 0;
+ ring->write_pos = 0;
InitializeCriticalSection(&ring->cs);
}
@@ -63,7 +60,6 @@ void DestroyRingBuffer(RingBuffer *ring)
if(ring)
{
DeleteCriticalSection(&ring->cs);
- free(ring->mem);
free(ring);
}
}
@@ -73,7 +69,7 @@ ALsizei RingBufferSize(RingBuffer *ring)
ALsizei s;
EnterCriticalSection(&ring->cs);
- s = (ring->write_pos-ring->read_pos-1+ring->length) % ring->length;
+ s = (ring->write_pos-ring->read_pos+ring->length) % ring->length;
LeaveCriticalSection(&ring->cs);
return s;
@@ -85,7 +81,7 @@ void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
EnterCriticalSection(&ring->cs);
- remain = (ring->read_pos-ring->write_pos+ring->length) % ring->length;
+ remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length;
if(remain < len) len = remain;
if(len > 0)