ringbuffer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * 限制2^N ​​预计总耗时1.5 ms(位运算和内存对齐优势)​, 可修改不限制但是​预计总耗时12 ms
  21. */
  22. #define RING_BUFFER_IS_POWER_OF_TWO(buffer_size) ((buffer_size & (buffer_size - 1)) == 0)
  23. /**
  24. * The type which is used to hold the size
  25. * and the indicies of the buffer.
  26. */
  27. typedef size_t ring_buffer_size_t;
  28. /**
  29. * Used as a modulo operator
  30. * as <tt> a % b = (a & (b ? 1)) </tt>
  31. * where \c a is a positive index in the buffer and
  32. * \c b is the (power of two) size of the buffer.
  33. */
  34. #define RING_BUFFER_MASK(rb) (rb->buffer_mask)
  35. /**
  36. * Simplifies the use of <tt>struct ring_buffer_t</tt>.
  37. */
  38. typedef struct ring_buffer_t ring_buffer_t;
  39. /**
  40. * Structure which holds a ring buffer.
  41. * The buffer contains a buffer array
  42. * as well as metadata for the ring buffer.
  43. */
  44. struct ring_buffer_t {
  45. /** Buffer memory. */
  46. char *buffer;
  47. /** Buffer mask. */
  48. ring_buffer_size_t buffer_mask;
  49. /** Index of tail. */
  50. ring_buffer_size_t tail_index;
  51. /** Index of head. */
  52. ring_buffer_size_t head_index;
  53. };
  54. /**
  55. * Initializes the ring buffer pointed to by <em>buffer</em>.
  56. * This function can also be used to empty/reset the buffer.
  57. * @param buffer The ring buffer to initialize.
  58. * @param buf The buffer allocated for the ringbuffer.
  59. * @param buf_size The size of the allocated ringbuffer.
  60. */
  61. void ring_buffer_init(ring_buffer_t *buffer, char *buf, size_t buf_size);
  62. /**
  63. * Adds a byte to a ring buffer.
  64. * @param buffer The buffer in which the data should be placed.
  65. * @param data The byte to place.
  66. */
  67. void ring_buffer_queue(ring_buffer_t *buffer, char data);
  68. /**
  69. * Adds an array of bytes to a ring buffer.
  70. * @param buffer The buffer in which the data should be placed.
  71. * @param data A pointer to the array of bytes to place in the queue.
  72. * @param size The size of the array.
  73. */
  74. void ring_buffer_queue_arr(ring_buffer_t *buffer, const char *data, ring_buffer_size_t size);
  75. /**
  76. * Returns the oldest byte in a ring buffer.
  77. * @param buffer The buffer from which the data should be returned.
  78. * @param data A pointer to the location at which the data should be placed.
  79. * @return 1 if data was returned; 0 otherwise.
  80. */
  81. uint8_t ring_buffer_dequeue(ring_buffer_t *buffer, char *data);
  82. /**
  83. * Returns the <em>len</em> oldest bytes in a ring buffer.
  84. * @param buffer The buffer from which the data should be returned.
  85. * @param data A pointer to the array at which the data should be placed.
  86. * @param len The maximum number of bytes to return.
  87. * @return The number of bytes returned.
  88. */
  89. ring_buffer_size_t ring_buffer_dequeue_arr(ring_buffer_t *buffer, char *data, ring_buffer_size_t len);
  90. /**
  91. * Peeks a ring buffer, i.e. returns an element without removing it.
  92. * @param buffer The buffer from which the data should be returned.
  93. * @param data A pointer to the location at which the data should be placed.
  94. * @param index The index to peek.
  95. * @return 1 if data was returned; 0 otherwise.
  96. */
  97. uint8_t ring_buffer_peek(ring_buffer_t *buffer, char *data, ring_buffer_size_t index);
  98. /**
  99. * Returns whether a ring buffer is empty.
  100. * @param buffer The buffer for which it should be returned whether it is empty.
  101. * @return 1 if empty; 0 otherwise.
  102. */
  103. inline uint8_t ring_buffer_is_empty(ring_buffer_t *buffer) {
  104. return (buffer->head_index == buffer->tail_index);
  105. }
  106. /**
  107. * Returns whether a ring buffer is full.
  108. * @param buffer The buffer for which it should be returned whether it is full.
  109. * @return 1 if full; 0 otherwise.
  110. */
  111. inline uint8_t ring_buffer_is_full(ring_buffer_t *buffer) {
  112. return ((buffer->head_index - buffer->tail_index) & RING_BUFFER_MASK(buffer)) == RING_BUFFER_MASK(buffer);
  113. }
  114. /**
  115. * Returns the number of items in a ring buffer.
  116. * @param buffer The buffer for which the number of items should be returned.
  117. * @return The number of items in the ring buffer.
  118. */
  119. inline ring_buffer_size_t ring_buffer_num_items(ring_buffer_t *buffer) {
  120. return ((buffer->head_index - buffer->tail_index) & RING_BUFFER_MASK(buffer));
  121. }
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif /* RINGBUFFER_H */