juce_AbstractFifo.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. AbstractFifo::AbstractFifo (const int capacity) noexcept
  22. : bufferSize (capacity)
  23. {
  24. jassert (bufferSize > 0);
  25. }
  26. AbstractFifo::~AbstractFifo() {}
  27. int AbstractFifo::getTotalSize() const noexcept { return bufferSize; }
  28. int AbstractFifo::getFreeSpace() const noexcept { return bufferSize - getNumReady() - 1; }
  29. int AbstractFifo::getNumReady() const noexcept
  30. {
  31. const int vs = validStart.get();
  32. const int ve = validEnd.get();
  33. return ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  34. }
  35. void AbstractFifo::reset() noexcept
  36. {
  37. validEnd = 0;
  38. validStart = 0;
  39. }
  40. void AbstractFifo::setTotalSize (int newSize) noexcept
  41. {
  42. jassert (newSize > 0);
  43. reset();
  44. bufferSize = newSize;
  45. }
  46. //==============================================================================
  47. void AbstractFifo::prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept
  48. {
  49. const int vs = validStart.get();
  50. const int ve = validEnd.value;
  51. const int freeSpace = ve >= vs ? (bufferSize - (ve - vs)) : (vs - ve);
  52. numToWrite = jmin (numToWrite, freeSpace - 1);
  53. if (numToWrite <= 0)
  54. {
  55. startIndex1 = 0;
  56. startIndex2 = 0;
  57. blockSize1 = 0;
  58. blockSize2 = 0;
  59. }
  60. else
  61. {
  62. startIndex1 = ve;
  63. startIndex2 = 0;
  64. blockSize1 = jmin (bufferSize - ve, numToWrite);
  65. numToWrite -= blockSize1;
  66. blockSize2 = numToWrite <= 0 ? 0 : jmin (numToWrite, vs);
  67. }
  68. }
  69. void AbstractFifo::finishedWrite (int numWritten) noexcept
  70. {
  71. jassert (numWritten >= 0 && numWritten < bufferSize);
  72. int newEnd = validEnd.value + numWritten;
  73. if (newEnd >= bufferSize)
  74. newEnd -= bufferSize;
  75. validEnd = newEnd;
  76. }
  77. void AbstractFifo::prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept
  78. {
  79. const int vs = validStart.value;
  80. const int ve = validEnd.get();
  81. const int numReady = ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  82. numWanted = jmin (numWanted, numReady);
  83. if (numWanted <= 0)
  84. {
  85. startIndex1 = 0;
  86. startIndex2 = 0;
  87. blockSize1 = 0;
  88. blockSize2 = 0;
  89. }
  90. else
  91. {
  92. startIndex1 = vs;
  93. startIndex2 = 0;
  94. blockSize1 = jmin (bufferSize - vs, numWanted);
  95. numWanted -= blockSize1;
  96. blockSize2 = numWanted <= 0 ? 0 : jmin (numWanted, ve);
  97. }
  98. }
  99. void AbstractFifo::finishedRead (int numRead) noexcept
  100. {
  101. jassert (numRead >= 0 && numRead <= bufferSize);
  102. int newStart = validStart.value + numRead;
  103. if (newStart >= bufferSize)
  104. newStart -= bufferSize;
  105. validStart = newStart;
  106. }
  107. //==============================================================================
  108. //==============================================================================
  109. #if JUCE_UNIT_TESTS
  110. class AbstractFifoTests : public UnitTest
  111. {
  112. public:
  113. AbstractFifoTests() : UnitTest ("Abstract Fifo") {}
  114. class WriteThread : public Thread
  115. {
  116. public:
  117. WriteThread (AbstractFifo& f, int* b, Random rng)
  118. : Thread ("fifo writer"), fifo (f), buffer (b), random (rng)
  119. {
  120. startThread();
  121. }
  122. ~WriteThread()
  123. {
  124. stopThread (5000);
  125. }
  126. void run()
  127. {
  128. int n = 0;
  129. while (! threadShouldExit())
  130. {
  131. int num = random.nextInt (2000) + 1;
  132. int start1, size1, start2, size2;
  133. fifo.prepareToWrite (num, start1, size1, start2, size2);
  134. jassert (size1 >= 0 && size2 >= 0);
  135. jassert (size1 == 0 || (start1 >= 0 && start1 < fifo.getTotalSize()));
  136. jassert (size2 == 0 || (start2 >= 0 && start2 < fifo.getTotalSize()));
  137. for (int i = 0; i < size1; ++i)
  138. buffer [start1 + i] = n++;
  139. for (int i = 0; i < size2; ++i)
  140. buffer [start2 + i] = n++;
  141. fifo.finishedWrite (size1 + size2);
  142. }
  143. }
  144. private:
  145. AbstractFifo& fifo;
  146. int* buffer;
  147. Random random;
  148. };
  149. void runTest()
  150. {
  151. beginTest ("AbstractFifo");
  152. int buffer [5000];
  153. AbstractFifo fifo (numElementsInArray (buffer));
  154. WriteThread writer (fifo, buffer, getRandom());
  155. int n = 0;
  156. Random r = getRandom();
  157. r.combineSeed (12345);
  158. for (int count = 100000; --count >= 0;)
  159. {
  160. int num = r.nextInt (6000) + 1;
  161. int start1, size1, start2, size2;
  162. fifo.prepareToRead (num, start1, size1, start2, size2);
  163. if (! (size1 >= 0 && size2 >= 0)
  164. && (size1 == 0 || (start1 >= 0 && start1 < fifo.getTotalSize()))
  165. && (size2 == 0 || (start2 >= 0 && start2 < fifo.getTotalSize())))
  166. {
  167. expect (false, "prepareToRead returned -ve values");
  168. break;
  169. }
  170. bool failed = false;
  171. for (int i = 0; i < size1; ++i)
  172. failed = (buffer [start1 + i] != n++) || failed;
  173. for (int i = 0; i < size2; ++i)
  174. failed = (buffer [start2 + i] != n++) || failed;
  175. if (failed)
  176. {
  177. expect (false, "read values were incorrect");
  178. break;
  179. }
  180. fifo.finishedRead (size1 + size2);
  181. }
  182. }
  183. };
  184. static AbstractFifoTests fifoUnitTests;
  185. #endif