ringbuffer.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * $Id: ringbuffer.c,v 1.1.1.1 2002/01/22 00:52:53 phil Exp $
  3. * ringbuffer.c
  4. * Ring Buffer utility..
  5. *
  6. * Author: Phil Burk, http://www.softsynth.com
  7. *
  8. * This program uses the PortAudio Portable Audio Library.
  9. * For more information see: http://www.audiomulch.com/portaudio/
  10. * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining
  13. * a copy of this software and associated documentation files
  14. * (the "Software"), to deal in the Software without restriction,
  15. * including without limitation the rights to use, copy, modify, merge,
  16. * publish, distribute, sublicense, and/or sell copies of the Software,
  17. * and to permit persons to whom the Software is furnished to do so,
  18. * subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be
  21. * included in all copies or substantial portions of the Software.
  22. *
  23. * Any person wishing to distribute modifications to the Software is
  24. * requested to send the modifications to the original developer so that
  25. * they can be incorporated into the canonical version.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  30. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  31. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  32. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. *
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <math.h>
  39. #include "ringbuffer.h"
  40. #include <string.h>
  41. /***************************************************************************
  42. * Initialize FIFO.
  43. * numBytes must be power of 2, returns -1 if not.
  44. */
  45. long RingBuffer_Init( RingBuffer *rbuf, long numBytes, void *dataPtr )
  46. {
  47. if( ((numBytes-1) & numBytes) != 0) return -1; /* Not Power of two. */
  48. rbuf->bufferSize = numBytes;
  49. rbuf->buffer = (char *)dataPtr;
  50. RingBuffer_Flush( rbuf );
  51. rbuf->bigMask = (numBytes*2)-1;
  52. rbuf->smallMask = (numBytes)-1;
  53. return 0;
  54. }
  55. /***************************************************************************
  56. ** Return number of bytes available for reading. */
  57. long RingBuffer_GetReadAvailable( RingBuffer *rbuf )
  58. {
  59. return ( (rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask );
  60. }
  61. /***************************************************************************
  62. ** Return number of bytes available for writing. */
  63. long RingBuffer_GetWriteAvailable( RingBuffer *rbuf )
  64. {
  65. return ( rbuf->bufferSize - RingBuffer_GetReadAvailable(rbuf));
  66. }
  67. /***************************************************************************
  68. ** Clear buffer. Should only be called when buffer is NOT being read. */
  69. void RingBuffer_Flush( RingBuffer *rbuf )
  70. {
  71. rbuf->writeIndex = rbuf->readIndex = 0;
  72. }
  73. /***************************************************************************
  74. ** Get address of region(s) to which we can write data.
  75. ** If the region is contiguous, size2 will be zero.
  76. ** If non-contiguous, size2 will be the size of second region.
  77. ** Returns room available to be written or numBytes, whichever is smaller.
  78. */
  79. long RingBuffer_GetWriteRegions( RingBuffer *rbuf, long numBytes,
  80. void **dataPtr1, long *sizePtr1,
  81. void **dataPtr2, long *sizePtr2 )
  82. {
  83. long index;
  84. long available = RingBuffer_GetWriteAvailable( rbuf );
  85. if( numBytes > available ) numBytes = available;
  86. /* Check to see if write is not contiguous. */
  87. index = rbuf->writeIndex & rbuf->smallMask;
  88. if( (index + numBytes) > rbuf->bufferSize )
  89. {
  90. /* Write data in two blocks that wrap the buffer. */
  91. long firstHalf = rbuf->bufferSize - index;
  92. *dataPtr1 = &rbuf->buffer[index];
  93. *sizePtr1 = firstHalf;
  94. *dataPtr2 = &rbuf->buffer[0];
  95. *sizePtr2 = numBytes - firstHalf;
  96. }
  97. else
  98. {
  99. *dataPtr1 = &rbuf->buffer[index];
  100. *sizePtr1 = numBytes;
  101. *dataPtr2 = NULL;
  102. *sizePtr2 = 0;
  103. }
  104. return numBytes;
  105. }
  106. /***************************************************************************
  107. */
  108. long RingBuffer_AdvanceWriteIndex( RingBuffer *rbuf, long numBytes )
  109. {
  110. return rbuf->writeIndex = (rbuf->writeIndex + numBytes) & rbuf->bigMask;
  111. }
  112. /***************************************************************************
  113. ** Get address of region(s) from which we can read data.
  114. ** If the region is contiguous, size2 will be zero.
  115. ** If non-contiguous, size2 will be the size of second region.
  116. ** Returns room available to be written or numBytes, whichever is smaller.
  117. */
  118. long RingBuffer_GetReadRegions( RingBuffer *rbuf, long numBytes,
  119. void **dataPtr1, long *sizePtr1,
  120. void **dataPtr2, long *sizePtr2 )
  121. {
  122. long index;
  123. long available = RingBuffer_GetReadAvailable( rbuf );
  124. if( numBytes > available ) numBytes = available;
  125. /* Check to see if read is not contiguous. */
  126. index = rbuf->readIndex & rbuf->smallMask;
  127. if( (index + numBytes) > rbuf->bufferSize )
  128. {
  129. /* Write data in two blocks that wrap the buffer. */
  130. long firstHalf = rbuf->bufferSize - index;
  131. *dataPtr1 = &rbuf->buffer[index];
  132. *sizePtr1 = firstHalf;
  133. *dataPtr2 = &rbuf->buffer[0];
  134. *sizePtr2 = numBytes - firstHalf;
  135. }
  136. else
  137. {
  138. *dataPtr1 = &rbuf->buffer[index];
  139. *sizePtr1 = numBytes;
  140. *dataPtr2 = NULL;
  141. *sizePtr2 = 0;
  142. }
  143. return numBytes;
  144. }
  145. /***************************************************************************
  146. */
  147. long RingBuffer_AdvanceReadIndex( RingBuffer *rbuf, long numBytes )
  148. {
  149. return rbuf->readIndex = (rbuf->readIndex + numBytes) & rbuf->bigMask;
  150. }
  151. /***************************************************************************
  152. ** Return bytes written. */
  153. long RingBuffer_Write( RingBuffer *rbuf, void *data, long numBytes )
  154. {
  155. long size1, size2, numWritten;
  156. void *data1, *data2;
  157. numWritten = RingBuffer_GetWriteRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 );
  158. if( size2 > 0 )
  159. {
  160. memcpy( data1, data, size1 );
  161. data = ((char *)data) + size1;
  162. memcpy( data2, data, size2 );
  163. }
  164. else
  165. {
  166. memcpy( data1, data, size1 );
  167. }
  168. RingBuffer_AdvanceWriteIndex( rbuf, numWritten );
  169. return numWritten;
  170. }
  171. /***************************************************************************
  172. ** Return bytes read. */
  173. long RingBuffer_Read( RingBuffer *rbuf, void *data, long numBytes )
  174. {
  175. long size1, size2, numRead;
  176. void *data1, *data2;
  177. numRead = RingBuffer_GetReadRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 );
  178. if( size2 > 0 )
  179. {
  180. memcpy( data, data1, size1 );
  181. data = ((char *)data) + size1;
  182. memcpy( data, data2, size2 );
  183. }
  184. else
  185. {
  186. memcpy( data, data1, size1 );
  187. }
  188. RingBuffer_AdvanceReadIndex( rbuf, numRead );
  189. return numRead;
  190. }
粤ICP备19079148号