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.3 2006/06/10 21:30:55 dmazzoni Exp $ 10 * ringbuffer.h 11 * Ring Buffer utility.. 12 * 13 * Author: Phil Burk, http://www.softsynth.com 14 * 15 * This program is distributed with the PortAudio Portable Audio Library. 16 * For more information see: http://www.audiomulch.com/portaudio/ 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 * Any person wishing to distribute modifications to the Software is 31 * requested to send the modifications to the original developer so that 32 * they can be incorporated into the canonical version. 33 * 34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 35 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 37 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 38 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 39 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 40 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 41 * 42 */ 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <math.h> 46 #include "ringbuffer.h" 47 #include <string.h> 48 49 #ifdef USE_IAXC_RINGBUFFER_PREFIX 50 #define RingBuffer_Init _iaxc_RingBuffer_Init 51 #define RingBuffer_Flush _iaxc_RingBuffer_Flush 52 #define RingBuffer_GetWriteAvailable _iaxc_RingBuffer_GetWriteAvailable 53 #define RingBuffer_GetReadAvailable _iaxc_RingBuffer_GetReadAvailable 54 #define RingBuffer_Write _iaxc_RingBuffer_Write 55 #define RingBuffer_Read _iaxc_RingBuffer_Read 56 #define RingBuffer_GetWriteRegions _iaxc_RingBuffer_GetWriteRegions 57 #define RingBuffer_AdvanceWriteIndex _iaxc_RingBuffer_AdvanceWriteIndex 58 #define RingBuffer_GetReadRegions _iaxc_RingBuffer_GetReadRegions 59 #define RingBuffer_AdvanceReadIndex _iaxc_RingBuffer_AdvanceReadIndex 60 #endif 61 62 typedef struct 63 { 64 long bufferSize; /* Number of bytes in FIFO. Power of 2. Set by RingBuffer_Init. */ 65 long writeIndex; /* Index of next writable byte. Set by RingBuffer_AdvanceWriteIndex. */ 66 long readIndex; /* Index of next readable byte. Set by RingBuffer_AdvanceReadIndex. */ 67 long bigMask; /* Used for wrapping indices with extra bit to distinguish full/empty. */ 68 long smallMask; /* Used for fitting indices to buffer. */ 69 char *buffer; 70 } 71 RingBuffer; 72 /* 73 * Initialize Ring Buffer. 74 * numBytes must be power of 2, returns -1 if not. 75 */ 76 long RingBuffer_Init( RingBuffer *rbuf, long numBytes, void *dataPtr ); 77 78 /* Clear buffer. Should only be called when buffer is NOT being read. */ 79 void RingBuffer_Flush( RingBuffer *rbuf ); 80 81 /* Return number of bytes available for writing. */ 82 long RingBuffer_GetWriteAvailable( RingBuffer *rbuf ); 83 /* Return number of bytes available for read. */ 84 long RingBuffer_GetReadAvailable( RingBuffer *rbuf ); 85 /* Return bytes written. */ 86 long RingBuffer_Write( RingBuffer *rbuf, void *data, long numBytes ); 87 /* Return bytes read. */ 88 long RingBuffer_Read( RingBuffer *rbuf, void *data, long numBytes ); 89 90 /* Get address of region(s) to which we can write data. 91 ** If the region is contiguous, size2 will be zero. 92 ** If non-contiguous, size2 will be the size of second region. 93 ** Returns room available to be written or numBytes, whichever is smaller. 94 */ 95 long RingBuffer_GetWriteRegions( RingBuffer *rbuf, long numBytes, 96 void **dataPtr1, long *sizePtr1, 97 void **dataPtr2, long *sizePtr2 ); 98 long RingBuffer_AdvanceWriteIndex( RingBuffer *rbuf, long numBytes ); 99 100 /* Get address of region(s) from which we can read data. 101 ** If the region is contiguous, size2 will be zero. 102 ** If non-contiguous, size2 will be the size of second region. 103 ** Returns room available to be written or numBytes, whichever is smaller. 104 */ 105 long RingBuffer_GetReadRegions( RingBuffer *rbuf, long numBytes, 106 void **dataPtr1, long *sizePtr1, 107 void **dataPtr2, long *sizePtr2 ); 108 109 long RingBuffer_AdvanceReadIndex( RingBuffer *rbuf, long numBytes ); 110 111 #ifdef __cplusplus 112 } 113 #endif /* __cplusplus */ 114 #endif /* _RINGBUFFER_H */ 115