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.

272 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 (int capacity) noexcept : bufferSize (capacity)
  20. {
  21. jassert (bufferSize > 0);
  22. }
  23. AbstractFifo::~AbstractFifo() {}
  24. int AbstractFifo::getTotalSize() const noexcept { return bufferSize; }
  25. int AbstractFifo::getFreeSpace() const noexcept { return bufferSize - getNumReady() - 1; }
  26. int AbstractFifo::getNumReady() const noexcept
  27. {
  28. auto vs = validStart.get();
  29. auto ve = validEnd.get();
  30. return ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  31. }
  32. void AbstractFifo::reset() noexcept
  33. {
  34. validEnd = 0;
  35. validStart = 0;
  36. }
  37. void AbstractFifo::setTotalSize (int newSize) noexcept
  38. {
  39. jassert (newSize > 0);
  40. reset();
  41. bufferSize = newSize;
  42. }
  43. //==============================================================================
  44. void AbstractFifo::prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1,
  45. int& startIndex2, int& blockSize2) const noexcept
  46. {
  47. auto vs = validStart.get();
  48. auto ve = validEnd.get();
  49. auto 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. auto newEnd = validEnd.get() + numWritten;
  71. if (newEnd >= bufferSize)
  72. newEnd -= bufferSize;
  73. validEnd = newEnd;
  74. }
  75. void AbstractFifo::prepareToRead (int numWanted, int& startIndex1, int& blockSize1,
  76. int& startIndex2, int& blockSize2) const noexcept
  77. {
  78. auto vs = validStart.get();
  79. auto ve = validEnd.get();
  80. auto numReady = ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  81. numWanted = jmin (numWanted, numReady);
  82. if (numWanted <= 0)
  83. {
  84. startIndex1 = 0;
  85. startIndex2 = 0;
  86. blockSize1 = 0;
  87. blockSize2 = 0;
  88. }
  89. else
  90. {
  91. startIndex1 = vs;
  92. startIndex2 = 0;
  93. blockSize1 = jmin (bufferSize - vs, numWanted);
  94. numWanted -= blockSize1;
  95. blockSize2 = numWanted <= 0 ? 0 : jmin (numWanted, ve);
  96. }
  97. }
  98. void AbstractFifo::finishedRead (int numRead) noexcept
  99. {
  100. jassert (numRead >= 0 && numRead <= bufferSize);
  101. auto newStart = validStart.get() + numRead;
  102. if (newStart >= bufferSize)
  103. newStart -= bufferSize;
  104. validStart = newStart;
  105. }
  106. //==============================================================================
  107. template <AbstractFifo::ReadOrWrite mode>
  108. AbstractFifo::ScopedReadWrite<mode>::ScopedReadWrite (ScopedReadWrite&& other) noexcept
  109. : startIndex1 (other.startIndex1),
  110. blockSize1 (other.blockSize1),
  111. startIndex2 (other.startIndex2),
  112. blockSize2 (other.blockSize2)
  113. {
  114. swap (other);
  115. }
  116. template <AbstractFifo::ReadOrWrite mode>
  117. AbstractFifo::ScopedReadWrite<mode>&
  118. AbstractFifo::ScopedReadWrite<mode>::operator= (ScopedReadWrite&& other) noexcept
  119. {
  120. swap (other);
  121. return *this;
  122. }
  123. template <AbstractFifo::ReadOrWrite mode>
  124. void AbstractFifo::ScopedReadWrite<mode>::swap (ScopedReadWrite& other) noexcept
  125. {
  126. std::swap (other.fifo, fifo);
  127. std::swap (other.startIndex1, startIndex1);
  128. std::swap (other.blockSize1, blockSize1);
  129. std::swap (other.startIndex2, startIndex2);
  130. std::swap (other.blockSize2, blockSize2);
  131. }
  132. template class AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>;
  133. template class AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>;
  134. AbstractFifo::ScopedRead AbstractFifo::read (int numToRead) noexcept { return { *this, numToRead }; }
  135. AbstractFifo::ScopedWrite AbstractFifo::write (int numToWrite) noexcept { return { *this, numToWrite }; }
  136. //==============================================================================
  137. //==============================================================================
  138. #if JUCE_UNIT_TESTS
  139. class AbstractFifoTests : public UnitTest
  140. {
  141. public:
  142. AbstractFifoTests()
  143. : UnitTest ("Abstract Fifo", UnitTestCategories::containers)
  144. {}
  145. struct WriteThread : public Thread
  146. {
  147. WriteThread (AbstractFifo& f, int* b, Random rng)
  148. : Thread ("fifo writer"), fifo (f), buffer (b), random (rng)
  149. {
  150. startThread();
  151. }
  152. ~WriteThread()
  153. {
  154. stopThread (5000);
  155. }
  156. void run()
  157. {
  158. int n = 0;
  159. while (! threadShouldExit())
  160. {
  161. int num = random.nextInt (2000) + 1;
  162. auto writer = fifo.write (num);
  163. jassert (writer.blockSize1 >= 0 && writer.blockSize2 >= 0);
  164. jassert (writer.blockSize1 == 0
  165. || (writer.startIndex1 >= 0 && writer.startIndex1 < fifo.getTotalSize()));
  166. jassert (writer.blockSize2 == 0
  167. || (writer.startIndex2 >= 0 && writer.startIndex2 < fifo.getTotalSize()));
  168. writer.forEach ([this, &n] (int index) { this->buffer[index] = n++; });
  169. }
  170. }
  171. AbstractFifo& fifo;
  172. int* buffer;
  173. Random random;
  174. };
  175. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6262)
  176. void runTest() override
  177. {
  178. beginTest ("AbstractFifo");
  179. int buffer[5000];
  180. AbstractFifo fifo (numElementsInArray (buffer));
  181. WriteThread writer (fifo, buffer, getRandom());
  182. int n = 0;
  183. Random r = getRandom();
  184. r.combineSeed (12345);
  185. for (int count = 100000; --count >= 0;)
  186. {
  187. int num = r.nextInt (6000) + 1;
  188. auto reader = fifo.read (num);
  189. if (! (reader.blockSize1 >= 0 && reader.blockSize2 >= 0)
  190. && (reader.blockSize1 == 0
  191. || (reader.startIndex1 >= 0 && reader.startIndex1 < fifo.getTotalSize()))
  192. && (reader.blockSize2 == 0
  193. || (reader.startIndex2 >= 0 && reader.startIndex2 < fifo.getTotalSize())))
  194. {
  195. expect (false, "prepareToRead returned -ve values");
  196. break;
  197. }
  198. bool failed = false;
  199. reader.forEach ([&failed, &buffer, &n] (int index)
  200. {
  201. failed = (buffer[index] != n++) || failed;
  202. });
  203. if (failed)
  204. {
  205. expect (false, "read values were incorrect");
  206. break;
  207. }
  208. }
  209. }
  210. JUCE_END_IGNORE_WARNINGS_MSVC
  211. };
  212. static AbstractFifoTests fifoUnitTests;
  213. #endif
  214. } // namespace juce