RingBuffer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "RingBuffer.h"
  4. ByteBuffer::ByteBuffer(uint32_t _size)
  5. {
  6. buf = (uint8_t*)calloc(1, _size);
  7. size = buf ? _size : 0;
  8. }
  9. ByteBuffer::~ByteBuffer(void)
  10. {
  11. free(buf);
  12. }
  13. /*
  14. * Caller is responsible for locking in set_size()
  15. */
  16. bool ByteBuffer::set_size(uint32_t _size)
  17. {
  18. head = tail = 0;
  19. if (_size != size) {
  20. free(buf);
  21. buf = (uint8_t*)calloc(1, _size);
  22. if (!buf) {
  23. size = 0;
  24. return false;
  25. }
  26. size = _size;
  27. }
  28. return true;
  29. }
  30. uint32_t ByteBuffer::available(void) const
  31. {
  32. /* use a copy on stack to avoid race conditions of @tail being updated by
  33. * the writer thread */
  34. uint32_t _tail = tail;
  35. if (head > _tail) {
  36. return size - head + _tail;
  37. }
  38. return _tail - head;
  39. }
  40. void ByteBuffer::clear(void)
  41. {
  42. head = tail = 0;
  43. }
  44. uint32_t ByteBuffer::space(void) const
  45. {
  46. if (size == 0) {
  47. return 0;
  48. }
  49. /* use a copy on stack to avoid race conditions of @head being updated by
  50. * the reader thread */
  51. uint32_t _head = head;
  52. uint32_t ret = 0;
  53. if (_head <= tail) {
  54. ret = size;
  55. }
  56. ret += _head - tail - 1;
  57. return ret;
  58. }
  59. bool ByteBuffer::empty(void) const
  60. {
  61. return head == tail;
  62. }
  63. uint32_t ByteBuffer::write(const uint8_t *data, uint32_t len)
  64. {
  65. ByteBuffer::IoVec vec[2];
  66. const auto n_vec = reserve(vec, len);
  67. uint32_t ret = 0;
  68. for (int i = 0; i < n_vec; i++) {
  69. memcpy(vec[i].data, data + ret, vec[i].len);
  70. ret += vec[i].len;
  71. }
  72. commit(ret);
  73. return ret;
  74. }
  75. /*
  76. update bytes at the read pointer. Used to update an object without
  77. popping it
  78. */
  79. bool ByteBuffer::update(const uint8_t *data, uint32_t len)
  80. {
  81. if (len > available()) {
  82. return false;
  83. }
  84. // perform as two memcpy calls
  85. uint32_t n = size - head;
  86. if (n > len) {
  87. n = len;
  88. }
  89. memcpy(&buf[head], data, n);
  90. data += n;
  91. if (len > n) {
  92. memcpy(&buf[0], data, len-n);
  93. }
  94. return true;
  95. }
  96. bool ByteBuffer::advance(uint32_t n)
  97. {
  98. if (n > available()) {
  99. return false;
  100. }
  101. head = (head + n) % size;
  102. return true;
  103. }
  104. uint8_t ByteBuffer::peekiovec(ByteBuffer::IoVec iovec[2], uint32_t len)
  105. {
  106. uint32_t n = available();
  107. if (len > n) {
  108. len = n;
  109. }
  110. if (len == 0) {
  111. return 0;
  112. }
  113. auto b = readptr(n);
  114. if (n == 0) {
  115. iovec[0].data = buf;
  116. iovec[0].len = len;
  117. iovec[1].data = nullptr;
  118. iovec[1].len = 0;
  119. return 1;
  120. }
  121. if (n > len) {
  122. n = len;
  123. }
  124. iovec[0].data = const_cast<uint8_t *>(b);
  125. iovec[0].len = n;
  126. if (len <= n) {
  127. return 1;
  128. }
  129. iovec[1].data = buf;
  130. iovec[1].len = len - n;
  131. return 2;
  132. }
  133. /*
  134. read len bytes without advancing the read pointer
  135. */
  136. uint32_t ByteBuffer::peekbytes(uint8_t *data, uint32_t len)
  137. {
  138. ByteBuffer::IoVec vec[2];
  139. const auto n_vec = peekiovec(vec, len);
  140. uint32_t ret = 0;
  141. for (int i = 0; i < n_vec; i++) {
  142. memcpy(data + ret, vec[i].data, vec[i].len);
  143. ret += vec[i].len;
  144. }
  145. return ret;
  146. }
  147. uint8_t ByteBuffer::reserve(ByteBuffer::IoVec iovec[2], uint32_t len)
  148. {
  149. uint32_t n = space();
  150. if (len > n) {
  151. len = n;
  152. }
  153. if (!len) {
  154. return 0;
  155. }
  156. iovec[0].data = &buf[tail];
  157. n = size - tail;
  158. if (len <= n) {
  159. iovec[0].len = len;
  160. return 1;
  161. }
  162. iovec[0].len = n;
  163. iovec[1].data = buf;
  164. iovec[1].len = len - n;
  165. return 2;
  166. }
  167. /*
  168. * Advance the writer pointer by 'len'
  169. */
  170. bool ByteBuffer::commit(uint32_t len)
  171. {
  172. if (len > space()) {
  173. return false; //Someone broke the agreement
  174. }
  175. tail = (tail + len) % size;
  176. return true;
  177. }
  178. uint32_t ByteBuffer::read(uint8_t *data, uint32_t len)
  179. {
  180. uint32_t ret = peekbytes(data, len);
  181. advance(ret);
  182. return ret;
  183. }
  184. bool ByteBuffer::read_byte(uint8_t *data)
  185. {
  186. if (!data) {
  187. return false;
  188. }
  189. int16_t ret = peek(0);
  190. if (ret < 0) {
  191. return false;
  192. }
  193. *data = ret;
  194. return advance(1);
  195. }
  196. /*
  197. * Returns the pointer and size to a contiguous read in the buffer
  198. */
  199. const uint8_t *ByteBuffer::readptr(uint32_t &available_bytes)
  200. {
  201. uint32_t _tail = tail;
  202. available_bytes = (head > _tail) ? size - head : _tail - head;
  203. return available_bytes ? &buf[head] : nullptr;
  204. }
  205. int16_t ByteBuffer::peek(uint32_t ofs) const
  206. {
  207. if (ofs >= available()) {
  208. return -1;
  209. }
  210. return buf[(head+ofs)%size];
  211. }