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.

ringbuffer.c 9.6KB

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