jack2 codebase
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.

259 lines
8.1KB

  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. */
  16. #ifndef _RINGBUFFER_H
  17. #define _RINGBUFFER_H
  18. #ifdef __cplusplus
  19. extern "C"
  20. {
  21. #endif
  22. #include <sys/types.h>
  23. #include <jack/systemdeps.h>
  24. /** @file ringbuffer.h
  25. *
  26. * A set of library functions to make lock-free ringbuffers available
  27. * to JACK clients. The `capture_client.c' (in the example_clients
  28. * directory) is a fully functioning user of this API.
  29. *
  30. * The key attribute of a ringbuffer is that it can be safely accessed
  31. * by two threads simultaneously -- one reading from the buffer and
  32. * the other writing to it -- without using any synchronization or
  33. * mutual exclusion primitives. For this to work correctly, there can
  34. * only be a single reader and a single writer thread. Their
  35. * identities cannot be interchanged.
  36. */
  37. typedef struct {
  38. char *buf;
  39. size_t len;
  40. }
  41. jack_ringbuffer_data_t ;
  42. typedef struct {
  43. char *buf;
  44. volatile size_t write_ptr;
  45. volatile size_t read_ptr;
  46. size_t size;
  47. size_t size_mask;
  48. int mlocked;
  49. }
  50. jack_ringbuffer_t ;
  51. /**
  52. * Allocates a ringbuffer data structure of a specified size. The
  53. * caller must arrange for a call to jack_ringbuffer_free() to release
  54. * the memory associated with the ringbuffer.
  55. *
  56. * @param sz the ringbuffer size in bytes.
  57. *
  58. * @return a pointer to a new jack_ringbuffer_t, if successful; NULL
  59. * otherwise.
  60. */
  61. JACK_CLIENT_API_EXPORT
  62. jack_ringbuffer_t *jack_ringbuffer_create(size_t sz);
  63. /**
  64. * Frees the ringbuffer data structure allocated by an earlier call to
  65. * jack_ringbuffer_create().
  66. *
  67. * @param rb a pointer to the ringbuffer structure.
  68. */
  69. JACK_CLIENT_API_EXPORT
  70. void jack_ringbuffer_free(jack_ringbuffer_t *rb);
  71. /**
  72. * Fill a data structure with a description of the current readable
  73. * data held in the ringbuffer. This description is returned in a two
  74. * element array of jack_ringbuffer_data_t. Two elements are needed
  75. * because the data to be read may be split across the end of the
  76. * ringbuffer.
  77. *
  78. * The first element will always contain a valid @a len field, which
  79. * may be zero or greater. If the @a len field is non-zero, then data
  80. * can be read in a contiguous fashion using the address given in the
  81. * corresponding @a buf field.
  82. *
  83. * If the second element has a non-zero @a len field, then a second
  84. * contiguous stretch of data can be read from the address given in
  85. * its corresponding @a buf field.
  86. *
  87. * @param rb a pointer to the ringbuffer structure.
  88. * @param vec a pointer to a 2 element array of jack_ringbuffer_data_t.
  89. *
  90. */
  91. JACK_CLIENT_API_EXPORT
  92. void jack_ringbuffer_get_read_vector(const jack_ringbuffer_t *rb,
  93. jack_ringbuffer_data_t *vec);
  94. /**
  95. * Fill a data structure with a description of the current writable
  96. * space in the ringbuffer. The description is returned in a two
  97. * element array of jack_ringbuffer_data_t. Two elements are needed
  98. * because the space available for writing may be split across the end
  99. * of the ringbuffer.
  100. *
  101. * The first element will always contain a valid @a len field, which
  102. * may be zero or greater. If the @a len field is non-zero, then data
  103. * can be written in a contiguous fashion using the address given in
  104. * the corresponding @a buf field.
  105. *
  106. * If the second element has a non-zero @a len field, then a second
  107. * contiguous stretch of data can be written to the address given in
  108. * the corresponding @a buf field.
  109. *
  110. * @param rb a pointer to the ringbuffer structure.
  111. * @param vec a pointer to a 2 element array of jack_ringbuffer_data_t.
  112. */
  113. JACK_CLIENT_API_EXPORT
  114. void jack_ringbuffer_get_write_vector(const jack_ringbuffer_t *rb,
  115. jack_ringbuffer_data_t *vec);
  116. /**
  117. * Read data from the ringbuffer.
  118. *
  119. * @param rb a pointer to the ringbuffer structure.
  120. * @param dest a pointer to a buffer where data read from the
  121. * ringbuffer will go.
  122. * @param cnt the number of bytes to read.
  123. *
  124. * @return the number of bytes read, which may range from 0 to cnt.
  125. */
  126. JACK_CLIENT_API_EXPORT
  127. size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  128. /**
  129. * Read data from the ringbuffer. Opposed to jack_ringbuffer_read()
  130. * this function does not move the read pointer. Thus it's
  131. * a convenient way to inspect data in the ringbuffer in a
  132. * continuous fashion. The price is that the data is copied
  133. * into a user provided buffer. For "raw" non-copy inspection
  134. * of the data in the ringbuffer use jack_ringbuffer_get_read_vector().
  135. *
  136. * @param rb a pointer to the ringbuffer structure.
  137. * @param dest a pointer to a buffer where data read from the
  138. * ringbuffer will go.
  139. * @param cnt the number of bytes to read.
  140. *
  141. * @return the number of bytes read, which may range from 0 to cnt.
  142. */
  143. JACK_CLIENT_API_EXPORT
  144. size_t jack_ringbuffer_peek(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  145. /**
  146. * Advance the read pointer.
  147. *
  148. * After data have been read from the ringbuffer using the pointers
  149. * returned by jack_ringbuffer_get_read_vector(), use this function to
  150. * advance the buffer pointers, making that space available for future
  151. * write operations.
  152. *
  153. * @param rb a pointer to the ringbuffer structure.
  154. * @param cnt the number of bytes read.
  155. */
  156. JACK_CLIENT_API_EXPORT
  157. void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt);
  158. /**
  159. * Return the number of bytes available for reading.
  160. *
  161. * @param rb a pointer to the ringbuffer structure.
  162. *
  163. * @return the number of bytes available to read.
  164. */
  165. JACK_CLIENT_API_EXPORT
  166. size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb);
  167. /**
  168. * Lock a ringbuffer data block into memory.
  169. *
  170. * Uses the mlock() system call. This is not a realtime operation.
  171. *
  172. * @param rb a pointer to the ringbuffer structure.
  173. */
  174. JACK_CLIENT_API_EXPORT
  175. int jack_ringbuffer_mlock(jack_ringbuffer_t *rb);
  176. /**
  177. * Reset the read and write pointers, making an empty buffer.
  178. *
  179. * This is not thread safe.
  180. *
  181. * @param rb a pointer to the ringbuffer structure.
  182. */
  183. JACK_CLIENT_API_EXPORT
  184. void jack_ringbuffer_reset(jack_ringbuffer_t *rb);
  185. /**
  186. * Reset the internal "available" size, and read and write pointers, making an empty buffer.
  187. *
  188. * This is not thread safe.
  189. *
  190. * @param rb a pointer to the ringbuffer structure.
  191. * @param sz the new size, that must be less than allocated size.
  192. */
  193. JACK_CLIENT_API_EXPORT
  194. void jack_ringbuffer_reset_size (jack_ringbuffer_t * rb, size_t sz);
  195. /**
  196. * Write data into the ringbuffer.
  197. *
  198. * @param rb a pointer to the ringbuffer structure.
  199. * @param src a pointer to the data to be written to the ringbuffer.
  200. * @param cnt the number of bytes to write.
  201. *
  202. * @return the number of bytes write, which may range from 0 to cnt
  203. */
  204. JACK_CLIENT_API_EXPORT
  205. size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src,
  206. size_t cnt);
  207. /**
  208. * Advance the write pointer.
  209. *
  210. * After data have been written the ringbuffer using the pointers
  211. * returned by jack_ringbuffer_get_write_vector(), use this function
  212. * to advance the buffer pointer, making the data available for future
  213. * read operations.
  214. *
  215. * @param rb a pointer to the ringbuffer structure.
  216. * @param cnt the number of bytes written.
  217. */
  218. JACK_CLIENT_API_EXPORT
  219. void jack_ringbuffer_write_advance(jack_ringbuffer_t *rb, size_t cnt);
  220. /**
  221. * Return the number of bytes available for writing.
  222. *
  223. * @param rb a pointer to the ringbuffer structure.
  224. *
  225. * @return the amount of free space (in bytes) available for writing.
  226. */
  227. JACK_CLIENT_API_EXPORT
  228. size_t jack_ringbuffer_write_space(const jack_ringbuffer_t *rb);
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif