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.

244 lines
7.8KB

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