ringbuffer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-14 Jackistang add comments for function interface.
  9. */
  10. #ifndef RINGBUFFER_H__
  11. #define RINGBUFFER_H__
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #include <rtthread.h>
  16. /* ring buffer */
  17. struct rt_ringbuffer
  18. {
  19. rt_uint8_t *buffer_ptr;
  20. /* use the msb of the {read,write}_index as mirror bit. You can see this as
  21. * if the buffer adds a virtual mirror and the pointers point either to the
  22. * normal or to the mirrored buffer. If the write_index has the same value
  23. * with the read_index, but in a different mirror, the buffer is full.
  24. * While if the write_index and the read_index are the same and within the
  25. * same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
  26. *
  27. * mirror = 0 mirror = 1
  28. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  29. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
  30. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  31. * read_idx-^ write_idx-^
  32. *
  33. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  34. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
  35. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  36. * read_idx-^ ^-write_idx
  37. *
  38. * The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
  39. * But it should be enough for most of the cases.
  40. *
  41. * Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
  42. rt_uint16_t read_mirror : 1;
  43. rt_uint16_t read_index : 15;
  44. rt_uint16_t write_mirror : 1;
  45. rt_uint16_t write_index : 15;
  46. /* as we use msb of index as mirror bit, the size should be signed and
  47. * could only be positive. */
  48. rt_int16_t buffer_size;
  49. };
  50. enum rt_ringbuffer_state
  51. {
  52. RT_RINGBUFFER_EMPTY,
  53. RT_RINGBUFFER_FULL,
  54. /* half full is neither full nor empty */
  55. RT_RINGBUFFER_HALFFULL,
  56. };
  57. /**
  58. * RingBuffer for DeviceDriver
  59. *
  60. * Please note that the ring buffer implementation of RT-Thread
  61. * has no thread wait or resume feature.
  62. */
  63. void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size);
  64. void rt_ringbuffer_reset(struct rt_ringbuffer *rb);
  65. rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
  66. rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
  67. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch);
  68. rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch);
  69. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint16_t length);
  70. rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr);
  71. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
  72. rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);
  73. #ifdef RT_USING_HEAP
  74. struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t length);
  75. void rt_ringbuffer_destroy(struct rt_ringbuffer *rb);
  76. #endif
  77. /**
  78. * @brief Get the buffer size of the ring buffer object.
  79. *
  80. * @param rb A pointer to the ring buffer object.
  81. *
  82. * @return Buffer size.
  83. */
  84. rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
  85. {
  86. RT_ASSERT(rb != RT_NULL);
  87. return rb->buffer_size;
  88. }
  89. /** return the size of empty space in rb */
  90. #define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif