00001 #ifndef _RINGBUFFER_H 00002 #define _RINGBUFFER_H 00003 #ifdef __cplusplus 00004 extern "C" 00005 { 00006 #endif /* __cplusplus */ 00007 00008 /* 00009 * $Id: ringbuffer.h 94 2003-05-13 10:54:17Z rene-cvs $ 00010 * ringbuffer.h 00011 * Ring Buffer utility.. 00012 * 00013 * Author: Phil Burk, http://www.softsynth.com 00014 * 00015 * This program is distributed with the PortAudio Portable Audio Library. 00016 * For more information see: http://www.audiomulch.com/portaudio/ 00017 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk 00018 * 00019 * Permission is hereby granted, free of charge, to any person obtaining 00020 * a copy of this software and associated documentation files 00021 * (the "Software"), to deal in the Software without restriction, 00022 * including without limitation the rights to use, copy, modify, merge, 00023 * publish, distribute, sublicense, and/or sell copies of the Software, 00024 * and to permit persons to whom the Software is furnished to do so, 00025 * subject to the following conditions: 00026 * 00027 * The above copyright notice and this permission notice shall be 00028 * included in all copies or substantial portions of the Software. 00029 * 00030 * Any person wishing to distribute modifications to the Software is 00031 * requested to send the modifications to the original developer so that 00032 * they can be incorporated into the canonical version. 00033 * 00034 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00035 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00036 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00037 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 00038 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 00039 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00040 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00041 * 00042 */ 00043 #include <stdio.h> 00044 #include <stdlib.h> 00045 #include <math.h> 00046 #include "ringbuffer.h" 00047 #include <string.h> 00048 00049 typedef struct 00050 { 00051 long bufferSize; /* Number of bytes in FIFO. Power of 2. Set by RingBuffer_Init. */ 00052 long writeIndex; /* Index of next writable byte. Set by RingBuffer_AdvanceWriteIndex. */ 00053 long readIndex; /* Index of next readable byte. Set by RingBuffer_AdvanceReadIndex. */ 00054 long bigMask; /* Used for wrapping indices with extra bit to distinguish full/empty. */ 00055 long smallMask; /* Used for fitting indices to buffer. */ 00056 char *buffer; 00057 } 00058 RingBuffer; 00059 /* 00060 * Initialize Ring Buffer. 00061 * numBytes must be power of 2, returns -1 if not. 00062 */ 00063 long RingBuffer_Init( RingBuffer *rbuf, long numBytes, void *dataPtr ); 00064 00065 /* Clear buffer. Should only be called when buffer is NOT being read. */ 00066 void RingBuffer_Flush( RingBuffer *rbuf ); 00067 00068 /* Return number of bytes available for writing. */ 00069 long RingBuffer_GetWriteAvailable( RingBuffer *rbuf ); 00070 /* Return number of bytes available for read. */ 00071 long RingBuffer_GetReadAvailable( RingBuffer *rbuf ); 00072 /* Return bytes written. */ 00073 long RingBuffer_Write( RingBuffer *rbuf, void *data, long numBytes ); 00074 /* Return bytes read. */ 00075 long RingBuffer_Read( RingBuffer *rbuf, void *data, long numBytes ); 00076 00077 /* Get address of region(s) to which we can write data. 00078 ** If the region is contiguous, size2 will be zero. 00079 ** If non-contiguous, size2 will be the size of second region. 00080 ** Returns room available to be written or numBytes, whichever is smaller. 00081 */ 00082 long RingBuffer_GetWriteRegions( RingBuffer *rbuf, long numBytes, 00083 void **dataPtr1, long *sizePtr1, 00084 void **dataPtr2, long *sizePtr2 ); 00085 long RingBuffer_AdvanceWriteIndex( RingBuffer *rbuf, long numBytes ); 00086 00087 /* Get address of region(s) from which we can read data. 00088 ** If the region is contiguous, size2 will be zero. 00089 ** If non-contiguous, size2 will be the size of second region. 00090 ** Returns room available to be written or numBytes, whichever is smaller. 00091 */ 00092 long RingBuffer_GetReadRegions( RingBuffer *rbuf, long numBytes, 00093 void **dataPtr1, long *sizePtr1, 00094 void **dataPtr2, long *sizePtr2 ); 00095 00096 long RingBuffer_AdvanceReadIndex( RingBuffer *rbuf, long numBytes ); 00097 00098 #ifdef __cplusplus 00099 } 00100 #endif /* __cplusplus */ 00101 #endif /* _RINGBUFFER_H */