| 1 | /* |
| 2 | * $Id: ringbuffer.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $ |
| 3 | * ringbuffer.c |
| 4 | * Ring Buffer utility.. |
| 5 | * |
| 6 | * Author: Phil Burk, http://www.softsynth.com |
| 7 | * modified for SMP safety on Mac OS X by Bjorn Roche |
| 8 | * also, alowed for const where possible |
| 9 | * Note that this is safe only for a single-thread reader and a |
| 10 | * single-thread writer. |
| 11 | * |
| 12 | * This program uses the PortAudio Portable Audio Library. |
| 13 | * For more information see: http://www.audiomulch.com/portaudio/ |
| 14 | * Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 15 | * |
| 16 | * Permission is hereby granted, free of charge, to any person obtaining |
| 17 | * a copy of this software and associated documentation files |
| 18 | * (the "Software"), to deal in the Software without restriction, |
| 19 | * including without limitation the rights to use, copy, modify, merge, |
| 20 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 21 | * and to permit persons to whom the Software is furnished to do so, |
| 22 | * subject to the following conditions: |
| 23 | * |
| 24 | * The above copyright notice and this permission notice shall be |
| 25 | * included in all copies or substantial portions of the Software. |
| 26 | * |
| 27 | * Any person wishing to distribute modifications to the Software is |
| 28 | * requested to send the modifications to the original developer so that |
| 29 | * they can be incorporated into the canonical version. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 34 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 35 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 36 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | * |
| 39 | */ |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <math.h> |
| 43 | #include "ringbuffer.h" |
| 44 | #include <string.h> |
| 45 | |
| 46 | /* |
| 47 | * We can undefine this, to turn off memory barriers, but that |
| 48 | * is only useful if we know we don't need to be MP safe or |
| 49 | * we are interested in doing some kind of tests. |
| 50 | */ |
| 51 | #define MPSAFE |
| 52 | |
| 53 | /**************** |
| 54 | * First, we'll define some memory barrier primitives based on the system. |
| 55 | * right now only OS X and FreeBSD are supported. In addition to providing |
| 56 | * memory barriers, these functions should ensure that data cached in registers |
| 57 | * is written out to cache where it can be snooped by other CPUs. (ie, the volatile |
| 58 | * keyword should not be required) |
| 59 | * |
| 60 | * the primitives that must be defined are: |
| 61 | * |
| 62 | * FullMemoryBarrier() |
| 63 | * ReadMemoryBarrier() |
| 64 | * WriteMemoryBarrier() |
| 65 | * |
| 66 | ****************/ |
| 67 | |
| 68 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 69 | //# include <libkern/OSAtomic.h> |
| 70 | /* Here are the memory barrier functions. Mac OS X and FreeBSD only provide |
| 71 | full memory barriers, so the three types of barriers are the same. |
| 72 | The asm volatile may be redundant with the memory barrier, but |
| 73 | until I have proof of that, I'm leaving it. */ |
| 74 | /* Mihai: trying to compile without assembly, since gcc barfs on asm statements for some reason |
| 75 | # define FullMemoryBarrier() do{ asm volatile("":::"memory"); OSMemoryBarrier(); }while(false) |
| 76 | # define ReadMemoryBarrier() do{ asm volatile("":::"memory"); OSMemoryBarrier(); }while(false) |
| 77 | # define WriteMemoryBarrier() do{ asm volatile("":::"memory"); OSMemoryBarrier(); }while(false) |
| 78 | |
| 79 | # define FullMemoryBarrier() do{ OSMemoryBarrier(); }while(false) |
| 80 | # define ReadMemoryBarrier() do{ OSMemoryBarrier(); }while(false) |
| 81 | # define WriteMemoryBarrier() do{ OSMemoryBarrier(); }while(false) |
| 82 | */ |
| 83 | |
| 84 | /* |
| 85 | * Mihai: OSMemoryBarrier is not available on 10.3.9 and I am not able to find a replacement |
| 86 | * As far as I can tell, not having synchronized memory acces will hurt only MP machines, and |
| 87 | * more specifically only weakly ordered memory models architectures (i.e. PowerPC). In case |
| 88 | * this code runs on a MP PPC machine, worst I can see happening is a corruption of the sound |
| 89 | * buffer, which will lead to crappy sound, but not much more. |
| 90 | * If anybody finds a 10.3.9 replacement for OSMemoryBarrier, I'd be glad to hear about it |
| 91 | */ |
| 92 | #define FullMemoryBarrier() |
| 93 | #define ReadMemoryBarrier() |
| 94 | #define WriteMemoryBarrier() |
| 95 | |
| 96 | #else |
| 97 | # error Memory Barriers not defined on this system or system unknown |
| 98 | #endif |
| 99 | |
| 100 | /*************************************************************************** |
| 101 | * Initialize FIFO. |
| 102 | * numBytes must be power of 2, returns -1 if not. |
| 103 | */ |
| 104 | long RingBuffer_Init( RingBuffer *rbuf, long numBytes, void *dataPtr ) |
| 105 | { |
| 106 | if( ((numBytes-1) & numBytes) != 0) return -1; /* Not Power of two. */ |
| 107 | rbuf->bufferSize = numBytes; |
| 108 | rbuf->buffer = (char *)dataPtr; |
| 109 | RingBuffer_Flush( rbuf ); |
| 110 | rbuf->bigMask = (numBytes*2)-1; |
| 111 | rbuf->smallMask = (numBytes)-1; |
| 112 | return 0; |
| 113 | } |
| 114 | /*************************************************************************** |
| 115 | ** Return number of bytes available for reading. */ |
| 116 | long RingBuffer_GetReadAvailable( RingBuffer *rbuf ) |
| 117 | { |
| 118 | #ifdef MPSAFE |
| 119 | ReadMemoryBarrier(); |
| 120 | #endif |
| 121 | return ( (rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask ); |
| 122 | } |
| 123 | /*************************************************************************** |
| 124 | ** Return number of bytes available for writing. */ |
| 125 | long RingBuffer_GetWriteAvailable( RingBuffer *rbuf ) |
| 126 | { |
| 127 | /* Since we are calling RingBuffer_GetReadAvailable, we don't need an aditional MB */ |
| 128 | return ( rbuf->bufferSize - RingBuffer_GetReadAvailable(rbuf)); |
| 129 | } |
| 130 | |
| 131 | /*************************************************************************** |
| 132 | ** Clear buffer. Should only be called when buffer is NOT being read. */ |
| 133 | void RingBuffer_Flush( RingBuffer *rbuf ) |
| 134 | { |
| 135 | rbuf->writeIndex = rbuf->readIndex = 0; |
| 136 | } |
| 137 | |
| 138 | /*************************************************************************** |
| 139 | ** Get address of region(s) to which we can write data. |
| 140 | ** If the region is contiguous, size2 will be zero. |
| 141 | ** If non-contiguous, size2 will be the size of second region. |
| 142 | ** Returns room available to be written or numBytes, whichever is smaller. |
| 143 | */ |
| 144 | long RingBuffer_GetWriteRegions( RingBuffer *rbuf, long numBytes, |
| 145 | void **dataPtr1, long *sizePtr1, |
| 146 | void **dataPtr2, long *sizePtr2 ) |
| 147 | { |
| 148 | long index; |
| 149 | long available = RingBuffer_GetWriteAvailable( rbuf ); |
| 150 | if( numBytes > available ) numBytes = available; |
| 151 | /* Check to see if write is not contiguous. */ |
| 152 | index = rbuf->writeIndex & rbuf->smallMask; |
| 153 | if( (index + numBytes) > rbuf->bufferSize ) |
| 154 | { |
| 155 | /* Write data in two blocks that wrap the buffer. */ |
| 156 | long firstHalf = rbuf->bufferSize - index; |
| 157 | *dataPtr1 = &rbuf->buffer[index]; |
| 158 | *sizePtr1 = firstHalf; |
| 159 | *dataPtr2 = &rbuf->buffer[0]; |
| 160 | *sizePtr2 = numBytes - firstHalf; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | *dataPtr1 = &rbuf->buffer[index]; |
| 165 | *sizePtr1 = numBytes; |
| 166 | *dataPtr2 = NULL; |
| 167 | *sizePtr2 = 0; |
| 168 | } |
| 169 | return numBytes; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /*************************************************************************** |
| 174 | */ |
| 175 | long RingBuffer_AdvanceWriteIndex( RingBuffer *rbuf, long numBytes ) |
| 176 | { |
| 177 | #ifdef MPSAFE |
| 178 | /* we need to ensure that previous writes are seen before we update the write index */ |
| 179 | WriteMemoryBarrier(); |
| 180 | return rbuf->writeIndex = (rbuf->writeIndex + numBytes) & rbuf->bigMask; |
| 181 | #else |
| 182 | return rbuf->writeIndex = (rbuf->writeIndex + numBytes) & rbuf->bigMask; |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | /*************************************************************************** |
| 187 | ** Get address of region(s) from which we can read data. |
| 188 | ** If the region is contiguous, size2 will be zero. |
| 189 | ** If non-contiguous, size2 will be the size of second region. |
| 190 | ** Returns room available to be written or numBytes, whichever is smaller. |
| 191 | */ |
| 192 | long RingBuffer_GetReadRegions( RingBuffer *rbuf, long numBytes, |
| 193 | void **dataPtr1, long *sizePtr1, |
| 194 | void **dataPtr2, long *sizePtr2 ) |
| 195 | { |
| 196 | long index; |
| 197 | long available = RingBuffer_GetReadAvailable( rbuf ); |
| 198 | if( numBytes > available ) numBytes = available; |
| 199 | /* Check to see if read is not contiguous. */ |
| 200 | index = rbuf->readIndex & rbuf->smallMask; |
| 201 | if( (index + numBytes) > rbuf->bufferSize ) |
| 202 | { |
| 203 | /* Write data in two blocks that wrap the buffer. */ |
| 204 | long firstHalf = rbuf->bufferSize - index; |
| 205 | *dataPtr1 = &rbuf->buffer[index]; |
| 206 | *sizePtr1 = firstHalf; |
| 207 | *dataPtr2 = &rbuf->buffer[0]; |
| 208 | *sizePtr2 = numBytes - firstHalf; |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | *dataPtr1 = &rbuf->buffer[index]; |
| 213 | *sizePtr1 = numBytes; |
| 214 | *dataPtr2 = NULL; |
| 215 | *sizePtr2 = 0; |
| 216 | } |
| 217 | return numBytes; |
| 218 | } |
| 219 | /*************************************************************************** |
| 220 | */ |
| 221 | long RingBuffer_AdvanceReadIndex( RingBuffer *rbuf, long numBytes ) |
| 222 | { |
| 223 | #ifdef MPSAFE |
| 224 | /* we need to ensure that previous writes are always seen before updating the index. */ |
| 225 | WriteMemoryBarrier(); |
| 226 | return rbuf->readIndex = (rbuf->readIndex + numBytes) & rbuf->bigMask; |
| 227 | #else |
| 228 | return rbuf->readIndex = (rbuf->readIndex + numBytes) & rbuf->bigMask; |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | /*************************************************************************** |
| 233 | ** Return bytes written. */ |
| 234 | long RingBuffer_Write( RingBuffer *rbuf, const void *data, long numBytes ) |
| 235 | { |
| 236 | long size1, size2, numWritten; |
| 237 | void *data1, *data2; |
| 238 | numWritten = RingBuffer_GetWriteRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 ); |
| 239 | if( size2 > 0 ) |
| 240 | { |
| 241 | |
| 242 | memcpy( data1, data, size1 ); |
| 243 | data = ((char *)data) + size1; |
| 244 | memcpy( data2, data, size2 ); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | memcpy( data1, data, size1 ); |
| 249 | } |
| 250 | RingBuffer_AdvanceWriteIndex( rbuf, numWritten ); |
| 251 | return numWritten; |
| 252 | } |
| 253 | |
| 254 | /*************************************************************************** |
| 255 | ** Return bytes read. */ |
| 256 | long RingBuffer_Read( RingBuffer *rbuf, void *data, long numBytes ) |
| 257 | { |
| 258 | long size1, size2, numRead; |
| 259 | void *data1, *data2; |
| 260 | numRead = RingBuffer_GetReadRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 ); |
| 261 | if( size2 > 0 ) |
| 262 | { |
| 263 | memcpy( data, data1, size1 ); |
| 264 | data = ((char *)data) + size1; |
| 265 | memcpy( data, data2, size2 ); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | memcpy( data, data1, size1 ); |
| 270 | } |
| 271 | RingBuffer_AdvanceReadIndex( rbuf, numRead ); |
| 272 | return numRead; |
| 273 | } |