ringbuffer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <inttypes.h>
  2. #include <stddef.h>
  3. #include <assert.h>
  4. /**
  5. * @file
  6. * Prototypes and structures for the ring buffer module.
  7. */
  8. #ifndef RINGBUFFER_H
  9. #define RINGBUFFER_H
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. #define RING_BUFFER_ASSERT(x) assert(x)
  15. /**
  16. * Checks if the buffer_size is a power of two.
  17. * Due to the design only <tt> RING_BUFFER_SIZE-1 </tt> items
  18. * can be contained in the buffer.
  19. * buffer_size must be a power of two.
  20. */
  21. #define RING_BUFFER_IS_POWER_OF_TWO(buffer_size) ((buffer_size & (buffer_size - 1)) == 0)
  22. /**
  23. * The type which is used to hold the size
  24. * and the indicies of the buffer.
  25. */
  26. typedef size_t ring_buffer_size_t;
  27. /**
  28. * Used as a modulo operator
  29. * as <tt> a % b = (a & (b ? 1)) </tt>
  30. * where \c a is a positive index in the buffer and
  31. * \c b is the (power of two) size of the buffer.
  32. */
  33. #define RING_BUFFER_MASK(rb) (rb->buffer_mask)
  34. /**
  35. * Simplifies the use of <tt>struct ring_buffer_t</tt>.
  36. */
  37. typedef struct ring_buffer_t ring_buffer_t;
  38. /**
  39. * Structure which holds a ring buffer.
  40. * The buffer contains a buffer array
  41. * as well as metadata for the ring buffer.
  42. */
  43. struct ring_buffer_t {
  44. /** Buffer memory. */
  45. char *buffer;
  46. /** Buffer mask. */
  47. ring_buffer_size_t buffer_mask;
  48. /** Index of tail. */
  49. ring_buffer_size_t tail_index;
  50. /** Index of head. */
  51. ring_buffer_size_t head_index;
  52. };
  53. /**
  54. * Initializes the ring buffer pointed to by <em>buffer</em>.
  55. * This function can also be used to empty/reset the buffer.
  56. * @param buffer The ring buffer to initialize.
  57. * @param buf The buffer allocated for the ringbuffer.
  58. * @param buf_size The size of the allocated ringbuffer.
  59. */
  60. void ring_buffer_init(ring_buffer_t *buffer, char *buf, size_t buf_size);
  61. /**
  62. * Adds a byte to a ring buffer.
  63. * @param buffer The buffer in which the data should be placed.
  64. * @param data The byte to place.
  65. */
  66. void ring_buffer_queue(ring_buffer_t *buffer, char data);
  67. /**
  68. * Adds an array of bytes to a ring buffer.
  69. * @param buffer The buffer in which the data should be placed.
  70. * @param data A pointer to the array of bytes to place in the queue.
  71. * @param size The size of the array.
  72. */
  73. void ring_buffer_queue_arr(ring_buffer_t *buffer, const char *data, ring_buffer_size_t size);
  74. /**
  75. * Returns the oldest byte in a ring buffer.
  76. * @param buffer The buffer from which the data should be returned.
  77. * @param data A pointer to the location at which the data should be placed.
  78. * @return 1 if data was returned; 0 otherwise.
  79. */
  80. uint8_t ring_buffer_dequeue(ring_buffer_t *buffer, char *data);
  81. /**
  82. * Returns the <em>len</em> oldest bytes in a ring buffer.
  83. * @param buffer The buffer from which the data should be returned.
  84. * @param data A pointer to the array at which the data should be placed.
  85. * @param len The maximum number of bytes to return.
  86. * @return The number of bytes returned.
  87. */
  88. ring_buffer_size_t ring_buffer_dequeue_arr(ring_buffer_t *buffer, char *data, ring_buffer_size_t len);
  89. /**
  90. * Peeks a ring buffer, i.e. returns an element without removing it.
  91. * @param buffer The buffer from which the data should be returned.
  92. * @param data A pointer to the location at which the data should be placed.
  93. * @param index The index to peek.
  94. * @return 1 if data was returned; 0 otherwise.
  95. */
  96. uint8_t ring_buffer_peek(ring_buffer_t *buffer, char *data, ring_buffer_size_t index);
  97. /**
  98. * Returns whether a ring buffer is empty.
  99. * @param buffer The buffer for which it should be returned whether it is empty.
  100. * @return 1 if empty; 0 otherwise.
  101. */
  102. inline uint8_t ring_buffer_is_empty(ring_buffer_t *buffer) {
  103. return (buffer->head_index == buffer->tail_index);
  104. }
  105. /**
  106. * Returns whether a ring buffer is full.
  107. * @param buffer The buffer for which it should be returned whether it is full.
  108. * @return 1 if full; 0 otherwise.
  109. */
  110. inline uint8_t ring_buffer_is_full(ring_buffer_t *buffer) {
  111. return ((buffer->head_index - buffer->tail_index) & RING_BUFFER_MASK(buffer)) == RING_BUFFER_MASK(buffer);
  112. }
  113. /**
  114. * Returns the number of items in a ring buffer.
  115. * @param buffer The buffer for which the number of items should be returned.
  116. * @return The number of items in the ring buffer.
  117. */
  118. inline ring_buffer_size_t ring_buffer_num_items(ring_buffer_t *buffer) {
  119. return ((buffer->head_index - buffer->tail_index) & RING_BUFFER_MASK(buffer));
  120. }
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. #endif /* RINGBUFFER_H */