ringbuffer.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. * 2012-09-30 Bernard first version.
  9. * 2013-05-08 Grissiom reimplement
  10. * 2016-08-18 heyuanjie add interface
  11. * 2021-01-26 Loogg Move to Linux
  12. */
  13. #include "ringbuffer.h"
  14. #include <string.h>
  15. static __inline enum rt_ringbuffer_state rt_ringbuffer_status(struct rt_ringbuffer *rb)
  16. {
  17. if (rb->read_index == rb->write_index) {
  18. if (rb->read_mirror == rb->write_mirror)
  19. return RT_RINGBUFFER_EMPTY;
  20. else
  21. return RT_RINGBUFFER_FULL;
  22. }
  23. return RT_RINGBUFFER_HALFFULL;
  24. }
  25. void rt_ringbuffer_init(struct rt_ringbuffer *rb,
  26. uint8_t *pool,
  27. int16_t size)
  28. {
  29. assert(rb != NULL);
  30. assert(size > 0);
  31. /* initialize read and write index */
  32. rb->read_mirror = rb->read_index = 0;
  33. rb->write_mirror = rb->write_index = 0;
  34. /* set buffer pool and size */
  35. rb->buffer_ptr = pool;
  36. rb->buffer_size = RT_ALIGN_DOWN(size, __SIZEOF_POINTER__);
  37. }
  38. /**
  39. * put a block of data into ring buffer
  40. */
  41. uint32_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
  42. const uint8_t *ptr,
  43. uint16_t length)
  44. {
  45. uint16_t size;
  46. assert(rb != NULL);
  47. /* whether has enough space */
  48. size = rt_ringbuffer_space_len(rb);
  49. /* no space */
  50. if (size == 0)
  51. return 0;
  52. /* drop some data */
  53. if (size < length)
  54. length = size;
  55. if (rb->buffer_size - rb->write_index > length) {
  56. /* read_index - write_index = empty space */
  57. memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
  58. /* this should not cause overflow because there is enough space for
  59. * length of data in current mirror */
  60. rb->write_index += length;
  61. return length;
  62. }
  63. memcpy(&rb->buffer_ptr[rb->write_index],
  64. &ptr[0],
  65. rb->buffer_size - rb->write_index);
  66. memcpy(&rb->buffer_ptr[0],
  67. &ptr[rb->buffer_size - rb->write_index],
  68. length - (rb->buffer_size - rb->write_index));
  69. /* we are going into the other side of the mirror */
  70. rb->write_mirror = ~rb->write_mirror;
  71. rb->write_index = length - (rb->buffer_size - rb->write_index);
  72. return length;
  73. }
  74. /**
  75. * put a block of data into ring buffer
  76. *
  77. * When the buffer is full, it will discard the old data.
  78. */
  79. uint32_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
  80. const uint8_t *ptr,
  81. uint16_t length)
  82. {
  83. uint16_t space_length;
  84. assert(rb != NULL);
  85. space_length = rt_ringbuffer_space_len(rb);
  86. if (length > rb->buffer_size) {
  87. ptr = &ptr[length - rb->buffer_size];
  88. length = rb->buffer_size;
  89. }
  90. if (rb->buffer_size - rb->write_index > length) {
  91. /* read_index - write_index = empty space */
  92. memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
  93. /* this should not cause overflow because there is enough space for
  94. * length of data in current mirror */
  95. rb->write_index += length;
  96. if (length > space_length)
  97. rb->read_index = rb->write_index;
  98. return length;
  99. }
  100. memcpy(&rb->buffer_ptr[rb->write_index],
  101. &ptr[0],
  102. rb->buffer_size - rb->write_index);
  103. memcpy(&rb->buffer_ptr[0],
  104. &ptr[rb->buffer_size - rb->write_index],
  105. length - (rb->buffer_size - rb->write_index));
  106. /* we are going into the other side of the mirror */
  107. rb->write_mirror = ~rb->write_mirror;
  108. rb->write_index = length - (rb->buffer_size - rb->write_index);
  109. if (length > space_length) {
  110. rb->read_mirror = ~rb->read_mirror;
  111. rb->read_index = rb->write_index;
  112. }
  113. return length;
  114. }
  115. /**
  116. * get data from ring buffer
  117. */
  118. uint32_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
  119. uint8_t *ptr,
  120. uint16_t length)
  121. {
  122. uint32_t size;
  123. assert(rb != NULL);
  124. /* whether has enough data */
  125. size = rt_ringbuffer_data_len(rb);
  126. /* no data */
  127. if (size == 0)
  128. return 0;
  129. /* less data */
  130. if (size < length)
  131. length = size;
  132. if (rb->buffer_size - rb->read_index > length) {
  133. /* copy all of data */
  134. memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
  135. /* this should not cause overflow because there is enough space for
  136. * length of data in current mirror */
  137. rb->read_index += length;
  138. return length;
  139. }
  140. memcpy(&ptr[0],
  141. &rb->buffer_ptr[rb->read_index],
  142. rb->buffer_size - rb->read_index);
  143. memcpy(&ptr[rb->buffer_size - rb->read_index],
  144. &rb->buffer_ptr[0],
  145. length - (rb->buffer_size - rb->read_index));
  146. /* we are going into the other side of the mirror */
  147. rb->read_mirror = ~rb->read_mirror;
  148. rb->read_index = length - (rb->buffer_size - rb->read_index);
  149. return length;
  150. }
  151. /**
  152. * peak data from ring buffer
  153. */
  154. uint32_t rt_ringbuffer_peak(struct rt_ringbuffer *rb, uint8_t **ptr)
  155. {
  156. assert(rb != NULL);
  157. *ptr = NULL;
  158. /* whether has enough data */
  159. uint32_t size = rt_ringbuffer_data_len(rb);
  160. /* no data */
  161. if (size == 0)
  162. return 0;
  163. *ptr = &rb->buffer_ptr[rb->read_index];
  164. if (rb->buffer_size - rb->read_index > size) {
  165. rb->read_index += size;
  166. return size;
  167. }
  168. size = rb->buffer_size - rb->read_index;
  169. /* we are going into the other side of the mirror */
  170. rb->read_mirror = ~rb->read_mirror;
  171. rb->read_index = 0;
  172. return size;
  173. }
  174. /**
  175. * put a character into ring buffer
  176. */
  177. uint32_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const uint8_t ch)
  178. {
  179. assert(rb != NULL);
  180. /* whether has enough space */
  181. if (!rt_ringbuffer_space_len(rb))
  182. return 0;
  183. rb->buffer_ptr[rb->write_index] = ch;
  184. /* flip mirror */
  185. if (rb->write_index == rb->buffer_size - 1) {
  186. rb->write_mirror = ~rb->write_mirror;
  187. rb->write_index = 0;
  188. } else {
  189. rb->write_index++;
  190. }
  191. return 1;
  192. }
  193. /**
  194. * put a character into ring buffer
  195. *
  196. * When the buffer is full, it will discard one old data.
  197. */
  198. uint32_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const uint8_t ch)
  199. {
  200. enum rt_ringbuffer_state old_state;
  201. assert(rb != NULL);
  202. old_state = rt_ringbuffer_status(rb);
  203. rb->buffer_ptr[rb->write_index] = ch;
  204. /* flip mirror */
  205. if (rb->write_index == rb->buffer_size - 1) {
  206. rb->write_mirror = ~rb->write_mirror;
  207. rb->write_index = 0;
  208. if (old_state == RT_RINGBUFFER_FULL) {
  209. rb->read_mirror = ~rb->read_mirror;
  210. rb->read_index = rb->write_index;
  211. }
  212. } else {
  213. rb->write_index++;
  214. if (old_state == RT_RINGBUFFER_FULL)
  215. rb->read_index = rb->write_index;
  216. }
  217. return 1;
  218. }
  219. /**
  220. * get a character from a ringbuffer
  221. */
  222. uint32_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, uint8_t *ch)
  223. {
  224. assert(rb != NULL);
  225. /* ringbuffer is empty */
  226. if (!rt_ringbuffer_data_len(rb))
  227. return 0;
  228. /* put character */
  229. *ch = rb->buffer_ptr[rb->read_index];
  230. if (rb->read_index == rb->buffer_size - 1) {
  231. rb->read_mirror = ~rb->read_mirror;
  232. rb->read_index = 0;
  233. } else {
  234. rb->read_index++;
  235. }
  236. return 1;
  237. }
  238. /**
  239. * get the size of data in rb
  240. */
  241. uint32_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
  242. {
  243. switch (rt_ringbuffer_status(rb)) {
  244. case RT_RINGBUFFER_EMPTY:
  245. return 0;
  246. case RT_RINGBUFFER_FULL:
  247. return rb->buffer_size;
  248. case RT_RINGBUFFER_HALFFULL:
  249. default:
  250. if (rb->write_index > rb->read_index)
  251. return rb->write_index - rb->read_index;
  252. else
  253. return rb->buffer_size - (rb->read_index - rb->write_index);
  254. };
  255. }
  256. /**
  257. * empty the rb
  258. */
  259. void rt_ringbuffer_reset(struct rt_ringbuffer *rb)
  260. {
  261. assert(rb != NULL);
  262. rb->read_mirror = 0;
  263. rb->read_index = 0;
  264. rb->write_mirror = 0;
  265. rb->write_index = 0;
  266. }
  267. struct rt_ringbuffer *rt_ringbuffer_create(uint16_t size)
  268. {
  269. struct rt_ringbuffer *rb;
  270. uint8_t *pool;
  271. assert(size > 0);
  272. size = RT_ALIGN_DOWN(size, __SIZEOF_POINTER__);
  273. rb = (struct rt_ringbuffer *)malloc(sizeof(struct rt_ringbuffer));
  274. if (rb == NULL)
  275. goto exit;
  276. pool = (uint8_t *)malloc(size);
  277. if (pool == NULL) {
  278. free(rb);
  279. rb = NULL;
  280. goto exit;
  281. }
  282. rt_ringbuffer_init(rb, pool, size);
  283. exit:
  284. return rb;
  285. }
  286. void rt_ringbuffer_destroy(struct rt_ringbuffer *rb)
  287. {
  288. assert(rb != NULL);
  289. free(rb->buffer_ptr);
  290. free(rb);
  291. }