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

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