pa_ringbuffer.c (9403B)
1 /* 2 * $Id$ 3 * Portable Audio I/O Library 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 * modified for SMP safety on Linux by Leland Lucius 9 * also, allowed for const where possible 10 * modified for multiple-byte-sized data elements by Sven Fischer 11 * 12 * Note that this is safe only for a single-thread reader and a 13 * single-thread writer. 14 * 15 * This program uses the PortAudio Portable Audio Library. 16 * For more information see: http://www.portaudio.com 17 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk 18 * 19 * Permission is hereby granted, free of charge, to any person obtaining 20 * a copy of this software and associated documentation files 21 * (the "Software"), to deal in the Software without restriction, 22 * including without limitation the rights to use, copy, modify, merge, 23 * publish, distribute, sublicense, and/or sell copies of the Software, 24 * and to permit persons to whom the Software is furnished to do so, 25 * subject to the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be 28 * included in all copies or substantial portions of the Software. 29 * 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 33 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 34 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 35 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 37 */ 38 39 /* 40 * The text above constitutes the entire PortAudio license; however, 41 * the PortAudio community also makes the following non-binding requests: 42 * 43 * Any person wishing to distribute modifications to the Software is 44 * requested to send the modifications to the original developer so that 45 * they can be incorporated into the canonical version. It is also 46 * requested that these non-binding requests be included along with the 47 * license above. 48 */ 49 50 /** 51 @file 52 @ingroup common_src 53 */ 54 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <math.h> 58 #include "pa_ringbuffer.h" 59 #include <string.h> 60 #include "pa_memorybarrier.h" 61 62 /*************************************************************************** 63 * Initialize FIFO. 64 * elementCount must be power of 2, returns -1 if not. 65 */ 66 ring_buffer_size_t PaUtil_InitializeRingBuffer( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementSizeBytes, ring_buffer_size_t elementCount, void *dataPtr ) 67 { 68 if( ((elementCount-1) & elementCount) != 0) return -1; /* Not Power of two. */ 69 rbuf->bufferSize = elementCount; 70 rbuf->buffer = (char *)dataPtr; 71 PaUtil_FlushRingBuffer( rbuf ); 72 rbuf->bigMask = (elementCount*2)-1; 73 rbuf->smallMask = (elementCount)-1; 74 rbuf->elementSizeBytes = elementSizeBytes; 75 return 0; 76 } 77 78 /*************************************************************************** 79 ** Return number of elements available for reading. */ 80 ring_buffer_size_t PaUtil_GetRingBufferReadAvailable( const PaUtilRingBuffer *rbuf ) 81 { 82 return ( (rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask ); 83 } 84 /*************************************************************************** 85 ** Return number of elements available for writing. */ 86 ring_buffer_size_t PaUtil_GetRingBufferWriteAvailable( const PaUtilRingBuffer *rbuf ) 87 { 88 return ( rbuf->bufferSize - PaUtil_GetRingBufferReadAvailable(rbuf)); 89 } 90 91 /*************************************************************************** 92 ** Clear buffer. Should only be called when buffer is NOT being read or written. */ 93 void PaUtil_FlushRingBuffer( PaUtilRingBuffer *rbuf ) 94 { 95 rbuf->writeIndex = rbuf->readIndex = 0; 96 } 97 98 /*************************************************************************** 99 ** Get address of region(s) to which we can write data. 100 ** If the region is contiguous, size2 will be zero. 101 ** If non-contiguous, size2 will be the size of second region. 102 ** Returns room available to be written or elementCount, whichever is smaller. 103 */ 104 ring_buffer_size_t PaUtil_GetRingBufferWriteRegions( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount, 105 void **dataPtr1, ring_buffer_size_t *sizePtr1, 106 void **dataPtr2, ring_buffer_size_t *sizePtr2 ) 107 { 108 ring_buffer_size_t index; 109 ring_buffer_size_t available = PaUtil_GetRingBufferWriteAvailable( rbuf ); 110 if( elementCount > available ) elementCount = available; 111 /* Check to see if write is not contiguous. */ 112 index = rbuf->writeIndex & rbuf->smallMask; 113 if( (index + elementCount) > rbuf->bufferSize ) 114 { 115 /* Write data in two blocks that wrap the buffer. */ 116 ring_buffer_size_t firstHalf = rbuf->bufferSize - index; 117 *dataPtr1 = &rbuf->buffer[index*rbuf->elementSizeBytes]; 118 *sizePtr1 = firstHalf; 119 *dataPtr2 = &rbuf->buffer[0]; 120 *sizePtr2 = elementCount - firstHalf; 121 } 122 else 123 { 124 *dataPtr1 = &rbuf->buffer[index*rbuf->elementSizeBytes]; 125 *sizePtr1 = elementCount; 126 *dataPtr2 = NULL; 127 *sizePtr2 = 0; 128 } 129 130 if( available ) 131 PaUtil_FullMemoryBarrier(); /* (write-after-read) => full barrier */ 132 133 return elementCount; 134 } 135 136 137 /*************************************************************************** 138 */ 139 ring_buffer_size_t PaUtil_AdvanceRingBufferWriteIndex( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount ) 140 { 141 /* ensure that previous writes are seen before we update the write index 142 (write after write) 143 */ 144 PaUtil_WriteMemoryBarrier(); 145 return rbuf->writeIndex = (rbuf->writeIndex + elementCount) & rbuf->bigMask; 146 } 147 148 /*************************************************************************** 149 ** Get address of region(s) from which we can read data. 150 ** If the region is contiguous, size2 will be zero. 151 ** If non-contiguous, size2 will be the size of second region. 152 ** Returns room available to be read or elementCount, whichever is smaller. 153 */ 154 ring_buffer_size_t PaUtil_GetRingBufferReadRegions( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount, 155 void **dataPtr1, ring_buffer_size_t *sizePtr1, 156 void **dataPtr2, ring_buffer_size_t *sizePtr2 ) 157 { 158 ring_buffer_size_t index; 159 ring_buffer_size_t available = PaUtil_GetRingBufferReadAvailable( rbuf ); /* doesn't use memory barrier */ 160 if( elementCount > available ) elementCount = available; 161 /* Check to see if read is not contiguous. */ 162 index = rbuf->readIndex & rbuf->smallMask; 163 if( (index + elementCount) > rbuf->bufferSize ) 164 { 165 /* Write data in two blocks that wrap the buffer. */ 166 ring_buffer_size_t firstHalf = rbuf->bufferSize - index; 167 *dataPtr1 = &rbuf->buffer[index*rbuf->elementSizeBytes]; 168 *sizePtr1 = firstHalf; 169 *dataPtr2 = &rbuf->buffer[0]; 170 *sizePtr2 = elementCount - firstHalf; 171 } 172 else 173 { 174 *dataPtr1 = &rbuf->buffer[index*rbuf->elementSizeBytes]; 175 *sizePtr1 = elementCount; 176 *dataPtr2 = NULL; 177 *sizePtr2 = 0; 178 } 179 180 if( available ) 181 PaUtil_ReadMemoryBarrier(); /* (read-after-read) => read barrier */ 182 183 return elementCount; 184 } 185 /*************************************************************************** 186 */ 187 ring_buffer_size_t PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount ) 188 { 189 /* ensure that previous reads (copies out of the ring buffer) are always completed before updating (writing) the read index. 190 (write-after-read) => full barrier 191 */ 192 PaUtil_FullMemoryBarrier(); 193 return rbuf->readIndex = (rbuf->readIndex + elementCount) & rbuf->bigMask; 194 } 195 196 /*************************************************************************** 197 ** Return elements written. */ 198 ring_buffer_size_t PaUtil_WriteRingBuffer( PaUtilRingBuffer *rbuf, const void *data, ring_buffer_size_t elementCount ) 199 { 200 ring_buffer_size_t size1, size2, numWritten; 201 void *data1, *data2; 202 numWritten = PaUtil_GetRingBufferWriteRegions( rbuf, elementCount, &data1, &size1, &data2, &size2 ); 203 if( size2 > 0 ) 204 { 205 206 memcpy( data1, data, size1*rbuf->elementSizeBytes ); 207 data = ((char *)data) + size1*rbuf->elementSizeBytes; 208 memcpy( data2, data, size2*rbuf->elementSizeBytes ); 209 } 210 else 211 { 212 memcpy( data1, data, size1*rbuf->elementSizeBytes ); 213 } 214 PaUtil_AdvanceRingBufferWriteIndex( rbuf, numWritten ); 215 return numWritten; 216 } 217 218 /*************************************************************************** 219 ** Return elements read. */ 220 ring_buffer_size_t PaUtil_ReadRingBuffer( PaUtilRingBuffer *rbuf, void *data, ring_buffer_size_t elementCount ) 221 { 222 ring_buffer_size_t size1, size2, numRead; 223 void *data1, *data2; 224 numRead = PaUtil_GetRingBufferReadRegions( rbuf, elementCount, &data1, &size1, &data2, &size2 ); 225 if( size2 > 0 ) 226 { 227 memcpy( data, data1, size1*rbuf->elementSizeBytes ); 228 data = ((char *)data) + size1*rbuf->elementSizeBytes; 229 memcpy( data, data2, size2*rbuf->elementSizeBytes ); 230 } 231 else 232 { 233 memcpy( data, data1, size1*rbuf->elementSizeBytes ); 234 } 235 PaUtil_AdvanceRingBufferReadIndex( rbuf, numRead ); 236 return numRead; 237 }