The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. AbstractFifo::AbstractFifo (const int capacity) noexcept
  20. : bufferSize (capacity)
  21. {
  22. jassert (bufferSize > 0);
  23. }
  24. AbstractFifo::~AbstractFifo() {}
  25. int AbstractFifo::getTotalSize() const noexcept { return bufferSize; }
  26. int AbstractFifo::getFreeSpace() const noexcept { return bufferSize - getNumReady() - 1; }
  27. int AbstractFifo::getNumReady() const noexcept
  28. {
  29. const int vs = validStart.get();
  30. const int ve = validEnd.get();
  31. return ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  32. }
  33. void AbstractFifo::reset() noexcept
  34. {
  35. validEnd = 0;
  36. validStart = 0;
  37. }
  38. void AbstractFifo::setTotalSize (int newSize) noexcept
  39. {
  40. jassert (newSize > 0);
  41. reset();
  42. bufferSize = newSize;
  43. }
  44. //==============================================================================
  45. void AbstractFifo::prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept
  46. {
  47. const int vs = validStart.get();
  48. const int ve = validEnd.value;
  49. const int freeSpace = ve >= vs ? (bufferSize - (ve - vs)) : (vs - ve);
  50. numToWrite = jmin (numToWrite, freeSpace - 1);
  51. if (numToWrite <= 0)
  52. {
  53. startIndex1 = 0;
  54. startIndex2 = 0;
  55. blockSize1 = 0;
  56. blockSize2 = 0;
  57. }
  58. else
  59. {
  60. startIndex1 = ve;
  61. startIndex2 = 0;
  62. blockSize1 = jmin (bufferSize - ve, numToWrite);
  63. numToWrite -= blockSize1;
  64. blockSize2 = numToWrite <= 0 ? 0 : jmin (numToWrite, vs);
  65. }
  66. }
  67. void AbstractFifo::finishedWrite (int numWritten) noexcept
  68. {
  69. jassert (numWritten >= 0 && numWritten < bufferSize);
  70. int newEnd = validEnd.value + numWritten;
  71. if (newEnd >= bufferSize)
  72. newEnd -= bufferSize;
  73. validEnd = newEnd;
  74. }
  75. void AbstractFifo::prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept
  76. {
  77. const int vs = validStart.value;
  78. const int ve = validEnd.get();
  79. const int numReady = ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  80. numWanted = jmin (numWanted, numReady);
  81. if (numWanted <= 0)
  82. {
  83. startIndex1 = 0;
  84. startIndex2 = 0;
  85. blockSize1 = 0;
  86. blockSize2 = 0;
  87. }
  88. else
  89. {
  90. startIndex1 = vs;
  91. startIndex2 = 0;
  92. blockSize1 = jmin (bufferSize - vs, numWanted);
  93. numWanted -= blockSize1;
  94. blockSize2 = numWanted <= 0 ? 0 : jmin (numWanted, ve);
  95. }
  96. }
  97. void AbstractFifo::finishedRead (int numRead) noexcept
  98. {
  99. jassert (numRead >= 0 && numRead <= bufferSize);
  100. int newStart = validStart.value + numRead;
  101. if (newStart >= bufferSize)
  102. newStart -= bufferSize;
  103. validStart = newStart;
  104. }
  105. //==============================================================================
  106. //==============================================================================
  107. #if JUCE_UNIT_TESTS
  108. class AbstractFifoTests : public UnitTest
  109. {
  110. public:
  111. AbstractFifoTests() : UnitTest ("Abstract Fifo", "Containers") {}
  112. class WriteThread : public Thread
  113. {
  114. public:
  115. WriteThread (AbstractFifo& f, int* b, Random rng)
  116. : Thread ("fifo writer"), fifo (f), buffer (b), random (rng)
  117. {
  118. startThread();
  119. }
  120. ~WriteThread()
  121. {
  122. stopThread (5000);
  123. }
  124. void run()
  125. {
  126. int n = 0;
  127. while (! threadShouldExit())
  128. {
  129. int num = random.nextInt (2000) + 1;
  130. int start1, size1, start2, size2;
  131. fifo.prepareToWrite (num, start1, size1, start2, size2);
  132. jassert (size1 >= 0 && size2 >= 0);
  133. jassert (size1 == 0 || (start1 >= 0 && start1 < fifo.getTotalSize()));
  134. jassert (size2 == 0 || (start2 >= 0 && start2 < fifo.getTotalSize()));
  135. for (int i = 0; i < size1; ++i)
  136. buffer [start1 + i] = n++;
  137. for (int i = 0; i < size2; ++i)
  138. buffer [start2 + i] = n++;
  139. fifo.finishedWrite (size1 + size2);
  140. }
  141. }
  142. private:
  143. AbstractFifo& fifo;
  144. int* buffer;
  145. Random random;
  146. };
  147. void runTest() override
  148. {
  149. beginTest ("AbstractFifo");
  150. int buffer [5000];
  151. AbstractFifo fifo (numElementsInArray (buffer));
  152. WriteThread writer (fifo, buffer, getRandom());
  153. int n = 0;
  154. Random r = getRandom();
  155. r.combineSeed (12345);
  156. for (int count = 100000; --count >= 0;)
  157. {
  158. int num = r.nextInt (6000) + 1;
  159. int start1, size1, start2, size2;
  160. fifo.prepareToRead (num, start1, size1, start2, size2);
  161. if (! (size1 >= 0 && size2 >= 0)
  162. && (size1 == 0 || (start1 >= 0 && start1 < fifo.getTotalSize()))
  163. && (size2 == 0 || (start2 >= 0 && start2 < fifo.getTotalSize())))
  164. {
  165. expect (false, "prepareToRead returned -ve values");
  166. break;
  167. }
  168. bool failed = false;
  169. for (int i = 0; i < size1; ++i)
  170. failed = (buffer [start1 + i] != n++) || failed;
  171. for (int i = 0; i < size2; ++i)
  172. failed = (buffer [start2 + i] != n++) || failed;
  173. if (failed)
  174. {
  175. expect (false, "read values were incorrect");
  176. break;
  177. }
  178. fifo.finishedRead (size1 + size2);
  179. }
  180. }
  181. };
  182. static AbstractFifoTests fifoUnitTests;
  183. #endif
  184. } // namespace juce