Show
Ignore:
Timestamp:
02/20/08 05:09:20 (10 months ago)
Author:
rossb
Message:

added support for multi-byte elements in pa_ringbuffer.c/h thanks to Sven Fischer

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • portaudio/branches/v19-devel/src/common/pa_ringbuffer.h

    r1151 r1346  
    99 * modified for SMP safety on OS X by Bjorn Roche. 
    1010 * also allowed for const where possible. 
     11 * modified for multiple-byte-sized data elements by Sven Fischer  
     12 * 
    1113 * Note that this is safe only for a single-thread reader 
    1214 * and a single-thread writer. 
     
    5860typedef struct PaUtilRingBuffer 
    5961{ 
    60     long   bufferSize; /* Number of bytes in FIFO. Power of 2. Set by PaUtil_InitRingBuffer. */ 
    61     long   writeIndex; /* Index of next writable byte. Set by PaUtil_AdvanceRingBufferWriteIndex. */ 
    62     long   readIndex;  /* Index of next readable byte. Set by PaUtil_AdvanceRingBufferReadIndex. */ 
    63     long   bigMask;    /* Used for wrapping indices with extra bit to distinguish full/empty. */ 
    64     long   smallMask;  /* Used for fitting indices to buffer. */ 
     62    long  bufferSize;   /* Number of elements in FIFO. Power of 2. Set by PaUtil_InitRingBuffer. */ 
     63    long  writeIndex;   /* Index of next writable element. Set by PaUtil_AdvanceRingBufferWriteIndex. */ 
     64    long  readIndex;    /* Index of next readable element. Set by PaUtil_AdvanceRingBufferReadIndex. */ 
     65    long  bigMask;    /* Used for wrapping indices with extra bit to distinguish full/empty. */ 
     66    long  smallMask;  /* Used for fitting indices to buffer. */ 
     67    long  elementSizeBytes; /* Number of bytes per element. */ 
    6568    char  *buffer; 
    6669}PaUtilRingBuffer; 
     
    7073 @param rbuf The ring buffer. 
    7174 
    72  @param numBytes The number of bytes in the buffer and must be power of 2. 
     75 @param elementCount The number of elements in the buffer and must be power of 2. 
    7376 
    7477 @param dataPtr A pointer to a previously allocated area where the data 
    75  will be maintained.  It must be numBytes long. 
    76  
    77  @return -1 if numBytes is not a power of 2, otherwise 0. 
    78 */ 
    79 long PaUtil_InitializeRingBuffer( PaUtilRingBuffer *rbuf, long numBytes, void *dataPtr ); 
     78 will be maintained.  It must be elementCount*elementSizeBytes long. 
     79 
     80 @return -1 if elementCount is not a power of 2, otherwise 0. 
     81*/ 
     82long PaUtil_InitializeRingBuffer( PaUtilRingBuffer *rbuf, long elementSizeBytes, long elementCount, void *dataPtr ); 
    8083 
    8184/** Clear buffer. Should only be called when buffer is NOT being read. 
     
    8588void PaUtil_FlushRingBuffer( PaUtilRingBuffer *rbuf ); 
    8689 
    87 /** Retrieve the number of bytes available in the ring buffer for writing. 
    88  
    89  @param rbuf The ring buffer. 
    90  
    91  @return The number of bytes available for writing. 
     90/** Retrieve the number of elements available in the ring buffer for writing. 
     91 
     92 @param rbuf The ring buffer. 
     93 
     94 @return The number of elements available for writing. 
    9295*/ 
    9396long PaUtil_GetRingBufferWriteAvailable( PaUtilRingBuffer *rbuf ); 
    9497 
    95 /** Retrieve the number of bytes available in the ring buffer for reading. 
    96  
    97  @param rbuf The ring buffer. 
    98  
    99  @return The number of bytes available for reading. 
     98/** Retrieve the number of elements available in the ring buffer for reading. 
     99 
     100 @param rbuf The ring buffer. 
     101 
     102 @return The number of elements available for reading. 
    100103*/ 
    101104long PaUtil_GetRingBufferReadAvailable( PaUtilRingBuffer *rbuf ); 
     
    107110 @param data The address of new data to write to the buffer. 
    108111 
    109  @param numBytes The number of bytes to be written. 
    110  
    111  @return The number of bytes written. 
    112 */ 
    113 long PaUtil_WriteRingBuffer( PaUtilRingBuffer *rbuf, const void *data, long numBytes ); 
     112 @param elementCount The number of elements to be written. 
     113 
     114 @return The number of elements written. 
     115*/ 
     116long PaUtil_WriteRingBuffer( PaUtilRingBuffer *rbuf, const void *data, long elementCount ); 
    114117 
    115118/** Read data from the ring buffer. 
     
    119122 @param data The address where the data should be stored. 
    120123 
    121  @param numBytes The number of bytes to be read. 
    122  
    123  @return The number of bytes read. 
    124 */ 
    125 long PaUtil_ReadRingBuffer( PaUtilRingBuffer *rbuf, void *data, long numBytes ); 
     124 @param elementCount The number of elements to be read. 
     125 
     126 @return The number of elements read. 
     127*/ 
     128long PaUtil_ReadRingBuffer( PaUtilRingBuffer *rbuf, void *data, long elementCount ); 
    126129 
    127130/** Get address of region(s) to which we can write data. 
     
    129132 @param rbuf The ring buffer. 
    130133 
    131  @param numBytes The number of bytes desired. 
     134 @param elementCount The number of elements desired. 
    132135 
    133136 @param dataPtr1 The address where the first (or only) region pointer will be 
     
    138141 
    139142 @param dataPtr2 The address where the second region pointer will be stored if 
    140  the first region is too small to satisfy numBytes. 
     143 the first region is too small to satisfy elementCount. 
    141144 
    142145 @param sizePtr2 The address where the second region length will be stored if 
    143  the first region is too small to satisfy numBytes. 
    144  
    145  @return The room available to be written or numBytes, whichever is smaller. 
    146 */ 
    147 long PaUtil_GetRingBufferWriteRegions( PaUtilRingBuffer *rbuf, long numBytes, 
     146 the first region is too small to satisfy elementCount. 
     147 
     148 @return The room available to be written or elementCount, whichever is smaller. 
     149*/ 
     150long PaUtil_GetRingBufferWriteRegions( PaUtilRingBuffer *rbuf, long elementCount, 
    148151                                       void **dataPtr1, long *sizePtr1, 
    149152                                       void **dataPtr2, long *sizePtr2 ); 
     
    153156 @param rbuf The ring buffer. 
    154157 
    155  @param numBytes The number of bytes to advance. 
     158 @param elementCount The number of elements to advance. 
    156159 
    157160 @return The new position. 
    158161*/ 
    159 long PaUtil_AdvanceRingBufferWriteIndex( PaUtilRingBuffer *rbuf, long numBytes ); 
     162long PaUtil_AdvanceRingBufferWriteIndex( PaUtilRingBuffer *rbuf, long elementCount ); 
    160163 
    161164/** Get address of region(s) from which we can write data. 
     
    163166 @param rbuf The ring buffer. 
    164167 
    165  @param numBytes The number of bytes desired. 
     168 @param elementCount The number of elements desired. 
    166169 
    167170 @param dataPtr1 The address where the first (or only) region pointer will be 
     
    172175 
    173176 @param dataPtr2 The address where the second region pointer will be stored if 
    174  the first region is too small to satisfy numBytes. 
     177 the first region is too small to satisfy elementCount. 
    175178 
    176179 @param sizePtr2 The address where the second region length will be stored if 
    177  the first region is too small to satisfy numBytes. 
    178  
    179  @return The number of bytes available for reading. 
    180 */ 
    181 long PaUtil_GetRingBufferReadRegions( PaUtilRingBuffer *rbuf, long numBytes, 
     180 the first region is too small to satisfy elementCount. 
     181 
     182 @return The number of elements available for reading. 
     183*/ 
     184long PaUtil_GetRingBufferReadRegions( PaUtilRingBuffer *rbuf, long elementCount, 
    182185                                      void **dataPtr1, long *sizePtr1, 
    183186                                      void **dataPtr2, long *sizePtr2 ); 
     
    187190 @param rbuf The ring buffer. 
    188191 
    189  @param numBytes The number of bytes to advance. 
     192 @param elementCount The number of elements to advance. 
    190193 
    191194 @return The new position. 
    192195*/ 
    193 long PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, long numBytes ); 
     196long PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, long elementCount ); 
    194197 
    195198#ifdef __cplusplus