alcRing.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "alMain.h"
  24. #include "threads.h"
  25. #include "compat.h"
  26. struct RingBuffer {
  27. ALubyte *mem;
  28. ALsizei frame_size;
  29. ALsizei length;
  30. ALint read_pos;
  31. ALint write_pos;
  32. almtx_t mtx;
  33. };
  34. RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
  35. {
  36. RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size));
  37. if(ring)
  38. {
  39. ring->mem = (ALubyte*)(ring+1);
  40. ring->frame_size = frame_size;
  41. ring->length = length+1;
  42. ring->read_pos = 0;
  43. ring->write_pos = 0;
  44. almtx_init(&ring->mtx, almtx_plain);
  45. }
  46. return ring;
  47. }
  48. void DestroyRingBuffer(RingBuffer *ring)
  49. {
  50. if(ring)
  51. {
  52. almtx_destroy(&ring->mtx);
  53. free(ring);
  54. }
  55. }
  56. ALsizei RingBufferSize(RingBuffer *ring)
  57. {
  58. ALsizei s;
  59. almtx_lock(&ring->mtx);
  60. s = (ring->write_pos-ring->read_pos+ring->length) % ring->length;
  61. almtx_unlock(&ring->mtx);
  62. return s;
  63. }
  64. void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
  65. {
  66. int remain;
  67. almtx_lock(&ring->mtx);
  68. remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length;
  69. if(remain < len) len = remain;
  70. if(len > 0)
  71. {
  72. remain = ring->length - ring->write_pos;
  73. if(remain < len)
  74. {
  75. memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
  76. remain*ring->frame_size);
  77. memcpy(ring->mem, data+(remain*ring->frame_size),
  78. (len-remain)*ring->frame_size);
  79. }
  80. else
  81. memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
  82. len*ring->frame_size);
  83. ring->write_pos += len;
  84. ring->write_pos %= ring->length;
  85. }
  86. almtx_unlock(&ring->mtx);
  87. }
  88. void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len)
  89. {
  90. int remain;
  91. almtx_lock(&ring->mtx);
  92. remain = ring->length - ring->read_pos;
  93. if(remain < len)
  94. {
  95. memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size);
  96. memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size);
  97. }
  98. else
  99. memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size);
  100. ring->read_pos += len;
  101. ring->read_pos %= ring->length;
  102. almtx_unlock(&ring->mtx);
  103. }
  104. /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
  105. * to include an element size. Consequently, parameters and return values for a
  106. * size or count is in 'elements', not bytes. Additionally, it only supports
  107. * single-consumer/single-provider operation. */
  108. struct ll_ringbuffer {
  109. volatile size_t write_ptr;
  110. volatile size_t read_ptr;
  111. size_t size;
  112. size_t size_mask;
  113. size_t elem_size;
  114. int mlocked;
  115. alignas(16) char buf[];
  116. };
  117. /* Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes.
  118. * The number of elements is rounded up to the next power of two. */
  119. ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz)
  120. {
  121. ll_ringbuffer_t *rb;
  122. ALuint power_of_two;
  123. power_of_two = NextPowerOf2(sz);
  124. if(power_of_two < sz)
  125. return NULL;
  126. rb = al_malloc(16, sizeof(*rb) + power_of_two*elem_sz);
  127. if(!rb) return NULL;
  128. rb->size = power_of_two;
  129. rb->size_mask = rb->size - 1;
  130. rb->elem_size = elem_sz;
  131. rb->write_ptr = 0;
  132. rb->read_ptr = 0;
  133. rb->mlocked = 0;
  134. return rb;
  135. }
  136. /* Free all data associated with the ringbuffer `rb'. */
  137. void ll_ringbuffer_free(ll_ringbuffer_t *rb)
  138. {
  139. if(rb)
  140. {
  141. #ifdef USE_MLOCK
  142. if(rb->mlocked)
  143. munlock(rb, sizeof(*rb) + rb->size*rb->elem_size);
  144. #endif /* USE_MLOCK */
  145. al_free(rb);
  146. }
  147. }
  148. /* Lock the data block of `rb' using the system call 'mlock'. */
  149. int ll_ringbuffer_mlock(ll_ringbuffer_t *rb)
  150. {
  151. #ifdef USE_MLOCK
  152. if(!rb->locked && mlock(rb, sizeof(*rb) + rb->size*rb->elem_size))
  153. return -1;
  154. #endif /* USE_MLOCK */
  155. rb->mlocked = 1;
  156. return 0;
  157. }
  158. /* Reset the read and write pointers to zero. This is not thread safe. */
  159. void ll_ringbuffer_reset(ll_ringbuffer_t *rb)
  160. {
  161. rb->read_ptr = 0;
  162. rb->write_ptr = 0;
  163. memset(rb->buf, 0, rb->size*rb->elem_size);
  164. }
  165. /* Return the number of elements available for reading. This is the number of
  166. * elements in front of the read pointer and behind the write pointer. */
  167. size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb)
  168. {
  169. size_t w = rb->write_ptr;
  170. size_t r = rb->read_ptr;
  171. return (rb->size+w-r) & rb->size_mask;
  172. }
  173. /* Return the number of elements available for writing. This is the number of
  174. * elements in front of the write pointer and behind the read pointer. */
  175. size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb)
  176. {
  177. size_t w = rb->write_ptr;
  178. size_t r = rb->read_ptr;
  179. return (rb->size+r-w-1) & rb->size_mask;
  180. }
  181. /* The copying data reader. Copy at most `cnt' elements from `rb' to `dest'.
  182. * Returns the actual number of elements copied. */
  183. size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt)
  184. {
  185. size_t free_cnt;
  186. size_t cnt2;
  187. size_t to_read;
  188. size_t n1, n2;
  189. free_cnt = ll_ringbuffer_read_space(rb);
  190. if(free_cnt == 0) return 0;
  191. to_read = (cnt > free_cnt) ? free_cnt : cnt;
  192. cnt2 = rb->read_ptr + to_read;
  193. if(cnt2 > rb->size)
  194. {
  195. n1 = rb->size - rb->read_ptr;
  196. n2 = cnt2 & rb->size_mask;
  197. }
  198. else
  199. {
  200. n1 = to_read;
  201. n2 = 0;
  202. }
  203. memcpy(dest, &(rb->buf[rb->read_ptr*rb->elem_size]), n1*rb->elem_size);
  204. rb->read_ptr = (rb->read_ptr + n1) & rb->size_mask;
  205. if(n2)
  206. {
  207. memcpy(dest + n1*rb->elem_size, &(rb->buf[rb->read_ptr*rb->elem_size]), n2*rb->elem_size);
  208. rb->read_ptr = (rb->read_ptr + n2) & rb->size_mask;
  209. }
  210. return to_read;
  211. }
  212. /* The copying data reader w/o read pointer advance. Copy at most `cnt'
  213. * elements from `rb' to `dest'. Returns the actual number of elements copied.
  214. */
  215. size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt)
  216. {
  217. size_t free_cnt;
  218. size_t cnt2;
  219. size_t to_read;
  220. size_t n1, n2;
  221. size_t tmp_read_ptr;
  222. tmp_read_ptr = rb->read_ptr;
  223. free_cnt = ll_ringbuffer_read_space(rb);
  224. if(free_cnt == 0) return 0;
  225. to_read = (cnt > free_cnt) ? free_cnt : cnt;
  226. cnt2 = tmp_read_ptr + to_read;
  227. if(cnt2 > rb->size)
  228. {
  229. n1 = rb->size - tmp_read_ptr;
  230. n2 = cnt2 & rb->size_mask;
  231. }
  232. else
  233. {
  234. n1 = to_read;
  235. n2 = 0;
  236. }
  237. memcpy(dest, &(rb->buf[tmp_read_ptr*rb->elem_size]), n1*rb->elem_size);
  238. tmp_read_ptr = (tmp_read_ptr + n1) & rb->size_mask;
  239. if(n2)
  240. memcpy(dest + n1*rb->elem_size, &(rb->buf[tmp_read_ptr*rb->elem_size]), n2*rb->elem_size);
  241. return to_read;
  242. }
  243. /* The copying data writer. Copy at most `cnt' elements to `rb' from `src'.
  244. * Returns the actual number of elements copied. */
  245. size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt)
  246. {
  247. size_t free_cnt;
  248. size_t cnt2;
  249. size_t to_write;
  250. size_t n1, n2;
  251. free_cnt = ll_ringbuffer_write_space(rb);
  252. if(free_cnt == 0) return 0;
  253. to_write = (cnt > free_cnt) ? free_cnt : cnt;
  254. cnt2 = rb->write_ptr + to_write;
  255. if(cnt2 > rb->size)
  256. {
  257. n1 = rb->size - rb->write_ptr;
  258. n2 = cnt2 & rb->size_mask;
  259. }
  260. else
  261. {
  262. n1 = to_write;
  263. n2 = 0;
  264. }
  265. memcpy(&(rb->buf[rb->write_ptr*rb->elem_size]), src, n1*rb->elem_size);
  266. rb->write_ptr = (rb->write_ptr + n1) & rb->size_mask;
  267. if(n2)
  268. {
  269. memcpy(&(rb->buf[rb->write_ptr*rb->elem_size]), src + n1*rb->elem_size, n2*rb->elem_size);
  270. rb->write_ptr = (rb->write_ptr + n2) & rb->size_mask;
  271. }
  272. return to_write;
  273. }
  274. /* Advance the read pointer `cnt' places. */
  275. void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt)
  276. {
  277. size_t tmp = (rb->read_ptr + cnt) & rb->size_mask;
  278. rb->read_ptr = tmp;
  279. }
  280. /* Advance the write pointer `cnt' places. */
  281. void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt)
  282. {
  283. size_t tmp = (rb->write_ptr + cnt) & rb->size_mask;
  284. rb->write_ptr = tmp;
  285. }
  286. /* The non-copying data reader. `vec' is an array of two places. Set the values
  287. * at `vec' to hold the current readable data at `rb'. If the readable data is
  288. * in one segment the second segment has zero length. */
  289. void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t * vec)
  290. {
  291. size_t free_cnt;
  292. size_t cnt2;
  293. size_t w, r;
  294. w = rb->write_ptr;
  295. r = rb->read_ptr;
  296. free_cnt = (rb->size+w-r) & rb->size_mask;
  297. cnt2 = r + free_cnt;
  298. if(cnt2 > rb->size)
  299. {
  300. /* Two part vector: the rest of the buffer after the current write ptr,
  301. * plus some from the start of the buffer. */
  302. vec[0].buf = (char*)&(rb->buf[r*rb->elem_size]);
  303. vec[0].len = rb->size - r;
  304. vec[1].buf = (char*)rb->buf;
  305. vec[1].len = cnt2 & rb->size_mask;
  306. }
  307. else
  308. {
  309. /* Single part vector: just the rest of the buffer */
  310. vec[0].buf = (char*)&(rb->buf[r*rb->elem_size]);
  311. vec[0].len = free_cnt;
  312. vec[1].buf = NULL;
  313. vec[1].len = 0;
  314. }
  315. }
  316. /* The non-copying data writer. `vec' is an array of two places. Set the values
  317. * at `vec' to hold the current writeable data at `rb'. If the writeable data
  318. * is in one segment the second segment has zero length. */
  319. void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec)
  320. {
  321. size_t free_cnt;
  322. size_t cnt2;
  323. size_t w, r;
  324. w = rb->write_ptr;
  325. r = rb->read_ptr;
  326. free_cnt = (rb->size+r-w-1) & rb->size_mask;
  327. cnt2 = w + free_cnt;
  328. if(cnt2 > rb->size)
  329. {
  330. /* Two part vector: the rest of the buffer after the current write ptr,
  331. * plus some from the start of the buffer. */
  332. vec[0].buf = (char*)&(rb->buf[w*rb->elem_size]);
  333. vec[0].len = rb->size - w;
  334. vec[1].buf = (char*)rb->buf;
  335. vec[1].len = cnt2 & rb->size_mask;
  336. }
  337. else
  338. {
  339. vec[0].buf = (char*)&(rb->buf[w*rb->elem_size]);
  340. vec[0].len = free_cnt;
  341. vec[1].buf = NULL;
  342. vec[1].len = 0;
  343. }
  344. }