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 
00059 #include <stdio.h>
00060 #include <stdlib.h>
00061 #include <math.h>
00062 #include "ringbuffer.h"
00063 #include <string.h>
00064 
00065 typedef struct
00066 {
00067     long   bufferSize; /* Number of bytes in FIFO. Power of 2. Set by RingBuffer_Init. */
00068     long   writeIndex; /* Index of next writable byte. Set by RingBuffer_AdvanceWriteIndex. */
00069     long   readIndex;  /* Index of next readable byte. Set by RingBuffer_AdvanceReadIndex. */
00070     long   bigMask;    /* Used for wrapping indices with extra bit to distinguish full/empty. */
00071     long   smallMask;  /* Used for fitting indices to buffer. */
00072     char * buffer;
00073 }
00074 RingBuffer;
00075 /*
00076  * Initialize Ring Buffer.
00077  * numBytes must be power of 2, returns -1 if not.
00078  */
00079 long RingBuffer_Init( RingBuffer *rbuf, long numBytes, void *dataPtr );
00080 
00081 /* Clear buffer. Should only be called when buffer is NOT being read. */
00082 void RingBuffer_Flush( RingBuffer *rbuf );
00083 
00084 /* Return number of bytes available for writing. */
00085 long RingBuffer_GetWriteAvailable( RingBuffer *rbuf );
00086 /* Return number of bytes available for read. */
00087 long RingBuffer_GetReadAvailable( RingBuffer *rbuf );
00088 /* Return bytes written. */
00089 long RingBuffer_Write( RingBuffer *rbuf, const void *data, long numBytes );
00090 /* Return bytes read. */
00091 long RingBuffer_Read( RingBuffer *rbuf, void *data, long numBytes );
00092 
00093 /* Get address of region(s) to which we can write data.
00094 ** If the region is contiguous, size2 will be zero.
00095 ** If non-contiguous, size2 will be the size of second region.
00096 ** Returns room available to be written or numBytes, whichever is smaller.
00097 */
00098 long RingBuffer_GetWriteRegions( RingBuffer *rbuf, long numBytes,
00099                                  void **dataPtr1, long *sizePtr1,
00100                                  void **dataPtr2, long *sizePtr2 );
00101 long RingBuffer_AdvanceWriteIndex( RingBuffer *rbuf, long numBytes );
00102 
00103 /* Get address of region(s) from which we can read data.
00104 ** If the region is contiguous, size2 will be zero.
00105 ** If non-contiguous, size2 will be the size of second region.
00106 ** Returns room available to be written or numBytes, whichever is smaller.
00107 */
00108 long RingBuffer_GetReadRegions( RingBuffer *rbuf, long numBytes,
00109                                 void **dataPtr1, long *sizePtr1,
00110                                 void **dataPtr2, long *sizePtr2 );
00111 
00112 long RingBuffer_AdvanceReadIndex( RingBuffer *rbuf, long numBytes );
00113 
00114 #ifdef __cplusplus
00115 }
00116 #endif /* __cplusplus */
00117 #endif /* _RINGBUFFER_H */

Generated on Mon Sep 3 11:35:44 2007 for PortAudio by  doxygen 1.5.2