ringbuffer.h 3.4 KB

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