ringbuffer.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifndef RINGBUFFER_H
  2. #define RINGBUFFER_H
  3. #include <stddef.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. typedef struct ll_ringbuffer ll_ringbuffer_t;
  8. typedef struct ll_ringbuffer_data {
  9. char *buf;
  10. size_t len;
  11. } ll_ringbuffer_data_t;
  12. /**
  13. * Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes.
  14. * The number of elements is rounded up to the next power of two (even if it is
  15. * already a power of two, to ensure the requested amount can be written).
  16. */
  17. ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz, int limit_writes);
  18. /** Free all data associated with the ringbuffer `rb'. */
  19. void ll_ringbuffer_free(ll_ringbuffer_t *rb);
  20. /** Reset the read and write pointers to zero. This is not thread safe. */
  21. void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
  22. /**
  23. * The non-copying data reader. `vec' is an array of two places. Set the values
  24. * at `vec' to hold the current readable data at `rb'. If the readable data is
  25. * in one segment the second segment has zero length.
  26. */
  27. void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]);
  28. /**
  29. * The non-copying data writer. `vec' is an array of two places. Set the values
  30. * at `vec' to hold the current writeable data at `rb'. If the writeable data
  31. * is in one segment the second segment has zero length.
  32. */
  33. void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t vec[2]);
  34. /**
  35. * Return the number of elements available for reading. This is the number of
  36. * elements in front of the read pointer and behind the write pointer.
  37. */
  38. size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
  39. /**
  40. * The copying data reader. Copy at most `cnt' elements from `rb' to `dest'.
  41. * Returns the actual number of elements copied.
  42. */
  43. size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
  44. /**
  45. * The copying data reader w/o read pointer advance. Copy at most `cnt'
  46. * elements from `rb' to `dest'. Returns the actual number of elements copied.
  47. */
  48. size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
  49. /** Advance the read pointer `cnt' places. */
  50. void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
  51. /**
  52. * Return the number of elements available for writing. This is the number of
  53. * elements in front of the write pointer and behind the read pointer.
  54. */
  55. size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
  56. /**
  57. * The copying data writer. Copy at most `cnt' elements to `rb' from `src'.
  58. * Returns the actual number of elements copied.
  59. */
  60. size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
  61. /** Advance the write pointer `cnt' places. */
  62. void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
  63. #ifdef __cplusplus
  64. } /* extern "C" */
  65. #endif
  66. #endif /* RINGBUFFER_H */