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.

437 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. std::size_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. size = rb.size;
  47. head = rb.head;
  48. tail = rb.tail;
  49. wrtn = rb.wrtn;
  50. invalidateCommit = rb.invalidateCommit;
  51. std::memcpy(buf, rb.buf, size);
  52. }
  53. };
  54. struct StackBuffer {
  55. static const uint32_t size = 4096;
  56. std::size_t head, tail, wrtn;
  57. bool invalidateCommit;
  58. uint8_t buf[size];
  59. };
  60. // -----------------------------------------------------------------------
  61. // CarlaRingBuffer templated class
  62. template <class BufferStruct>
  63. class CarlaRingBuffer
  64. {
  65. public:
  66. CarlaRingBuffer() noexcept
  67. : fBuffer(nullptr) {}
  68. CarlaRingBuffer(BufferStruct* const ringBuf) noexcept
  69. : fBuffer(ringBuf)
  70. {
  71. if (ringBuf != nullptr)
  72. clear();
  73. }
  74. void clear() noexcept
  75. {
  76. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  77. fBuffer->head = 0;
  78. fBuffer->tail = 0;
  79. fBuffer->wrtn = 0;
  80. fBuffer->invalidateCommit = false;
  81. carla_zeroBytes(fBuffer->buf, fBuffer->size);
  82. }
  83. // -------------------------------------------------------------------
  84. bool commitWrite() noexcept
  85. {
  86. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  87. if (fBuffer->invalidateCommit)
  88. {
  89. fBuffer->wrtn = fBuffer->head;
  90. fBuffer->invalidateCommit = false;
  91. return false;
  92. }
  93. // nothing to commit?
  94. CARLA_SAFE_ASSERT_RETURN(fBuffer->head != fBuffer->wrtn, false);
  95. // all ok
  96. fBuffer->head = fBuffer->wrtn;
  97. return true;
  98. }
  99. bool isDataAvailableForReading() const noexcept
  100. {
  101. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  102. return (fBuffer->head != fBuffer->tail);
  103. }
  104. bool isEmpty() const noexcept
  105. {
  106. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  107. return (fBuffer->head == fBuffer->tail);
  108. }
  109. // -------------------------------------------------------------------
  110. bool readBool() noexcept
  111. {
  112. bool b = false;
  113. return tryRead(&b, sizeof(bool)) ? b : false;
  114. }
  115. int8_t readByte() noexcept
  116. {
  117. int8_t b = 0;
  118. return tryRead(&b, sizeof(int8_t)) ? b : 0;
  119. }
  120. uint8_t readUByte() noexcept
  121. {
  122. int8_t ub = -1;
  123. return (tryRead(&ub, sizeof(int8_t)) && ub >= 0 && ub <= INT_LEAST8_MAX) ? static_cast<uint8_t>(ub) : 0;
  124. }
  125. int16_t readShort() noexcept
  126. {
  127. int16_t s = 0;
  128. return tryRead(&s, sizeof(int16_t)) ? s : 0;
  129. }
  130. uint16_t readUShort() noexcept
  131. {
  132. int16_t us = -1;
  133. return (tryRead(&us, sizeof(int16_t)) && us >= 0 && us <= INT_LEAST16_MAX) ? static_cast<uint16_t>(us) : 0;
  134. }
  135. int32_t readInt() noexcept
  136. {
  137. int32_t i = 0;
  138. return tryRead(&i, sizeof(int32_t)) ? i : 0;
  139. }
  140. uint32_t readUInt() noexcept
  141. {
  142. int32_t ui = -1;
  143. return (tryRead(&ui, sizeof(int32_t)) && ui >= 0 && ui <= INT_LEAST32_MAX) ? static_cast<uint32_t>(ui) : 0;
  144. }
  145. int64_t readLong() noexcept
  146. {
  147. int64_t l = 0;
  148. return tryRead(&l, sizeof(int64_t)) ? l : 0;
  149. }
  150. uint64_t readULong() noexcept
  151. {
  152. int64_t ul = -1;
  153. return (tryRead(&ul, sizeof(int64_t)) && ul >= 0 && ul <= INT_LEAST64_MAX) ? static_cast<uint64_t>(ul) : 0;
  154. }
  155. float readFloat() noexcept
  156. {
  157. float f = 0.0f;
  158. return tryRead(&f, sizeof(float)) ? f : 0.0f;
  159. }
  160. double readDouble() noexcept
  161. {
  162. double d = 0.0;
  163. return tryRead(&d, sizeof(double)) ? d : 0.0;
  164. }
  165. void readCustomData(void* const data, const std::size_t size) noexcept
  166. {
  167. if (! tryRead(data, size))
  168. carla_zeroBytes(data, size);
  169. }
  170. template <typename T>
  171. void readCustomType(T& type) noexcept
  172. {
  173. if (! tryRead(&type, sizeof(T)))
  174. carla_zeroStruct(type);
  175. }
  176. // -------------------------------------------------------------------
  177. bool writeBool(const bool value) noexcept
  178. {
  179. return tryWrite(&value, sizeof(bool));
  180. }
  181. bool writeByte(const int8_t value) noexcept
  182. {
  183. return tryWrite(&value, sizeof(int8_t));
  184. }
  185. bool writeShort(const int16_t value) noexcept
  186. {
  187. return tryWrite(&value, sizeof(int16_t));
  188. }
  189. bool writeInt(const int32_t value) noexcept
  190. {
  191. return tryWrite(&value, sizeof(int32_t));
  192. }
  193. bool writeLong(const int64_t value) noexcept
  194. {
  195. return tryWrite(&value, sizeof(int64_t));
  196. }
  197. bool writeFloat(const float value) noexcept
  198. {
  199. return tryWrite(&value, sizeof(float));
  200. }
  201. bool writeDouble(const double value) noexcept
  202. {
  203. return tryWrite(&value, sizeof(double));
  204. }
  205. bool writeCustomData(const void* const value, const std::size_t size) noexcept
  206. {
  207. return tryWrite(value, size);
  208. }
  209. template <typename T>
  210. bool writeCustomType(const T& value) noexcept
  211. {
  212. return tryWrite(&value, sizeof(T));
  213. }
  214. // -------------------------------------------------------------------
  215. protected:
  216. void setRingBuffer(BufferStruct* const ringBuf, const bool reset) noexcept
  217. {
  218. CARLA_SAFE_ASSERT_RETURN(ringBuf != fBuffer,);
  219. fBuffer = ringBuf;
  220. if (reset && ringBuf != nullptr)
  221. clear();
  222. }
  223. // -------------------------------------------------------------------
  224. private:
  225. BufferStruct* fBuffer;
  226. bool tryRead(void* const buf, const std::size_t size) noexcept
  227. {
  228. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  229. CARLA_SAFE_ASSERT_RETURN(buf != nullptr, false);
  230. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  231. CARLA_SAFE_ASSERT_RETURN(size < fBuffer->size, false);
  232. // empty
  233. if (fBuffer->head == fBuffer->tail)
  234. return false;
  235. uint8_t* const bytebuf(static_cast<uint8_t*>(buf));
  236. const std::size_t head(fBuffer->head);
  237. const std::size_t tail(fBuffer->tail);
  238. const std::size_t wrap((head > tail) ? 0 : fBuffer->size);
  239. if (size > wrap + head - tail)
  240. {
  241. carla_stderr2("CarlaRingBuffer::tryRead(%p, " P_SIZE "): failed, not enough space", buf, size);
  242. return false;
  243. }
  244. std::size_t readto(tail + size);
  245. if (readto > fBuffer->size)
  246. {
  247. readto -= fBuffer->size;
  248. const std::size_t firstpart(fBuffer->size - tail);
  249. std::memcpy(bytebuf, fBuffer->buf + tail, firstpart);
  250. std::memcpy(bytebuf + firstpart, fBuffer->buf, readto);
  251. }
  252. else
  253. {
  254. std::memcpy(bytebuf, fBuffer->buf + tail, size);
  255. if (readto == fBuffer->size)
  256. readto = 0;
  257. }
  258. fBuffer->tail = readto;
  259. return true;
  260. }
  261. bool tryWrite(const void* const buf, const std::size_t size) noexcept
  262. {
  263. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  264. CARLA_SAFE_ASSERT_RETURN(buf != nullptr, false);
  265. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  266. CARLA_SAFE_ASSERT_RETURN(size < fBuffer->size, false);
  267. const uint8_t* const bytebuf(static_cast<const uint8_t*>(buf));
  268. const std::size_t tail(fBuffer->tail);
  269. const std::size_t wrtn(fBuffer->wrtn);
  270. const std::size_t wrap((tail > wrtn) ? 0 : fBuffer->size);
  271. if (size >= wrap + tail - wrtn)
  272. {
  273. carla_stderr2("CarlaRingBuffer::tryWrite(%p, " P_SIZE "): failed, not enough space", buf, size);
  274. fBuffer->invalidateCommit = true;
  275. return false;
  276. }
  277. std::size_t writeto(wrtn + size);
  278. if (writeto > fBuffer->size)
  279. {
  280. writeto -= fBuffer->size;
  281. const std::size_t firstpart(fBuffer->size - wrtn);
  282. std::memcpy(fBuffer->buf + wrtn, bytebuf, firstpart);
  283. std::memcpy(fBuffer->buf, bytebuf + firstpart, writeto);
  284. }
  285. else
  286. {
  287. std::memcpy(fBuffer->buf + wrtn, bytebuf, size);
  288. if (writeto == fBuffer->size)
  289. writeto = 0;
  290. }
  291. fBuffer->wrtn = writeto;
  292. return true;
  293. }
  294. CARLA_PREVENT_HEAP_ALLOCATION
  295. CARLA_DECLARE_NON_COPY_CLASS(CarlaRingBuffer)
  296. };
  297. // -----------------------------------------------------------------------
  298. // CarlaRingBuffer using heap space
  299. class CarlaHeapRingBuffer : public CarlaRingBuffer<HeapBuffer>
  300. {
  301. public:
  302. CarlaHeapRingBuffer() noexcept
  303. : CarlaRingBuffer<HeapBuffer>() {}
  304. ~CarlaHeapRingBuffer() noexcept
  305. {
  306. if (fHeapBuffer.buf == nullptr)
  307. return;
  308. delete[] fHeapBuffer.buf;
  309. fHeapBuffer.buf = nullptr;
  310. }
  311. void createBuffer(const uint32_t size)
  312. {
  313. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  314. fHeapBuffer.size = carla_nextPowerOf2(size);
  315. fHeapBuffer.buf = new uint8_t[fHeapBuffer.size];
  316. setRingBuffer(&fHeapBuffer, true);
  317. }
  318. void deleteBuffer() noexcept
  319. {
  320. CARLA_SAFE_ASSERT_RETURN(fHeapBuffer.buf != nullptr,);
  321. setRingBuffer(nullptr, false);
  322. delete[] fHeapBuffer.buf;
  323. fHeapBuffer.buf = nullptr;
  324. fHeapBuffer.size = 0;
  325. }
  326. private:
  327. HeapBuffer fHeapBuffer;
  328. CARLA_PREVENT_HEAP_ALLOCATION
  329. CARLA_DECLARE_NON_COPY_CLASS(CarlaHeapRingBuffer)
  330. };
  331. // -----------------------------------------------------------------------
  332. // CarlaRingBuffer using stack space
  333. class CarlaStackRingBuffer : public CarlaRingBuffer<StackBuffer>
  334. {
  335. public:
  336. CarlaStackRingBuffer() noexcept
  337. : CarlaRingBuffer<StackBuffer>(&fStackBuffer) {}
  338. private:
  339. StackBuffer fStackBuffer;
  340. CARLA_PREVENT_HEAP_ALLOCATION
  341. CARLA_DECLARE_NON_COPY_CLASS(CarlaStackRingBuffer)
  342. };
  343. // -----------------------------------------------------------------------
  344. #endif // CARLA_RING_BUFFER_HPP_INCLUDED