ringbuffer.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. type
  37. PPjack_ringbuffer_data_t = ^Pjack_ringbuffer_data_t;
  38. Pjack_ringbuffer_data_t = ^jack_ringbuffer_data_t;
  39. jack_ringbuffer_data_t = record
  40. buf: PChar;
  41. len: csize_t;
  42. end;
  43. PPjack_ringbuffer_t = ^Pjack_ringbuffer_t;
  44. Pjack_ringbuffer_t = ^jack_ringbuffer_t;
  45. jack_ringbuffer_t = record
  46. buf : PChar;
  47. write_ptr : csize_t;
  48. read_ptr : csize_t;
  49. size : csize_t;
  50. size_mask : csize_t;
  51. mlocked : cint;
  52. end;
  53. (**
  54. * Allocates a ringbuffer data structure of a specified size. The
  55. * caller must arrange for a call to jack_ringbuffer_free() to release
  56. * the memory associated with the ringbuffer.
  57. *
  58. * @param sz the ringbuffer size in bytes.
  59. *
  60. * @return a pointer to a new jack_ringbuffer_t, if successful; NULL
  61. * otherwise.
  62. *)
  63. function jack_ringbuffer_create(sz: csize_t): Pjack_ringbuffer_t; cdecl; external libjack;
  64. (**
  65. * Frees the ringbuffer data structure allocated by an earlier call to
  66. * jack_ringbuffer_create().
  67. *
  68. * @param rb a pointer to the ringbuffer structure.
  69. *)
  70. procedure jack_ringbuffer_free(rb: Pjack_ringbuffer_t); cdecl; external libjack;
  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. procedure jack_ringbuffer_get_read_vector(const rb: Pjack_ringbuffer_t;
  92. vec: Pjack_ringbuffer_data_t); cdecl; external libjack;
  93. (**
  94. * Fill a data structure with a description of the current writable
  95. * space in the ringbuffer. The description is returned in a two
  96. * element array of jack_ringbuffer_data_t. Two elements are needed
  97. * because the space available for writing may be split across the end
  98. * of the ringbuffer.
  99. *
  100. * The first element will always contain a valid @a len field, which
  101. * may be zero or greater. If the @a len field is non-zero, then data
  102. * can be written in a contiguous fashion using the address given in
  103. * the corresponding @a buf field.
  104. *
  105. * If the second element has a non-zero @a len field, then a second
  106. * contiguous stretch of data can be written to the address given in
  107. * the corresponding @a buf field.
  108. *
  109. * @param rb a pointer to the ringbuffer structure.
  110. * @param vec a pointer to a 2 element array of jack_ringbuffer_data_t.
  111. *)
  112. procedure jack_ringbuffer_get_write_vector(const rb: Pjack_ringbuffer_t;
  113. vec: Pjack_ringbuffer_data_t); cdecl; external libjack;
  114. (**
  115. * Read data from the ringbuffer.
  116. *
  117. * @param rb a pointer to the ringbuffer structure.
  118. * @param dest a pointer to a buffer where data read from the
  119. * ringbuffer will go.
  120. * @param cnt the number of bytes to read.
  121. *
  122. * @return the number of bytes read, which may range from 0 to cnt.
  123. *)
  124. function jack_ringbuffer_read(rb: Pjack_ringbuffer_t; dest: PChar; cnt: csize_t): csize_t; cdecl; external libjack;
  125. (**
  126. * Read data from the ringbuffer. Opposed to jack_ringbuffer_read()
  127. * this function does not move the read pointer. Thus it's
  128. * a convenient way to inspect data in the ringbuffer in a
  129. * continuous fashion. The price is that the data is copied
  130. * into a user provided buffer. For "raw" non-copy inspection
  131. * of the data in the ringbuffer use jack_ringbuffer_get_read_vector().
  132. *
  133. * @param rb a pointer to the ringbuffer structure.
  134. * @param dest a pointer to a buffer where data read from the
  135. * ringbuffer will go.
  136. * @param cnt the number of bytes to read.
  137. *
  138. * @return the number of bytes read, which may range from 0 to cnt.
  139. *)
  140. function jack_ringbuffer_peek(rb: Pjack_ringbuffer_t; dest: PChar; cnt: csize_t): csize_t; cdecl; external libjack;
  141. (**
  142. * Advance the read pointer.
  143. *
  144. * After data have been read from the ringbuffer using the pointers
  145. * returned by jack_ringbuffer_get_read_vector(), use this function to
  146. * advance the buffer pointers, making that space available for future
  147. * write operations.
  148. *
  149. * @param rb a pointer to the ringbuffer structure.
  150. * @param cnt the number of bytes read.
  151. *)
  152. procedure jack_ringbuffer_read_advance(rb: Pjack_ringbuffer_t; cnt: csize_t); cdecl; external libjack;
  153. (**
  154. * Return the number of bytes available for reading.
  155. *
  156. * @param rb a pointer to the ringbuffer structure.
  157. *
  158. * @return the number of bytes available to read.
  159. *)
  160. function jack_ringbuffer_read_space(const rb: Pjack_ringbuffer_t): csize_t; cdecl; external libjack;
  161. (**
  162. * Lock a ringbuffer data block into memory.
  163. *
  164. * Uses the mlock() system call. This is not a realtime operation.
  165. *
  166. * @param rb a pointer to the ringbuffer structure.
  167. *)
  168. function jack_ringbuffer_mlock(rb: Pjack_ringbuffer_t): cint; cdecl; external libjack;
  169. (**
  170. * Reset the read and write pointers, making an empty buffer.
  171. *
  172. * This is not thread safe.
  173. *
  174. * @param rb a pointer to the ringbuffer structure.
  175. *)
  176. procedure jack_ringbuffer_reset(rb: Pjack_ringbuffer_t); cdecl; external libjack;
  177. (**
  178. * Reset the internal "available" size, and read and write pointers, making an empty buffer.
  179. *
  180. * This is not thread safe.
  181. *
  182. * @param rb a pointer to the ringbuffer structure.
  183. * @param sz the new size, that must be less than allocated size.
  184. *)
  185. procedure jack_ringbuffer_reset_size (rb: Pjack_ringbuffer_t; sz: csize_t); cdecl; external libjack;
  186. (**
  187. * Write data into the ringbuffer.
  188. *
  189. * @param rb a pointer to the ringbuffer structure.
  190. * @param src a pointer to the data to be written to the ringbuffer.
  191. * @param cnt the number of bytes to write.
  192. *
  193. * @return the number of bytes write, which may range from 0 to cnt
  194. *)
  195. function jack_ringbuffer_write(rb: Pjack_ringbuffer_t; const src: PChar;
  196. cnt: csize_t): csize_t; cdecl; external libjack;
  197. (**
  198. * Advance the write pointer.
  199. *
  200. * After data have been written the ringbuffer using the pointers
  201. * returned by jack_ringbuffer_get_write_vector(), use this function
  202. * to advance the buffer pointer, making the data available for future
  203. * read operations.
  204. *
  205. * @param rb a pointer to the ringbuffer structure.
  206. * @param cnt the number of bytes written.
  207. *)
  208. procedure jack_ringbuffer_write_advance(rb: Pjack_ringbuffer_t; cnt: csize_t); cdecl; external libjack;
  209. (**
  210. * Return the number of bytes available for writing.
  211. *
  212. * @param rb a pointer to the ringbuffer structure.
  213. *
  214. * @return the amount of free space (in bytes) available for writing.
  215. *)
  216. function jack_ringbuffer_write_space(const rb: Pjack_ringbuffer_t): csize_t; cdecl; external libjack;
  217. //#ifdef __cplusplus
  218. //}
  219. //#endif
  220. {$endif}