Avoid using a single-letter iterator value
[openal-soft.git] / Alc / alcRing.c
blob3361eb6e5897f6a340cef7b0a251b06f490b7ddb
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <string.h>
24 #include <stdlib.h>
26 #include "alMain.h"
29 struct RingBuffer {
30 ALubyte *mem;
32 ALsizei frame_size;
33 ALsizei length;
34 ALint read_pos;
35 ALint write_pos;
37 CRITICAL_SECTION cs;
41 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
43 RingBuffer *ring = calloc(1, sizeof(*ring));
44 if(ring)
46 ring->frame_size = frame_size;
47 ring->length = length+1;
48 ring->write_pos = 1;
49 ring->mem = malloc(ring->length * ring->frame_size);
50 if(!ring->mem)
52 free(ring);
53 ring = NULL;
56 InitializeCriticalSection(&ring->cs);
58 return ring;
61 void DestroyRingBuffer(RingBuffer *ring)
63 if(ring)
65 DeleteCriticalSection(&ring->cs);
66 free(ring->mem);
67 free(ring);
71 ALsizei RingBufferSize(RingBuffer *ring)
73 ALsizei s;
75 EnterCriticalSection(&ring->cs);
76 s = (ring->write_pos-ring->read_pos-1+ring->length) % ring->length;
77 LeaveCriticalSection(&ring->cs);
79 return s;
82 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
84 int remain;
86 EnterCriticalSection(&ring->cs);
88 remain = (ring->read_pos-ring->write_pos+ring->length) % ring->length;
89 if(remain < len) len = remain;
91 if(len > 0)
93 remain = ring->length - ring->write_pos;
94 if(remain < len)
96 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
97 remain*ring->frame_size);
98 memcpy(ring->mem, data+(remain*ring->frame_size),
99 (len-remain)*ring->frame_size);
101 else
102 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
103 len*ring->frame_size);
105 ring->write_pos += len;
106 ring->write_pos %= ring->length;
109 LeaveCriticalSection(&ring->cs);
112 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len)
114 int remain;
116 EnterCriticalSection(&ring->cs);
118 remain = ring->length - ring->read_pos;
119 if(remain < len)
121 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size);
122 memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size);
124 else
125 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size);
127 ring->read_pos += len;
128 ring->read_pos %= ring->length;
130 LeaveCriticalSection(&ring->cs);