Audio plugin host https://kx.studio/carla
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.

442 lines
11KB

  1. /*
  2. * Carla Ring Buffer
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_RING_BUFFER_HPP_INCLUDED
  18. #define CARLA_RING_BUFFER_HPP_INCLUDED
  19. #include "CarlaMathUtils.hpp"
  20. // -----------------------------------------------------------------------
  21. // Buffer structs
  22. /*
  23. head:
  24. current writing position, headmost position of the buffer.
  25. increments when writing.
  26. tail:
  27. current reading position, last used position of the buffer.
  28. increments when reading.
  29. head == tail means empty buffer
  30. wrtn:
  31. temporary position of head until a commitWrite() is called.
  32. if buffer writing fails, wrtn will be back to head position thus ignoring the last operation(s).
  33. if buffer writing succeeds, head will be set to this variable.
  34. invalidateCommit:
  35. boolean used to check if a write operation failed.
  36. this ensures we don't get incomplete writes.
  37. */
  38. struct HeapBuffer {
  39. uint32_t size;
  40. uint32_t head, tail, wrtn;
  41. bool invalidateCommit;
  42. uint8_t* buf;
  43. void copyDataFrom(const HeapBuffer& rb) noexcept
  44. {
  45. CARLA_SAFE_ASSERT_RETURN(size == rb.size,);
  46. head = rb.head;
  47. tail = rb.tail;
  48. wrtn = rb.wrtn;
  49. invalidateCommit = rb.invalidateCommit;
  50. std::memcpy(buf, rb.buf, size);
  51. }
  52. };
  53. struct StackBuffer {
  54. static const uint32_t size = 4096;
  55. uint32_t head, tail, wrtn;
  56. bool invalidateCommit;
  57. uint8_t buf[size];
  58. };
  59. struct BigStackBuffer {
  60. static const uint32_t size = 16384;
  61. uint32_t head, tail, wrtn;
  62. bool invalidateCommit;
  63. uint8_t buf[size];
  64. };
  65. // -----------------------------------------------------------------------
  66. // CarlaRingBuffer templated class
  67. template <class BufferStruct>
  68. class CarlaRingBuffer
  69. {
  70. public:
  71. CarlaRingBuffer() noexcept
  72. : fBuffer(nullptr) {}
  73. CarlaRingBuffer(BufferStruct* const ringBuf) noexcept
  74. : fBuffer(ringBuf)
  75. {
  76. if (ringBuf != nullptr)
  77. clear();
  78. }
  79. void clear() noexcept
  80. {
  81. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  82. fBuffer->head = 0;
  83. fBuffer->tail = 0;
  84. fBuffer->wrtn = 0;
  85. fBuffer->invalidateCommit = false;
  86. carla_zeroBytes(fBuffer->buf, fBuffer->size);
  87. }
  88. // -------------------------------------------------------------------
  89. bool commitWrite() noexcept
  90. {
  91. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  92. if (fBuffer->invalidateCommit)
  93. {
  94. fBuffer->wrtn = fBuffer->head;
  95. fBuffer->invalidateCommit = false;
  96. return false;
  97. }
  98. // nothing to commit?
  99. CARLA_SAFE_ASSERT_RETURN(fBuffer->head != fBuffer->wrtn, false);
  100. // all ok
  101. fBuffer->head = fBuffer->wrtn;
  102. return true;
  103. }
  104. bool isDataAvailableForReading() const noexcept
  105. {
  106. return (fBuffer != nullptr && fBuffer->buf != nullptr && fBuffer->head != fBuffer->tail);
  107. }
  108. bool isEmpty() const noexcept
  109. {
  110. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  111. return (fBuffer->buf == nullptr || fBuffer->head == fBuffer->tail);
  112. }
  113. // -------------------------------------------------------------------
  114. bool readBool() noexcept
  115. {
  116. bool b = false;
  117. return tryRead(&b, sizeof(bool)) ? b : false;
  118. }
  119. uint8_t readByte() noexcept
  120. {
  121. int8_t ub = -1;
  122. return (tryRead(&ub, sizeof(int8_t)) && ub >= 0 && ub <= INT_LEAST8_MAX) ? static_cast<uint8_t>(ub) : 0;
  123. }
  124. int16_t readShort() noexcept
  125. {
  126. int16_t s = 0;
  127. return tryRead(&s, sizeof(int16_t)) ? s : 0;
  128. }
  129. uint16_t readUShort() noexcept
  130. {
  131. int16_t us = -1;
  132. return (tryRead(&us, sizeof(int16_t)) && us >= 0 && us <= INT_LEAST16_MAX) ? static_cast<uint16_t>(us) : 0;
  133. }
  134. int32_t readInt() noexcept
  135. {
  136. int32_t i = 0;
  137. return tryRead(&i, sizeof(int32_t)) ? i : 0;
  138. }
  139. uint32_t readUInt() noexcept
  140. {
  141. int32_t ui = -1;
  142. return (tryRead(&ui, sizeof(int32_t)) && ui >= 0 && ui <= INT_LEAST32_MAX) ? static_cast<uint32_t>(ui) : 0;
  143. }
  144. int64_t readLong() noexcept
  145. {
  146. int64_t l = 0;
  147. return tryRead(&l, sizeof(int64_t)) ? l : 0;
  148. }
  149. uint64_t readULong() noexcept
  150. {
  151. int64_t ul = -1;
  152. return (tryRead(&ul, sizeof(int64_t)) && ul >= 0 && ul <= INT_LEAST64_MAX) ? static_cast<uint64_t>(ul) : 0;
  153. }
  154. float readFloat() noexcept
  155. {
  156. float f = 0.0f;
  157. return tryRead(&f, sizeof(float)) ? f : 0.0f;
  158. }
  159. double readDouble() noexcept
  160. {
  161. double d = 0.0;
  162. return tryRead(&d, sizeof(double)) ? d : 0.0;
  163. }
  164. void readCustomData(void* const data, const uint32_t size) noexcept
  165. {
  166. if (! tryRead(data, size))
  167. carla_zeroBytes(data, size);
  168. }
  169. template <typename T>
  170. void readCustomType(T& type) noexcept
  171. {
  172. if (! tryRead(&type, sizeof(T)))
  173. carla_zeroStruct(type);
  174. }
  175. // -------------------------------------------------------------------
  176. void writeBool(const bool value) noexcept
  177. {
  178. tryWrite(&value, sizeof(bool));
  179. }
  180. void writeByte(const uint8_t value) noexcept
  181. {
  182. tryWrite(&value, sizeof(uint8_t));
  183. }
  184. void writeShort(const int16_t value) noexcept
  185. {
  186. tryWrite(&value, sizeof(int16_t));
  187. }
  188. void writeInt(const int32_t value) noexcept
  189. {
  190. tryWrite(&value, sizeof(int32_t));
  191. }
  192. void writeLong(const int64_t value) noexcept
  193. {
  194. tryWrite(&value, sizeof(int64_t));
  195. }
  196. void writeFloat(const float value) noexcept
  197. {
  198. tryWrite(&value, sizeof(float));
  199. }
  200. void writeDouble(const double value) noexcept
  201. {
  202. tryWrite(&value, sizeof(double));
  203. }
  204. void writeCustomData(const void* const value, const uint32_t size) noexcept
  205. {
  206. tryWrite(value, size);
  207. }
  208. template <typename T>
  209. void writeCustomType(const T& value) noexcept
  210. {
  211. tryWrite(&value, sizeof(T));
  212. }
  213. // -------------------------------------------------------------------
  214. protected:
  215. void setRingBuffer(BufferStruct* const ringBuf, const bool reset) noexcept
  216. {
  217. CARLA_SAFE_ASSERT_RETURN(ringBuf != fBuffer,);
  218. fBuffer = ringBuf;
  219. if (reset && ringBuf != nullptr)
  220. clear();
  221. }
  222. // -------------------------------------------------------------------
  223. bool tryRead(void* const buf, const uint32_t size) noexcept
  224. {
  225. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  226. CARLA_SAFE_ASSERT_RETURN(buf != nullptr, false);
  227. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  228. CARLA_SAFE_ASSERT_RETURN(size < fBuffer->size, false);
  229. // empty
  230. if (fBuffer->head == fBuffer->tail)
  231. return false;
  232. uint8_t* const bytebuf(static_cast<uint8_t*>(buf));
  233. const uint32_t head(fBuffer->head);
  234. const uint32_t tail(fBuffer->tail);
  235. const uint32_t wrap((head > tail) ? 0 : fBuffer->size);
  236. if (size > wrap + head - tail)
  237. {
  238. carla_stderr2("CarlaRingBuffer::tryRead(%p, " P_SIZE "): failed, not enough space", buf, size);
  239. return false;
  240. }
  241. uint32_t readto(tail + size);
  242. if (readto > fBuffer->size)
  243. {
  244. readto -= fBuffer->size;
  245. const uint32_t firstpart(fBuffer->size - tail);
  246. std::memcpy(bytebuf, fBuffer->buf + tail, firstpart);
  247. std::memcpy(bytebuf + firstpart, fBuffer->buf, readto);
  248. }
  249. else
  250. {
  251. std::memcpy(bytebuf, fBuffer->buf + tail, size);
  252. if (readto == fBuffer->size)
  253. readto = 0;
  254. }
  255. fBuffer->tail = readto;
  256. return true;
  257. }
  258. void tryWrite(const void* const buf, const uint32_t size) noexcept
  259. {
  260. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  261. CARLA_SAFE_ASSERT_RETURN(buf != nullptr,);
  262. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  263. CARLA_SAFE_ASSERT_RETURN(size < fBuffer->size,);
  264. const uint8_t* const bytebuf(static_cast<const uint8_t*>(buf));
  265. const uint32_t tail(fBuffer->tail);
  266. const uint32_t wrtn(fBuffer->wrtn);
  267. const uint32_t wrap((tail > wrtn) ? 0 : fBuffer->size);
  268. if (size >= wrap + tail - wrtn)
  269. {
  270. carla_stderr2("CarlaRingBuffer::tryWrite(%p, " P_SIZE "): failed, not enough space", buf, size);
  271. fBuffer->invalidateCommit = true;
  272. return;
  273. }
  274. uint32_t writeto(wrtn + size);
  275. if (writeto > fBuffer->size)
  276. {
  277. writeto -= fBuffer->size;
  278. const uint32_t firstpart(fBuffer->size - wrtn);
  279. std::memcpy(fBuffer->buf + wrtn, bytebuf, firstpart);
  280. std::memcpy(fBuffer->buf, bytebuf + firstpart, writeto);
  281. }
  282. else
  283. {
  284. std::memcpy(fBuffer->buf + wrtn, bytebuf, size);
  285. if (writeto == fBuffer->size)
  286. writeto = 0;
  287. }
  288. fBuffer->wrtn = writeto;
  289. }
  290. private:
  291. BufferStruct* fBuffer;
  292. CARLA_PREVENT_HEAP_ALLOCATION
  293. CARLA_DECLARE_NON_COPY_CLASS(CarlaRingBuffer)
  294. };
  295. // -----------------------------------------------------------------------
  296. // CarlaRingBuffer using heap space
  297. class CarlaHeapRingBuffer : public CarlaRingBuffer<HeapBuffer>
  298. {
  299. public:
  300. CarlaHeapRingBuffer() noexcept
  301. : CarlaRingBuffer<HeapBuffer>()
  302. {
  303. carla_zeroStruct(fHeapBuffer);
  304. }
  305. ~CarlaHeapRingBuffer() noexcept
  306. {
  307. if (fHeapBuffer.buf == nullptr)
  308. return;
  309. delete[] fHeapBuffer.buf;
  310. fHeapBuffer.buf = nullptr;
  311. }
  312. void createBuffer(const uint32_t size) noexcept
  313. {
  314. CARLA_SAFE_ASSERT_RETURN(fHeapBuffer.buf == nullptr,);
  315. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  316. const uint32_t p2size(carla_nextPowerOf2(size));
  317. try {
  318. fHeapBuffer.buf = new uint8_t[p2size];
  319. } CARLA_SAFE_EXCEPTION_RETURN("CarlaHeapRingBuffer::createBuffer",);
  320. fHeapBuffer.size = p2size;
  321. setRingBuffer(&fHeapBuffer, true);
  322. }
  323. void deleteBuffer() noexcept
  324. {
  325. CARLA_SAFE_ASSERT_RETURN(fHeapBuffer.buf != nullptr,);
  326. setRingBuffer(nullptr, false);
  327. delete[] fHeapBuffer.buf;
  328. fHeapBuffer.buf = nullptr;
  329. fHeapBuffer.size = 0;
  330. }
  331. private:
  332. HeapBuffer fHeapBuffer;
  333. CARLA_PREVENT_HEAP_ALLOCATION
  334. CARLA_DECLARE_NON_COPY_CLASS(CarlaHeapRingBuffer)
  335. };
  336. // -----------------------------------------------------------------------
  337. // CarlaRingBuffer using stack space
  338. class CarlaStackRingBuffer : public CarlaRingBuffer<StackBuffer>
  339. {
  340. public:
  341. CarlaStackRingBuffer() noexcept
  342. : CarlaRingBuffer<StackBuffer>(&fStackBuffer) {}
  343. private:
  344. StackBuffer fStackBuffer;
  345. CARLA_PREVENT_HEAP_ALLOCATION
  346. CARLA_DECLARE_NON_COPY_CLASS(CarlaStackRingBuffer)
  347. };
  348. // -----------------------------------------------------------------------
  349. #endif // CARLA_RING_BUFFER_HPP_INCLUDED