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.

405 lines
9.7KB

  1. /*
  2. Copyright (C) 2000 Paul Davis
  3. Copyright (C) 2003 Rohan Drape
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. ISO/POSIX C version of Paul Davis's lock free ringbuffer C++ code.
  16. This is safe for the case of one read thread and one write thread.
  17. */
  18. #define USE_MLOCK
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef USE_MLOCK
  22. #include <sys/mman.h>
  23. #endif /* USE_MLOCK */
  24. #include "CarlaDefines.h"
  25. typedef struct {
  26. char *buf;
  27. size_t len;
  28. }
  29. jack_ringbuffer_data_t ;
  30. typedef struct {
  31. char *buf;
  32. volatile size_t write_ptr;
  33. volatile size_t read_ptr;
  34. size_t size;
  35. size_t size_mask;
  36. int mlocked;
  37. }
  38. jack_ringbuffer_t ;
  39. CARLA_PLUGIN_EXPORT jack_ringbuffer_t *jack_ringbuffer_create(size_t sz);
  40. CARLA_PLUGIN_EXPORT void jack_ringbuffer_free(jack_ringbuffer_t *rb);
  41. CARLA_PLUGIN_EXPORT void jack_ringbuffer_get_read_vector(const jack_ringbuffer_t *rb, jack_ringbuffer_data_t *vec);
  42. CARLA_PLUGIN_EXPORT void jack_ringbuffer_get_write_vector(const jack_ringbuffer_t *rb, jack_ringbuffer_data_t *vec);
  43. CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  44. CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_peek(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  45. CARLA_PLUGIN_EXPORT void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt);
  46. CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb);
  47. CARLA_PLUGIN_EXPORT int jack_ringbuffer_mlock(jack_ringbuffer_t *rb);
  48. CARLA_PLUGIN_EXPORT void jack_ringbuffer_reset(jack_ringbuffer_t *rb);
  49. CARLA_PLUGIN_EXPORT void jack_ringbuffer_reset_size (jack_ringbuffer_t * rb, size_t sz);
  50. CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src, size_t cnt);
  51. CARLA_PLUGIN_EXPORT void jack_ringbuffer_write_advance(jack_ringbuffer_t *rb, size_t cnt);
  52. CARLA_PLUGIN_EXPORT size_t jack_ringbuffer_write_space(const jack_ringbuffer_t *rb);
  53. /* Create a new ringbuffer to hold at least `sz' bytes of data. The
  54. actual buffer size is rounded up to the next power of two. */
  55. CARLA_PLUGIN_EXPORT jack_ringbuffer_t *
  56. jack_ringbuffer_create (size_t sz)
  57. {
  58. unsigned int power_of_two;
  59. jack_ringbuffer_t *rb;
  60. if ((rb = (jack_ringbuffer_t *) malloc (sizeof (jack_ringbuffer_t))) == NULL) {
  61. return NULL;
  62. }
  63. for (power_of_two = 1U; 1U << power_of_two < sz; power_of_two++);
  64. rb->size = 1U << power_of_two;
  65. rb->size_mask = rb->size;
  66. rb->size_mask -= 1;
  67. rb->write_ptr = 0;
  68. rb->read_ptr = 0;
  69. if ((rb->buf = (char *) malloc (rb->size)) == NULL) {
  70. free (rb);
  71. return NULL;
  72. }
  73. rb->mlocked = 0;
  74. return rb;
  75. }
  76. /* Free all data associated with the ringbuffer `rb'. */
  77. CARLA_PLUGIN_EXPORT void
  78. jack_ringbuffer_free (jack_ringbuffer_t * rb)
  79. {
  80. #ifdef USE_MLOCK
  81. if (rb->mlocked) {
  82. munlock (rb->buf, rb->size);
  83. }
  84. #endif /* USE_MLOCK */
  85. free (rb->buf);
  86. free (rb);
  87. }
  88. /* Lock the data block of `rb' using the system call 'mlock'. */
  89. CARLA_PLUGIN_EXPORT int
  90. jack_ringbuffer_mlock (jack_ringbuffer_t * rb)
  91. {
  92. #ifdef USE_MLOCK
  93. if (mlock (rb->buf, rb->size)) {
  94. return -1;
  95. }
  96. #endif /* USE_MLOCK */
  97. rb->mlocked = 1;
  98. return 0;
  99. }
  100. /* Reset the read and write pointers to zero. This is not thread
  101. safe. */
  102. CARLA_PLUGIN_EXPORT void
  103. jack_ringbuffer_reset (jack_ringbuffer_t * rb)
  104. {
  105. rb->read_ptr = 0;
  106. rb->write_ptr = 0;
  107. memset(rb->buf, 0, rb->size);
  108. }
  109. /* Reset the read and write pointers to zero. This is not thread
  110. safe. */
  111. CARLA_PLUGIN_EXPORT void
  112. jack_ringbuffer_reset_size (jack_ringbuffer_t * rb, size_t sz)
  113. {
  114. rb->size = sz;
  115. rb->size_mask = rb->size;
  116. rb->size_mask -= 1;
  117. rb->read_ptr = 0;
  118. rb->write_ptr = 0;
  119. }
  120. /* Return the number of bytes available for reading. This is the
  121. number of bytes in front of the read pointer and behind the write
  122. pointer. */
  123. CARLA_PLUGIN_EXPORT size_t
  124. jack_ringbuffer_read_space (const jack_ringbuffer_t * rb)
  125. {
  126. size_t w, r;
  127. w = rb->write_ptr;
  128. r = rb->read_ptr;
  129. if (w > r) {
  130. return w - r;
  131. } else {
  132. return (w - r + rb->size) & rb->size_mask;
  133. }
  134. }
  135. /* Return the number of bytes available for writing. This is the
  136. number of bytes in front of the write pointer and behind the read
  137. pointer. */
  138. CARLA_PLUGIN_EXPORT size_t
  139. jack_ringbuffer_write_space (const jack_ringbuffer_t * rb)
  140. {
  141. size_t w, r;
  142. w = rb->write_ptr;
  143. r = rb->read_ptr;
  144. if (w > r) {
  145. return ((r - w + rb->size) & rb->size_mask) - 1;
  146. } else if (w < r) {
  147. return (r - w) - 1;
  148. } else {
  149. return rb->size - 1;
  150. }
  151. }
  152. /* The copying data reader. Copy at most `cnt' bytes from `rb' to
  153. `dest'. Returns the actual number of bytes copied. */
  154. CARLA_PLUGIN_EXPORT size_t
  155. jack_ringbuffer_read (jack_ringbuffer_t * rb, char *dest, size_t cnt)
  156. {
  157. size_t free_cnt;
  158. size_t cnt2;
  159. size_t to_read;
  160. size_t n1, n2;
  161. if ((free_cnt = jack_ringbuffer_read_space (rb)) == 0) {
  162. return 0;
  163. }
  164. to_read = cnt > free_cnt ? free_cnt : cnt;
  165. cnt2 = rb->read_ptr + to_read;
  166. if (cnt2 > rb->size) {
  167. n1 = rb->size - rb->read_ptr;
  168. n2 = cnt2 & rb->size_mask;
  169. } else {
  170. n1 = to_read;
  171. n2 = 0;
  172. }
  173. memcpy (dest, &(rb->buf[rb->read_ptr]), n1);
  174. rb->read_ptr = (rb->read_ptr + n1) & rb->size_mask;
  175. if (n2) {
  176. memcpy (dest + n1, &(rb->buf[rb->read_ptr]), n2);
  177. rb->read_ptr = (rb->read_ptr + n2) & rb->size_mask;
  178. }
  179. return to_read;
  180. }
  181. /* The copying data reader w/o read pointer advance. Copy at most
  182. `cnt' bytes from `rb' to `dest'. Returns the actual number of bytes
  183. copied. */
  184. CARLA_PLUGIN_EXPORT size_t
  185. jack_ringbuffer_peek (jack_ringbuffer_t * rb, char *dest, size_t cnt)
  186. {
  187. size_t free_cnt;
  188. size_t cnt2;
  189. size_t to_read;
  190. size_t n1, n2;
  191. size_t tmp_read_ptr;
  192. tmp_read_ptr = rb->read_ptr;
  193. if ((free_cnt = jack_ringbuffer_read_space (rb)) == 0) {
  194. return 0;
  195. }
  196. to_read = cnt > free_cnt ? free_cnt : cnt;
  197. cnt2 = tmp_read_ptr + to_read;
  198. if (cnt2 > rb->size) {
  199. n1 = rb->size - tmp_read_ptr;
  200. n2 = cnt2 & rb->size_mask;
  201. } else {
  202. n1 = to_read;
  203. n2 = 0;
  204. }
  205. memcpy (dest, &(rb->buf[tmp_read_ptr]), n1);
  206. tmp_read_ptr = (tmp_read_ptr + n1) & rb->size_mask;
  207. if (n2) {
  208. memcpy (dest + n1, &(rb->buf[tmp_read_ptr]), n2);
  209. }
  210. return to_read;
  211. }
  212. /* The copying data writer. Copy at most `cnt' bytes to `rb' from
  213. `src'. Returns the actual number of bytes copied. */
  214. CARLA_PLUGIN_EXPORT size_t
  215. jack_ringbuffer_write (jack_ringbuffer_t * rb, const char *src, size_t cnt)
  216. {
  217. size_t free_cnt;
  218. size_t cnt2;
  219. size_t to_write;
  220. size_t n1, n2;
  221. if ((free_cnt = jack_ringbuffer_write_space (rb)) == 0) {
  222. return 0;
  223. }
  224. to_write = cnt > free_cnt ? free_cnt : cnt;
  225. cnt2 = rb->write_ptr + to_write;
  226. if (cnt2 > rb->size) {
  227. n1 = rb->size - rb->write_ptr;
  228. n2 = cnt2 & rb->size_mask;
  229. } else {
  230. n1 = to_write;
  231. n2 = 0;
  232. }
  233. memcpy (&(rb->buf[rb->write_ptr]), src, n1);
  234. rb->write_ptr = (rb->write_ptr + n1) & rb->size_mask;
  235. if (n2) {
  236. memcpy (&(rb->buf[rb->write_ptr]), src + n1, n2);
  237. rb->write_ptr = (rb->write_ptr + n2) & rb->size_mask;
  238. }
  239. return to_write;
  240. }
  241. /* Advance the read pointer `cnt' places. */
  242. CARLA_PLUGIN_EXPORT void
  243. jack_ringbuffer_read_advance (jack_ringbuffer_t * rb, size_t cnt)
  244. {
  245. size_t tmp = (rb->read_ptr + cnt) & rb->size_mask;
  246. rb->read_ptr = tmp;
  247. }
  248. /* Advance the write pointer `cnt' places. */
  249. CARLA_PLUGIN_EXPORT void
  250. jack_ringbuffer_write_advance (jack_ringbuffer_t * rb, size_t cnt)
  251. {
  252. size_t tmp = (rb->write_ptr + cnt) & rb->size_mask;
  253. rb->write_ptr = tmp;
  254. }
  255. /* The non-copying data reader. `vec' is an array of two places. Set
  256. the values at `vec' to hold the current readable data at `rb'. If
  257. the readable data is in one segment the second segment has zero
  258. length. */
  259. CARLA_PLUGIN_EXPORT void
  260. jack_ringbuffer_get_read_vector (const jack_ringbuffer_t * rb,
  261. jack_ringbuffer_data_t * vec)
  262. {
  263. size_t free_cnt;
  264. size_t cnt2;
  265. size_t w, r;
  266. w = rb->write_ptr;
  267. r = rb->read_ptr;
  268. if (w > r) {
  269. free_cnt = w - r;
  270. } else {
  271. free_cnt = (w - r + rb->size) & rb->size_mask;
  272. }
  273. cnt2 = r + free_cnt;
  274. if (cnt2 > rb->size) {
  275. /* Two part vector: the rest of the buffer after the current write
  276. ptr, plus some from the start of the buffer. */
  277. vec[0].buf = &(rb->buf[r]);
  278. vec[0].len = rb->size - r;
  279. vec[1].buf = rb->buf;
  280. vec[1].len = cnt2 & rb->size_mask;
  281. } else {
  282. /* Single part vector: just the rest of the buffer */
  283. vec[0].buf = &(rb->buf[r]);
  284. vec[0].len = free_cnt;
  285. vec[1].len = 0;
  286. }
  287. }
  288. /* The non-copying data writer. `vec' is an array of two places. Set
  289. the values at `vec' to hold the current writeable data at `rb'. If
  290. the writeable data is in one segment the second segment has zero
  291. length. */
  292. CARLA_PLUGIN_EXPORT void
  293. jack_ringbuffer_get_write_vector (const jack_ringbuffer_t * rb,
  294. jack_ringbuffer_data_t * vec)
  295. {
  296. size_t free_cnt;
  297. size_t cnt2;
  298. size_t w, r;
  299. w = rb->write_ptr;
  300. r = rb->read_ptr;
  301. if (w > r) {
  302. free_cnt = ((r - w + rb->size) & rb->size_mask) - 1;
  303. } else if (w < r) {
  304. free_cnt = (r - w) - 1;
  305. } else {
  306. free_cnt = rb->size - 1;
  307. }
  308. cnt2 = w + free_cnt;
  309. if (cnt2 > rb->size) {
  310. /* Two part vector: the rest of the buffer after the current write
  311. ptr, plus some from the start of the buffer. */
  312. vec[0].buf = &(rb->buf[w]);
  313. vec[0].len = rb->size - w;
  314. vec[1].buf = rb->buf;
  315. vec[1].len = cnt2 & rb->size_mask;
  316. } else {
  317. vec[0].buf = &(rb->buf[w]);
  318. vec[0].len = free_cnt;
  319. vec[1].len = 0;
  320. }
  321. }