ringbuffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef _RINGBUFFER_H
  2. #define _RINGBUFFER_H
  3. #ifdef __cplusplus
  4. extern "C"
  5. {
  6. #endif /* __cplusplus */
  7. /*
  8. * $Id: ringbuffer.h,v 1.1.1.1.4.2 2003/04/28 17:45:34 philburk Exp $
  9. * ringbuffer.h
  10. * Ring Buffer utility..
  11. *
  12. * Author: Phil Burk, http://www.softsynth.com
  13. *
  14. * This program is distributed with the PortAudio Portable Audio Library.
  15. * For more information see: http://www.audiomulch.com/portaudio/
  16. * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining
  19. * a copy of this software and associated documentation files
  20. * (the "Software"), to deal in the Software without restriction,
  21. * including without limitation the rights to use, copy, modify, merge,
  22. * publish, distribute, sublicense, and/or sell copies of the Software,
  23. * and to permit persons to whom the Software is furnished to do so,
  24. * subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be
  27. * included in all copies or substantial portions of the Software.
  28. *
  29. * Any person wishing to distribute modifications to the Software is
  30. * requested to send the modifications to the original developer so that
  31. * they can be incorporated into the canonical version.
  32. *
  33. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  34. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  35. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  36. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  37. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  38. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  39. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  40. *
  41. */
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <math.h>
  45. #include "ringbuffer.h"
  46. #include <string.h>
  47. typedef struct
  48. {
  49. long bufferSize; /* Number of bytes in FIFO. Power of 2. Set by RingBuffer_Init. */
  50. /* These are declared volatile because they are written by a different thread than the reader. */
  51. volatile long writeIndex; /* Index of next writable byte. Set by RingBuffer_AdvanceWriteIndex. */
  52. volatile long readIndex; /* Index of next readable byte. Set by RingBuffer_AdvanceReadIndex. */
  53. long bigMask; /* Used for wrapping indices with extra bit to distinguish full/empty. */
  54. long smallMask; /* Used for fitting indices to buffer. */
  55. char *buffer;
  56. }
  57. RingBuffer;
  58. /*
  59. * Initialize Ring Buffer.
  60. * numBytes must be power of 2, returns -1 if not.
  61. */
  62. long RingBuffer_Init( RingBuffer *rbuf, long numBytes, void *dataPtr );
  63. /* Clear buffer. Should only be called when buffer is NOT being read. */
  64. void RingBuffer_Flush( RingBuffer *rbuf );
  65. /* Return number of bytes available for writing. */
  66. long RingBuffer_GetWriteAvailable( RingBuffer *rbuf );
  67. /* Return number of bytes available for read. */
  68. long RingBuffer_GetReadAvailable( RingBuffer *rbuf );
  69. /* Return bytes written. */
  70. long RingBuffer_Write( RingBuffer *rbuf, void *data, long numBytes );
  71. /* Return bytes read. */
  72. long RingBuffer_Read( RingBuffer *rbuf, void *data, long numBytes );
  73. /* Get address of region(s) to which we can write data.
  74. ** If the region is contiguous, size2 will be zero.
  75. ** If non-contiguous, size2 will be the size of second region.
  76. ** Returns room available to be written or numBytes, whichever is smaller.
  77. */
  78. long RingBuffer_GetWriteRegions( RingBuffer *rbuf, long numBytes,
  79. void **dataPtr1, long *sizePtr1,
  80. void **dataPtr2, long *sizePtr2 );
  81. long RingBuffer_AdvanceWriteIndex( RingBuffer *rbuf, long numBytes );
  82. /* Get address of region(s) from which we can read data.
  83. ** If the region is contiguous, size2 will be zero.
  84. ** If non-contiguous, size2 will be the size of second region.
  85. ** Returns room available to be read or numBytes, whichever is smaller.
  86. */
  87. long RingBuffer_GetReadRegions( RingBuffer *rbuf, long numBytes,
  88. void **dataPtr1, long *sizePtr1,
  89. void **dataPtr2, long *sizePtr2 );
  90. long RingBuffer_AdvanceReadIndex( RingBuffer *rbuf, long numBytes );
  91. #ifdef __cplusplus
  92. }
  93. #endif /* __cplusplus */
  94. #endif /* _RINGBUFFER_H */
粤ICP备19079148号