pa_ringbuffer.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef PA_RINGBUFFER_H
  2. #define PA_RINGBUFFER_H
  3. /*
  4. * $Id: pa_ringbuffer.h 1873 2012-10-07 19:00:11Z philburk $
  5. * Portable Audio I/O Library
  6. * Ring Buffer utility.
  7. *
  8. * Author: Phil Burk, http://www.softsynth.com
  9. * modified for SMP safety on OS X by Bjorn Roche.
  10. * also allowed for const where possible.
  11. * modified for multiple-byte-sized data elements by Sven Fischer
  12. *
  13. * Note that this is safe only for a single-thread reader
  14. * and a single-thread writer.
  15. *
  16. * This program is distributed with the PortAudio Portable Audio Library.
  17. * For more information see: http://www.portaudio.com
  18. * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
  19. *
  20. * Permission is hereby granted, free of charge, to any person obtaining
  21. * a copy of this software and associated documentation files
  22. * (the "Software"), to deal in the Software without restriction,
  23. * including without limitation the rights to use, copy, modify, merge,
  24. * publish, distribute, sublicense, and/or sell copies of the Software,
  25. * and to permit persons to whom the Software is furnished to do so,
  26. * subject to the following conditions:
  27. *
  28. * The above copyright notice and this permission notice shall be
  29. * included in all copies or substantial portions of the Software.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  34. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  35. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  36. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  37. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. */
  39. /*
  40. * The text above constitutes the entire PortAudio license; however,
  41. * the PortAudio community also makes the following non-binding requests:
  42. *
  43. * Any person wishing to distribute modifications to the Software is
  44. * requested to send the modifications to the original developer so that
  45. * they can be incorporated into the canonical version. It is also
  46. * requested that these non-binding requests be included along with the
  47. * license above.
  48. */
  49. /** @file
  50. @ingroup common_src
  51. @brief Single-reader single-writer lock-free ring buffer
  52. PaUtilRingBuffer is a ring buffer used to transport samples between
  53. different execution contexts (threads, OS callbacks, interrupt handlers)
  54. without requiring the use of any locks. This only works when there is
  55. a single reader and a single writer (ie. one thread or callback writes
  56. to the ring buffer, another thread or callback reads from it).
  57. The PaUtilRingBuffer structure manages a ring buffer containing N
  58. elements, where N must be a power of two. An element may be any size
  59. (specified in bytes).
  60. The memory area used to store the buffer elements must be allocated by
  61. the client prior to calling PaUtil_InitializeRingBuffer() and must outlive
  62. the use of the ring buffer.
  63. @note The ring buffer functions are not normally exposed in the PortAudio libraries.
  64. If you want to call them then you will need to add pa_ringbuffer.c to your application source code.
  65. */
  66. #if defined(__APPLE__)
  67. #include <sys/types.h>
  68. typedef int32_t ring_buffer_size_t;
  69. #elif defined( __GNUC__ )
  70. typedef long ring_buffer_size_t;
  71. #elif (_MSC_VER >= 1400)
  72. typedef long ring_buffer_size_t;
  73. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  74. typedef long ring_buffer_size_t;
  75. #else
  76. typedef long ring_buffer_size_t;
  77. #endif
  78. #ifdef __cplusplus
  79. extern "C"
  80. {
  81. #endif /* __cplusplus */
  82. typedef struct PaUtilRingBuffer
  83. {
  84. ring_buffer_size_t bufferSize; /**< Number of elements in FIFO. Power of 2. Set by PaUtil_InitRingBuffer. */
  85. volatile ring_buffer_size_t writeIndex; /**< Index of next writable element. Set by PaUtil_AdvanceRingBufferWriteIndex. */
  86. volatile ring_buffer_size_t readIndex; /**< Index of next readable element. Set by PaUtil_AdvanceRingBufferReadIndex. */
  87. ring_buffer_size_t bigMask; /**< Used for wrapping indices with extra bit to distinguish full/empty. */
  88. ring_buffer_size_t smallMask; /**< Used for fitting indices to buffer. */
  89. ring_buffer_size_t elementSizeBytes; /**< Number of bytes per element. */
  90. char *buffer; /**< Pointer to the buffer containing the actual data. */
  91. }PaUtilRingBuffer;
  92. /** Initialize Ring Buffer to empty state ready to have elements written to it.
  93. @param rbuf The ring buffer.
  94. @param elementSizeBytes The size of a single data element in bytes.
  95. @param elementCount The number of elements in the buffer (must be a power of 2).
  96. @param dataPtr A pointer to a previously allocated area where the data
  97. will be maintained. It must be elementCount*elementSizeBytes long.
  98. @return -1 if elementCount is not a power of 2, otherwise 0.
  99. */
  100. ring_buffer_size_t PaUtil_InitializeRingBuffer( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementSizeBytes, ring_buffer_size_t elementCount, void *dataPtr );
  101. /** Reset buffer to empty. Should only be called when buffer is NOT being read or written.
  102. @param rbuf The ring buffer.
  103. */
  104. void PaUtil_FlushRingBuffer( PaUtilRingBuffer *rbuf );
  105. /** Retrieve the number of elements available in the ring buffer for writing.
  106. @param rbuf The ring buffer.
  107. @return The number of elements available for writing.
  108. */
  109. ring_buffer_size_t PaUtil_GetRingBufferWriteAvailable( const PaUtilRingBuffer *rbuf );
  110. /** Retrieve the number of elements available in the ring buffer for reading.
  111. @param rbuf The ring buffer.
  112. @return The number of elements available for reading.
  113. */
  114. ring_buffer_size_t PaUtil_GetRingBufferReadAvailable( const PaUtilRingBuffer *rbuf );
  115. /** Write data to the ring buffer.
  116. @param rbuf The ring buffer.
  117. @param data The address of new data to write to the buffer.
  118. @param elementCount The number of elements to be written.
  119. @return The number of elements written.
  120. */
  121. ring_buffer_size_t PaUtil_WriteRingBuffer( PaUtilRingBuffer *rbuf, const void *data, ring_buffer_size_t elementCount );
  122. /** Read data from the ring buffer.
  123. @param rbuf The ring buffer.
  124. @param data The address where the data should be stored.
  125. @param elementCount The number of elements to be read.
  126. @return The number of elements read.
  127. */
  128. ring_buffer_size_t PaUtil_ReadRingBuffer( PaUtilRingBuffer *rbuf, void *data, ring_buffer_size_t elementCount );
  129. /** Get address of region(s) to which we can write data.
  130. @param rbuf The ring buffer.
  131. @param elementCount The number of elements desired.
  132. @param dataPtr1 The address where the first (or only) region pointer will be
  133. stored.
  134. @param sizePtr1 The address where the first (or only) region length will be
  135. stored.
  136. @param dataPtr2 The address where the second region pointer will be stored if
  137. the first region is too small to satisfy elementCount.
  138. @param sizePtr2 The address where the second region length will be stored if
  139. the first region is too small to satisfy elementCount.
  140. @return The room available to be written or elementCount, whichever is smaller.
  141. */
  142. ring_buffer_size_t PaUtil_GetRingBufferWriteRegions( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount,
  143. void **dataPtr1, ring_buffer_size_t *sizePtr1,
  144. void **dataPtr2, ring_buffer_size_t *sizePtr2 );
  145. /** Advance the write index to the next location to be written.
  146. @param rbuf The ring buffer.
  147. @param elementCount The number of elements to advance.
  148. @return The new position.
  149. */
  150. ring_buffer_size_t PaUtil_AdvanceRingBufferWriteIndex( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount );
  151. /** Get address of region(s) from which we can read data.
  152. @param rbuf The ring buffer.
  153. @param elementCount The number of elements desired.
  154. @param dataPtr1 The address where the first (or only) region pointer will be
  155. stored.
  156. @param sizePtr1 The address where the first (or only) region length will be
  157. stored.
  158. @param dataPtr2 The address where the second region pointer will be stored if
  159. the first region is too small to satisfy elementCount.
  160. @param sizePtr2 The address where the second region length will be stored if
  161. the first region is too small to satisfy elementCount.
  162. @return The number of elements available for reading.
  163. */
  164. ring_buffer_size_t PaUtil_GetRingBufferReadRegions( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount,
  165. void **dataPtr1, ring_buffer_size_t *sizePtr1,
  166. void **dataPtr2, ring_buffer_size_t *sizePtr2 );
  167. /** Advance the read index to the next location to be read.
  168. @param rbuf The ring buffer.
  169. @param elementCount The number of elements to advance.
  170. @return The new position.
  171. */
  172. ring_buffer_size_t PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount );
  173. #ifdef __cplusplus
  174. }
  175. #endif /* __cplusplus */
  176. #endif /* PA_RINGBUFFER_H */