1 #ifndef _RINGBUFFER_H 2 #define _RINGBUFFER_H 3 #ifdef __cplusplus 4 extern "C" 5 { 6 #endif /* __cplusplus */ 7 8 /* 9 * $Id: ringbuffer.h,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $ 10 * ringbuffer.h 11 * Ring Buffer utility.. 12 * 13 * Author: Phil Burk, http://www.softsynth.com 14 * modified for SMP safety on OS X by Bjorn Roche. 15 * also allowed for const where possible. 16 * Note that this is safe only for a single-thread reader 17 * and a single-thread writer. 18 * 19 * This program is distributed with the PortAudio Portable Audio Library. 20 * For more information see: http://www.audiomulch.com/portaudio/ 21 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk 22 * 23 * Permission is hereby granted, free of charge, to any person obtaining 24 * a copy of this software and associated documentation files 25 * (the "Software"), to deal in the Software without restriction, 26 * including without limitation the rights to use, copy, modify, merge, 27 * publish, distribute, sublicense, and/or sell copies of the Software, 28 * and to permit persons to whom the Software is furnished to do so, 29 * subject to the following conditions: 30 * 31 * The above copyright notice and this permission notice shall be 32 * included in all copies or substantial portions of the Software. 33 * 34 * Any person wishing to distribute modifications to the Software is 35 * requested to send the modifications to the original developer so that 36 * they can be incorporated into the canonical version. 37 * 38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 39 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 40 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 41 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 42 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 43 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 44 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 45 * 46 */ 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <math.h> 50 #include "ringbuffer.h" 51 #include <string.h> 52 53 typedef struct 54 { 55 long bufferSize; /* Number of bytes in FIFO. Power of 2. Set by RingBuffer_Init. */ 56 long writeIndex; /* Index of next writable byte. Set by RingBuffer_AdvanceWriteIndex. */ 57 long readIndex; /* Index of next readable byte. Set by RingBuffer_AdvanceReadIndex. */ 58 long bigMask; /* Used for wrapping indices with extra bit to distinguish full/empty. */ 59 long smallMask; /* Used for fitting indices to buffer. */ 60 char * buffer; 61 } 62 RingBuffer; 63 /* 64 * Initialize Ring Buffer. 65 * numBytes must be power of 2, returns -1 if not. 66 */ 67 long RingBuffer_Init( RingBuffer *rbuf, long numBytes, void *dataPtr ); 68 69 /* Clear buffer. Should only be called when buffer is NOT being read. */ 70 void RingBuffer_Flush( RingBuffer *rbuf ); 71 72 /* Return number of bytes available for writing. */ 73 long RingBuffer_GetWriteAvailable( RingBuffer *rbuf ); 74 /* Return number of bytes available for read. */ 75 long RingBuffer_GetReadAvailable( RingBuffer *rbuf ); 76 /* Return bytes written. */ 77 long RingBuffer_Write( RingBuffer *rbuf, const void *data, long numBytes ); 78 /* Return bytes read. */ 79 long RingBuffer_Read( RingBuffer *rbuf, void *data, long numBytes ); 80 81 /* Get address of region(s) to which we can write data. 82 ** If the region is contiguous, size2 will be zero. 83 ** If non-contiguous, size2 will be the size of second region. 84 ** Returns room available to be written or numBytes, whichever is smaller. 85 */ 86 long RingBuffer_GetWriteRegions( RingBuffer *rbuf, long numBytes, 87 void **dataPtr1, long *sizePtr1, 88 void **dataPtr2, long *sizePtr2 ); 89 long RingBuffer_AdvanceWriteIndex( RingBuffer *rbuf, long numBytes ); 90 91 /* Get address of region(s) from which we can read data. 92 ** If the region is contiguous, size2 will be zero. 93 ** If non-contiguous, size2 will be the size of second region. 94 ** Returns room available to be written or numBytes, whichever is smaller. 95 */ 96 long RingBuffer_GetReadRegions( RingBuffer *rbuf, long numBytes, 97 void **dataPtr1, long *sizePtr1, 98 void **dataPtr2, long *sizePtr2 ); 99 100 long RingBuffer_AdvanceReadIndex( RingBuffer *rbuf, long numBytes ); 101 102 #ifdef __cplusplus 103 } 104 #endif /* __cplusplus */ 105 #endif /* _RINGBUFFER_H */ 106