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.

CarlaRingBuffer.hpp 13KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 SmallStackBuffer {
  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. struct HugeStackBuffer {
  66. static const uint32_t size = 65536;
  67. uint32_t head, tail, wrtn;
  68. bool invalidateCommit;
  69. uint8_t buf[size];
  70. };
  71. #ifdef CARLA_PROPER_CPP11_SUPPORT
  72. # define HeapBuffer_INIT {0, 0, 0, 0, false, nullptr}
  73. # define StackBuffer_INIT {0, 0, 0, false, {0}}
  74. #else
  75. # define HeapBuffer_INIT
  76. # define StackBuffer_INIT
  77. #endif
  78. // -----------------------------------------------------------------------
  79. // CarlaRingBufferControl templated class
  80. template <class BufferStruct>
  81. class CarlaRingBufferControl
  82. {
  83. public:
  84. CarlaRingBufferControl() noexcept
  85. : fBuffer(nullptr),
  86. fErrorReading(false),
  87. fErrorWriting(false) {}
  88. virtual ~CarlaRingBufferControl() noexcept {}
  89. // -------------------------------------------------------------------
  90. void clearData() noexcept
  91. {
  92. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  93. fBuffer->head = 0;
  94. fBuffer->tail = 0;
  95. fBuffer->wrtn = 0;
  96. fBuffer->invalidateCommit = false;
  97. carla_zeroBytes(fBuffer->buf, fBuffer->size);
  98. }
  99. // -------------------------------------------------------------------
  100. bool commitWrite() noexcept
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  103. if (fBuffer->invalidateCommit)
  104. {
  105. fBuffer->wrtn = fBuffer->head;
  106. fBuffer->invalidateCommit = false;
  107. return false;
  108. }
  109. // nothing to commit?
  110. CARLA_SAFE_ASSERT_RETURN(fBuffer->head != fBuffer->wrtn, false);
  111. // all ok
  112. fBuffer->head = fBuffer->wrtn;
  113. fErrorWriting = false;
  114. return true;
  115. }
  116. bool isDataAvailableForReading() const noexcept;
  117. bool isEmpty() const noexcept
  118. {
  119. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  120. return (fBuffer->buf == nullptr || fBuffer->head == fBuffer->tail);
  121. }
  122. uint32_t getAvailableDataSize() const noexcept
  123. {
  124. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, 0);
  125. const uint32_t wrap((fBuffer->tail > fBuffer->wrtn) ? 0 : fBuffer->size);
  126. return wrap + fBuffer->tail - fBuffer->wrtn;
  127. }
  128. // -------------------------------------------------------------------
  129. bool readBool() noexcept
  130. {
  131. bool b = false;
  132. return tryRead(&b, sizeof(bool)) ? b : false;
  133. }
  134. uint8_t readByte() noexcept
  135. {
  136. uint8_t B = 0;
  137. return tryRead(&B, sizeof(uint8_t)) ? B : 0;
  138. }
  139. int16_t readShort() noexcept
  140. {
  141. int16_t s = 0;
  142. return tryRead(&s, sizeof(int16_t)) ? s : 0;
  143. }
  144. uint16_t readUShort() noexcept
  145. {
  146. uint16_t us = 0;
  147. return tryRead(&us, sizeof(uint16_t)) ? us : 0;
  148. }
  149. int32_t readInt() noexcept
  150. {
  151. int32_t i = 0;
  152. return tryRead(&i, sizeof(int32_t)) ? i : 0;
  153. }
  154. uint32_t readUInt() noexcept
  155. {
  156. uint32_t ui = 0;
  157. return tryRead(&ui, sizeof(int32_t)) ? ui : 0;
  158. }
  159. int64_t readLong() noexcept
  160. {
  161. int64_t l = 0;
  162. return tryRead(&l, sizeof(int64_t)) ? l : 0;
  163. }
  164. uint64_t readULong() noexcept
  165. {
  166. uint64_t ul = 0;
  167. return tryRead(&ul, sizeof(int64_t)) ? ul : 0;
  168. }
  169. float readFloat() noexcept
  170. {
  171. float f = 0.0f;
  172. return tryRead(&f, sizeof(float)) ? f : 0.0f;
  173. }
  174. double readDouble() noexcept
  175. {
  176. double d = 0.0;
  177. return tryRead(&d, sizeof(double)) ? d : 0.0;
  178. }
  179. void readCustomData(void* const data, const uint32_t size) noexcept
  180. {
  181. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  182. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  183. if (! tryRead(data, size))
  184. std::memset(data, 0, size);
  185. }
  186. template <typename T>
  187. void readCustomType(T& type) noexcept
  188. {
  189. if (! tryRead(&type, sizeof(T)))
  190. std::memset(&type, 0, sizeof(T));
  191. }
  192. // -------------------------------------------------------------------
  193. void writeBool(const bool value) noexcept
  194. {
  195. tryWrite(&value, sizeof(bool));
  196. }
  197. void writeByte(const uint8_t value) noexcept
  198. {
  199. tryWrite(&value, sizeof(uint8_t));
  200. }
  201. void writeShort(const int16_t value) noexcept
  202. {
  203. tryWrite(&value, sizeof(int16_t));
  204. }
  205. void writeUShort(const uint16_t value) noexcept
  206. {
  207. tryWrite(&value, sizeof(uint16_t));
  208. }
  209. void writeInt(const int32_t value) noexcept
  210. {
  211. tryWrite(&value, sizeof(int32_t));
  212. }
  213. void writeUInt(const uint32_t value) noexcept
  214. {
  215. tryWrite(&value, sizeof(uint32_t));
  216. }
  217. void writeLong(const int64_t value) noexcept
  218. {
  219. tryWrite(&value, sizeof(int64_t));
  220. }
  221. void writeULong(const uint64_t value) noexcept
  222. {
  223. tryWrite(&value, sizeof(uint64_t));
  224. }
  225. void writeFloat(const float value) noexcept
  226. {
  227. tryWrite(&value, sizeof(float));
  228. }
  229. void writeDouble(const double value) noexcept
  230. {
  231. tryWrite(&value, sizeof(double));
  232. }
  233. void writeCustomData(const void* const data, const uint32_t size) noexcept
  234. {
  235. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  236. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  237. tryWrite(data, size);
  238. }
  239. template <typename T>
  240. void writeCustomType(const T& type) noexcept
  241. {
  242. tryWrite(&type, sizeof(T));
  243. }
  244. // -------------------------------------------------------------------
  245. protected:
  246. void setRingBuffer(BufferStruct* const ringBuf, const bool resetBuffer) noexcept
  247. {
  248. CARLA_SAFE_ASSERT_RETURN(fBuffer != ringBuf,);
  249. fBuffer = ringBuf;
  250. if (resetBuffer && ringBuf != nullptr)
  251. clearData();
  252. }
  253. // -------------------------------------------------------------------
  254. bool tryRead(void* const buf, const uint32_t size) noexcept
  255. {
  256. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  257. CARLA_SAFE_ASSERT_RETURN(buf != nullptr, false);
  258. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  259. CARLA_SAFE_ASSERT_RETURN(size < fBuffer->size, false);
  260. // empty
  261. if (fBuffer->head == fBuffer->tail)
  262. return false;
  263. uint8_t* const bytebuf(static_cast<uint8_t*>(buf));
  264. const uint32_t head(fBuffer->head);
  265. const uint32_t tail(fBuffer->tail);
  266. const uint32_t wrap((head > tail) ? 0 : fBuffer->size);
  267. if (size > wrap + head - tail)
  268. {
  269. if (! fErrorReading)
  270. {
  271. fErrorReading = true;
  272. carla_stderr2("CarlaRingBuffer::tryRead(%p, " P_SIZE "): failed, not enough space", buf, size);
  273. }
  274. return false;
  275. }
  276. uint32_t readto(tail + size);
  277. if (readto > fBuffer->size)
  278. {
  279. readto -= fBuffer->size;
  280. const uint32_t firstpart(fBuffer->size - tail);
  281. std::memcpy(bytebuf, fBuffer->buf + tail, firstpart);
  282. std::memcpy(bytebuf + firstpart, fBuffer->buf, readto);
  283. }
  284. else
  285. {
  286. std::memcpy(bytebuf, fBuffer->buf + tail, size);
  287. if (readto == fBuffer->size)
  288. readto = 0;
  289. }
  290. fBuffer->tail = readto;
  291. fErrorReading = false;
  292. return true;
  293. }
  294. bool tryWrite(const void* const buf, const uint32_t size) noexcept
  295. {
  296. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  297. CARLA_SAFE_ASSERT_RETURN(buf != nullptr, false);
  298. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  299. CARLA_SAFE_ASSERT_RETURN(size < fBuffer->size, false);
  300. const uint8_t* const bytebuf(static_cast<const uint8_t*>(buf));
  301. const uint32_t tail(fBuffer->tail);
  302. const uint32_t wrtn(fBuffer->wrtn);
  303. const uint32_t wrap((tail > wrtn) ? 0 : fBuffer->size);
  304. if (size >= wrap + tail - wrtn)
  305. {
  306. if (! fErrorWriting)
  307. {
  308. fErrorWriting = true;
  309. carla_stderr2("CarlaRingBuffer::tryWrite(%p, " P_SIZE "): failed, not enough space", buf, size);
  310. }
  311. fBuffer->invalidateCommit = true;
  312. return false;
  313. }
  314. uint32_t writeto(wrtn + size);
  315. if (writeto > fBuffer->size)
  316. {
  317. writeto -= fBuffer->size;
  318. const uint32_t firstpart(fBuffer->size - wrtn);
  319. std::memcpy(fBuffer->buf + wrtn, bytebuf, firstpart);
  320. std::memcpy(fBuffer->buf, bytebuf + firstpart, writeto);
  321. }
  322. else
  323. {
  324. std::memcpy(fBuffer->buf + wrtn, bytebuf, size);
  325. if (writeto == fBuffer->size)
  326. writeto = 0;
  327. }
  328. fBuffer->wrtn = writeto;
  329. return true;
  330. }
  331. private:
  332. BufferStruct* fBuffer;
  333. // wherever read/write errors have been printed to terminal
  334. bool fErrorReading;
  335. bool fErrorWriting;
  336. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  337. CARLA_DECLARE_NON_COPY_CLASS(CarlaRingBufferControl)
  338. };
  339. template <class BufferStruct>
  340. inline bool CarlaRingBufferControl<BufferStruct>::isDataAvailableForReading() const noexcept
  341. {
  342. return (fBuffer != nullptr && fBuffer->head != fBuffer->tail);
  343. }
  344. template <>
  345. inline bool CarlaRingBufferControl<HeapBuffer>::isDataAvailableForReading() const noexcept
  346. {
  347. return (fBuffer != nullptr && fBuffer->buf != nullptr && fBuffer->head != fBuffer->tail);
  348. }
  349. // -----------------------------------------------------------------------
  350. // CarlaRingBuffer using heap space
  351. class CarlaHeapRingBuffer : public CarlaRingBufferControl<HeapBuffer>
  352. {
  353. public:
  354. CarlaHeapRingBuffer() noexcept
  355. : fHeapBuffer(HeapBuffer_INIT)
  356. {
  357. carla_zeroStruct(fHeapBuffer);
  358. }
  359. ~CarlaHeapRingBuffer() noexcept override
  360. {
  361. if (fHeapBuffer.buf == nullptr)
  362. return;
  363. delete[] fHeapBuffer.buf;
  364. fHeapBuffer.buf = nullptr;
  365. }
  366. void createBuffer(const uint32_t size) noexcept
  367. {
  368. CARLA_SAFE_ASSERT_RETURN(fHeapBuffer.buf == nullptr,);
  369. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  370. const uint32_t p2size(carla_nextPowerOf2(size));
  371. try {
  372. fHeapBuffer.buf = new uint8_t[p2size];
  373. } CARLA_SAFE_EXCEPTION_RETURN("CarlaHeapRingBuffer::createBuffer",);
  374. fHeapBuffer.size = p2size;
  375. setRingBuffer(&fHeapBuffer, true);
  376. }
  377. void deleteBuffer() noexcept
  378. {
  379. CARLA_SAFE_ASSERT_RETURN(fHeapBuffer.buf != nullptr,);
  380. setRingBuffer(nullptr, false);
  381. delete[] fHeapBuffer.buf;
  382. fHeapBuffer.buf = nullptr;
  383. fHeapBuffer.size = 0;
  384. }
  385. private:
  386. HeapBuffer fHeapBuffer;
  387. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  388. CARLA_DECLARE_NON_COPY_CLASS(CarlaHeapRingBuffer)
  389. };
  390. // -----------------------------------------------------------------------
  391. // CarlaRingBuffer using small stack space
  392. class CarlaSmallStackRingBuffer : public CarlaRingBufferControl<SmallStackBuffer>
  393. {
  394. public:
  395. CarlaSmallStackRingBuffer() noexcept
  396. : fStackBuffer(StackBuffer_INIT)
  397. {
  398. setRingBuffer(&fStackBuffer, true);
  399. }
  400. private:
  401. SmallStackBuffer fStackBuffer;
  402. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  403. CARLA_DECLARE_NON_COPY_CLASS(CarlaSmallStackRingBuffer)
  404. };
  405. // -----------------------------------------------------------------------
  406. #endif // CARLA_RING_BUFFER_HPP_INCLUDED