ringbuffer.h

Go to the documentation of this file.
00001 #ifndef _RINGBUFFER_H
00002 #define _RINGBUFFER_H
00003 #ifdef __cplusplus
00004 extern "C"
00005 {
00006 #endif /* __cplusplus */
00007 
00008 /*
00009  * $Id: ringbuffer.h 90 2002-01-22 00:51:49Z phil $
00010  * ringbuffer.h
00011  * Ring Buffer utility..
00012  *
00013  * Author: Phil Burk, http://www.softsynth.com
00014  * modified for SMP safety on OS X by Bjorn Roche.
00015  * also allowed for const where possible.
00016  * Note that this is safe only for a single-thread reader
00017  * and a single-thread writer.
00018  *
00019  * This program is distributed with the PortAudio Portable Audio Library.
00020  * For more information see: http://www.portaudio.com
00021  * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
00022  *
00023  * Permission is hereby granted, free of charge, to any person obtaining
00024  * a copy of this software and associated documentation files
00025  * (the "Software"), to deal in the Software without restriction,
00026  * including without limitation the rights to use, copy, modify, merge,
00027  * publish, distribute, sublicense, and/or sell copies of the Software,
00028  * and to permit persons to whom the Software is furnished to do so,
00029  * subject to the following conditions:
00030  *
00031  * The above copyright notice and this permission notice shall be
00032  * included in all copies or substantial portions of the Software.
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 /*
00044  * The text above constitutes the entire PortAudio license; however, 
00045  * the PortAudio community also makes the following non-binding requests:
00046  *
00047  * Any person wishing to distribute modifications to the Software is
00048  * requested to send the modifications to the original developer so that
00049  * they can be incorporated into the canonical version. It is also 
00050  * requested that these non-binding requests be included along with the 
00051  * license above.
00052  */
00053 
00054 #include <stdio.h>
00055 #include <stdlib.h>
00056 #include <math.h>
00057 #include "ringbuffer.h"
00058 #include <string.h>
00059 
00060 typedef struct
00061 {
00062     long   bufferSize; /* Number of bytes in FIFO. Power of 2. Set by RingBuffer_Init. */
00063     long   writeIndex; /* Index of next writable byte. Set by RingBuffer_AdvanceWriteIndex. */
00064     long   readIndex;  /* Index of next readable byte. Set by RingBuffer_AdvanceReadIndex. */
00065     long   bigMask;    /* Used for wrapping indices with extra bit to distinguish full/empty. */
00066     long   smallMask;  /* Used for fitting indices to buffer. */
00067     char * buffer;
00068 }
00069 RingBuffer;
00070 /*
00071  * Initialize Ring Buffer.
00072  * numBytes must be power of 2, returns -1 if not.
00073  */
00074 long RingBuffer_Init( RingBuffer *rbuf, long numBytes, void *dataPtr );
00075 
00076 /* Clear buffer. Should only be called when buffer is NOT being read. */
00077 void RingBuffer_Flush( RingBuffer *rbuf );
00078 
00079 /* Return number of bytes available for writing. */
00080 long RingBuffer_GetWriteAvailable( RingBuffer *rbuf );
00081 /* Return number of bytes available for read. */
00082 long RingBuffer_GetReadAvailable( RingBuffer *rbuf );
00083 /* Return bytes written. */
00084 long RingBuffer_Write( RingBuffer *rbuf, const void *data, long numBytes );
00085 /* Return bytes read. */
00086 long RingBuffer_Read( RingBuffer *rbuf, void *data, long numBytes );
00087 
00088 /* Get address of region(s) to which we can write data.
00089 ** If the region is contiguous, size2 will be zero.
00090 ** If non-contiguous, size2 will be the size of second region.
00091 ** Returns room available to be written or numBytes, whichever is smaller.
00092 */
00093 long RingBuffer_GetWriteRegions( RingBuffer *rbuf, long numBytes,
00094                                  void **dataPtr1, long *sizePtr1,
00095                                  void **dataPtr2, long *sizePtr2 );
00096 long RingBuffer_AdvanceWriteIndex( RingBuffer *rbuf, long numBytes );
00097 
00098 /* Get address of region(s) from which we can read data.
00099 ** If the region is contiguous, size2 will be zero.
00100 ** If non-contiguous, size2 will be the size of second region.
00101 ** Returns room available to be written or numBytes, whichever is smaller.
00102 */
00103 long RingBuffer_GetReadRegions( RingBuffer *rbuf, long numBytes,
00104                                 void **dataPtr1, long *sizePtr1,
00105                                 void **dataPtr2, long *sizePtr2 );
00106 
00107 long RingBuffer_AdvanceReadIndex( RingBuffer *rbuf, long numBytes );
00108 
00109 #ifdef __cplusplus
00110 }
00111 #endif /* __cplusplus */
00112 #endif /* _RINGBUFFER_H */

Generated on Mon Apr 30 04:06:40 2007 for PortAudio by  doxygen 1.4.7