ringbuffer.h 3.3 KB

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