ringbuf.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #pragma once
  2. /*
  3. * ringbuf.h - C ring buffer (FIFO) interface.
  4. *
  5. * Written in 2011 by Drew Hess <dhess-src@bothan.net>.
  6. *
  7. * To the extent possible under law, the author(s) have dedicated all
  8. * copyright and related and neighboring rights to this software to
  9. * the public domain worldwide. This software is distributed without
  10. * any warranty.
  11. *
  12. * You should have received a copy of the CC0 Public Domain Dedication
  13. * along with this software. If not, see
  14. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  15. */
  16. /*
  17. * A byte-addressable ring buffer FIFO implementation.
  18. *
  19. * The ring buffer's head pointer points to the starting location
  20. * where data should be written when copying data *into* the buffer
  21. * (e.g., with ringbuf_read). The ring buffer's tail pointer points to
  22. * the starting location where data should be read when copying data
  23. * *from* the buffer (e.g., with ringbuf_write).
  24. */
  25. #include <stddef.h>
  26. #include <sys/types.h>
  27. typedef struct ringbuf_t *ringbuf_t;
  28. /*
  29. * Create a new ring buffer with the given capacity (usable
  30. * bytes). Note that the actual internal buffer size may be one or
  31. * more bytes larger than the usable capacity, for bookkeeping.
  32. *
  33. * Returns the new ring buffer object, or 0 if there's not enough
  34. * memory to fulfill the request for the given capacity.
  35. */
  36. ringbuf_t
  37. ringbuf_new(size_t capacity);
  38. /*
  39. * The size of the internal buffer, in bytes. One or more bytes may be
  40. * unusable in order to distinguish the "buffer full" state from the
  41. * "buffer empty" state.
  42. *
  43. * For the usable capacity of the ring buffer, use the
  44. * ringbuf_capacity function.
  45. */
  46. size_t
  47. ringbuf_buffer_size(const struct ringbuf_t *rb);
  48. /*
  49. * Deallocate a ring buffer, and, as a side effect, set the pointer to
  50. * 0.
  51. */
  52. void
  53. ringbuf_free(ringbuf_t *rb);
  54. /*
  55. * Reset a ring buffer to its initial state (empty).
  56. */
  57. void
  58. ringbuf_reset(ringbuf_t rb);
  59. /*
  60. * The usable capacity of the ring buffer, in bytes. Note that this
  61. * value may be less than the ring buffer's internal buffer size, as
  62. * returned by ringbuf_buffer_size.
  63. */
  64. size_t
  65. ringbuf_capacity(const struct ringbuf_t *rb);
  66. /*
  67. * The number of free/available bytes in the ring buffer. This value
  68. * is never larger than the ring buffer's usable capacity.
  69. */
  70. size_t
  71. ringbuf_bytes_free(const struct ringbuf_t *rb);
  72. /*
  73. * The number of bytes currently being used in the ring buffer. This
  74. * value is never larger than the ring buffer's usable capacity.
  75. */
  76. size_t
  77. ringbuf_bytes_used(const struct ringbuf_t *rb);
  78. int
  79. ringbuf_is_full(const struct ringbuf_t *rb);
  80. int
  81. ringbuf_is_empty(const struct ringbuf_t *rb);
  82. /*
  83. * Const access to the head and tail pointers of the ring buffer.
  84. */
  85. const void *
  86. ringbuf_tail(const struct ringbuf_t *rb);
  87. const void *
  88. ringbuf_head(const struct ringbuf_t *rb);
  89. /*
  90. * Locate the first occurrence of character c (converted to an
  91. * unsigned char) in ring buffer rb, beginning the search at offset
  92. * bytes from the ring buffer's tail pointer. The function returns the
  93. * offset of the character from the ring buffer's tail pointer, if
  94. * found. If c does not occur in the ring buffer, the function returns
  95. * the number of bytes used in the ring buffer.
  96. *
  97. * Note that the offset parameter and the returned offset are logical
  98. * offsets from the tail pointer, not necessarily linear offsets.
  99. */
  100. size_t
  101. ringbuf_findchr(const struct ringbuf_t *rb, int c, size_t offset);
  102. /*
  103. * Beginning at ring buffer dst's head pointer, fill the ring buffer
  104. * with a repeating sequence of len bytes, each of value c (converted
  105. * to an unsigned char). len can be as large as you like, but the
  106. * function will never write more than ringbuf_buffer_size(dst) bytes
  107. * in a single invocation, since that size will cause all bytes in the
  108. * ring buffer to be written exactly once each.
  109. *
  110. * Note that if len is greater than the number of free bytes in dst,
  111. * the ring buffer will overflow. When an overflow occurs, the state
  112. * of the ring buffer is guaranteed to be consistent, including the
  113. * head and tail pointers; old data will simply be overwritten in FIFO
  114. * fashion, as needed. However, note that, if calling the function
  115. * results in an overflow, the value of the ring buffer's tail pointer
  116. * may be different than it was before the function was called.
  117. *
  118. * Returns the actual number of bytes written to dst: len, if
  119. * len < ringbuf_buffer_size(dst), else ringbuf_buffer_size(dst).
  120. */
  121. size_t
  122. ringbuf_memset(ringbuf_t dst, int c, size_t len);
  123. /*
  124. * Copy n bytes from a contiguous memory area src into the ring buffer
  125. * dst. Returns the ring buffer's new head pointer.
  126. *
  127. * It is possible to copy more data from src than is available in the
  128. * buffer; i.e., it's possible to overflow the ring buffer using this
  129. * function. When an overflow occurs, the state of the ring buffer is
  130. * guaranteed to be consistent, including the head and tail pointers;
  131. * old data will simply be overwritten in FIFO fashion, as
  132. * needed. However, note that, if calling the function results in an
  133. * overflow, the value of the ring buffer's tail pointer may be
  134. * different than it was before the function was called.
  135. */
  136. void *
  137. ringbuf_memcpy_into(ringbuf_t dst, const void *src, size_t count);
  138. /*
  139. * This convenience function calls read(2) on the file descriptor fd,
  140. * using the ring buffer rb as the destination buffer for the read,
  141. * and returns the value returned by read(2). It will only call
  142. * read(2) once, and may return a short count.
  143. *
  144. * It is possible to read more data from the file descriptor than is
  145. * available in the buffer; i.e., it's possible to overflow the ring
  146. * buffer using this function. When an overflow occurs, the state of
  147. * the ring buffer is guaranteed to be consistent, including the head
  148. * and tail pointers: old data will simply be overwritten in FIFO
  149. * fashion, as needed. However, note that, if calling the function
  150. * results in an overflow, the value of the ring buffer's tail pointer
  151. * may be different than it was before the function was called.
  152. */
  153. ssize_t
  154. ringbuf_read(int fd, ringbuf_t rb, size_t count);
  155. /*
  156. * Copy n bytes from the ring buffer src, starting from its tail
  157. * pointer, into a contiguous memory area dst. Returns the value of
  158. * src's tail pointer after the copy is finished.
  159. *
  160. * Note that this copy is destructive with respect to the ring buffer:
  161. * the n bytes copied from the ring buffer are no longer available in
  162. * the ring buffer after the copy is complete, and the ring buffer
  163. * will have n more free bytes than it did before the function was
  164. * called.
  165. *
  166. * This function will *not* allow the ring buffer to underflow. If
  167. * count is greater than the number of bytes used in the ring buffer,
  168. * no bytes are copied, and the function will return 0.
  169. */
  170. void *
  171. ringbuf_memmove_from(void *dst, ringbuf_t src, size_t count);
  172. /* ringbuf_memmove_from() optimized for a single character.
  173. * Must only be called if the ringbuf is not empty */
  174. unsigned char
  175. ringbuf_move_char(ringbuf_t src);
  176. /*
  177. * Same as ringbuf_memmove_from() except that it does not change the ringbuffer
  178. * and returns the actual number of bytes copied, which is the minimum of ringbuf_bytes_used
  179. * and count.
  180. */
  181. size_t
  182. ringbuf_memcpy_from(void *dst, const ringbuf_t src, size_t count);
  183. /*
  184. * This convenience function calls write(2) on the file descriptor fd,
  185. * using the ring buffer rb as the source buffer for writing (starting
  186. * at the ring buffer's tail pointer), and returns the value returned
  187. * by write(2). It will only call write(2) once, and may return a
  188. * short count.
  189. *
  190. * Note that this copy is destructive with respect to the ring buffer:
  191. * any bytes written from the ring buffer to the file descriptor are
  192. * no longer available in the ring buffer after the copy is complete,
  193. * and the ring buffer will have N more free bytes than it did before
  194. * the function was called, where N is the value returned by the
  195. * function (unless N is < 0, in which case an error occurred and no
  196. * bytes were written).
  197. *
  198. * This function will *not* allow the ring buffer to underflow. If
  199. * count is greater than the number of bytes used in the ring buffer,
  200. * no bytes are written to the file descriptor, and the function will
  201. * return 0.
  202. */
  203. ssize_t
  204. ringbuf_write(int fd, ringbuf_t rb, size_t count);
  205. /*
  206. * Copy count bytes from ring buffer src, starting from its tail
  207. * pointer, into ring buffer dst. Returns dst's new head pointer after
  208. * the copy is finished.
  209. *
  210. * Note that this copy is destructive with respect to the ring buffer
  211. * src: any bytes copied from src into dst are no longer available in
  212. * src after the copy is complete, and src will have 'count' more free
  213. * bytes than it did before the function was called.
  214. *
  215. * It is possible to copy more data from src than is available in dst;
  216. * i.e., it's possible to overflow dst using this function. When an
  217. * overflow occurs, the state of dst is guaranteed to be consistent,
  218. * including the head and tail pointers; old data will simply be
  219. * overwritten in FIFO fashion, as needed. However, note that, if
  220. * calling the function results in an overflow, the value dst's tail
  221. * pointer may be different than it was before the function was
  222. * called.
  223. *
  224. * It is *not* possible to underflow src; if count is greater than the
  225. * number of bytes used in src, no bytes are copied, and the function
  226. * returns 0.
  227. */
  228. void *
  229. ringbuf_copy(ringbuf_t dst, ringbuf_t src, size_t count);