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.

270 lines
7.8KB

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