RingBuffer.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_RINGBUFFER_H
  14. #define ZT_RINGBUFFER_H
  15. #include <typeinfo>
  16. #include <cstdint>
  17. #include <cstdlib>
  18. #include <cmath>
  19. #include <algorithm>
  20. namespace ZeroTier {
  21. /**
  22. * A circular buffer
  23. *
  24. * For fast handling of continuously-evolving variables (such as path quality metrics).
  25. * Using this, we can maintain longer sliding historical windows for important path
  26. * metrics without the need for potentially expensive calls to memcpy/memmove.
  27. *
  28. * Some basic statistical functionality is implemented here in an attempt
  29. * to reduce the complexity of code needed to interact with this type of buffer.
  30. */
  31. template <class T,size_t S>
  32. class RingBuffer
  33. {
  34. private:
  35. T buf[S];
  36. size_t begin;
  37. size_t end;
  38. bool wrap;
  39. public:
  40. inline RingBuffer() :
  41. begin(0),
  42. end(0),
  43. wrap(false)
  44. {
  45. memset(buf,0,sizeof(T)*S);
  46. }
  47. /**
  48. * @return A pointer to the underlying buffer
  49. */
  50. inline T *get_buf()
  51. {
  52. return buf + begin;
  53. }
  54. /**
  55. * Adjust buffer index pointer as if we copied data in
  56. * @param n Number of elements to copy in
  57. * @return Number of elements we copied in
  58. */
  59. inline size_t produce(size_t n)
  60. {
  61. n = std::min(n, getFree());
  62. if (n == 0) {
  63. return n;
  64. }
  65. const size_t first_chunk = std::min(n, S - end);
  66. end = (end + first_chunk) % S;
  67. if (first_chunk < n) {
  68. const size_t second_chunk = n - first_chunk;
  69. end = (end + second_chunk) % S;
  70. }
  71. if (begin == end) {
  72. wrap = true;
  73. }
  74. return n;
  75. }
  76. /**
  77. * Fast erase, O(1).
  78. * Merely reset the buffer pointer, doesn't erase contents
  79. */
  80. inline void reset() { consume(count()); }
  81. /**
  82. * adjust buffer index pointer as if we copied data out
  83. * @param n Number of elements we copied from the buffer
  84. * @return Number of elements actually available from the buffer
  85. */
  86. inline size_t consume(size_t n)
  87. {
  88. n = std::min(n, count());
  89. if (n == 0) {
  90. return n;
  91. }
  92. if (wrap) {
  93. wrap = false;
  94. }
  95. const size_t first_chunk = std::min(n, S - begin);
  96. begin = (begin + first_chunk) % S;
  97. if (first_chunk < n) {
  98. const size_t second_chunk = n - first_chunk;
  99. begin = (begin + second_chunk) % S;
  100. }
  101. return n;
  102. }
  103. /**
  104. * @param data Buffer that is to be written to the ring
  105. * @param n Number of elements to write to the buffer
  106. */
  107. inline size_t write(const T * data, size_t n)
  108. {
  109. n = std::min(n, getFree());
  110. if (n == 0) {
  111. return n;
  112. }
  113. const size_t first_chunk = std::min(n, S - end);
  114. memcpy(buf + end, data, first_chunk * sizeof(T));
  115. end = (end + first_chunk) % S;
  116. if (first_chunk < n) {
  117. const size_t second_chunk = n - first_chunk;
  118. memcpy(buf + end, data + first_chunk, second_chunk * sizeof(T));
  119. end = (end + second_chunk) % S;
  120. }
  121. if (begin == end) {
  122. wrap = true;
  123. }
  124. return n;
  125. }
  126. /**
  127. * Place a single value on the buffer. If the buffer is full, consume a value first.
  128. *
  129. * @param value A single value to be placed in the buffer
  130. */
  131. inline void push(const T value)
  132. {
  133. if (count() == S) {
  134. consume(1);
  135. }
  136. const size_t first_chunk = std::min((size_t)1, S - end);
  137. *(buf + end) = value;
  138. end = (end + first_chunk) % S;
  139. if (begin == end) {
  140. wrap = true;
  141. }
  142. }
  143. /**
  144. * @return The most recently pushed element on the buffer
  145. */
  146. inline T get_most_recent() { return *(buf + end); }
  147. /**
  148. * @param dest Destination buffer
  149. * @param n Size (in terms of number of elements) of the destination buffer
  150. * @return Number of elements read from the buffer
  151. */
  152. inline size_t read(T *dest,size_t n)
  153. {
  154. n = std::min(n, count());
  155. if (n == 0) {
  156. return n;
  157. }
  158. if (wrap) {
  159. wrap = false;
  160. }
  161. const size_t first_chunk = std::min(n, S - begin);
  162. memcpy(dest, buf + begin, first_chunk * sizeof(T));
  163. begin = (begin + first_chunk) % S;
  164. if (first_chunk < n) {
  165. const size_t second_chunk = n - first_chunk;
  166. memcpy(dest + first_chunk, buf + begin, second_chunk * sizeof(T));
  167. begin = (begin + second_chunk) % S;
  168. }
  169. return n;
  170. }
  171. /**
  172. * Return how many elements are in the buffer, O(1).
  173. *
  174. * @return The number of elements in the buffer
  175. */
  176. inline size_t count()
  177. {
  178. if (end == begin) {
  179. return wrap ? S : 0;
  180. }
  181. else if (end > begin) {
  182. return end - begin;
  183. }
  184. else {
  185. return S + end - begin;
  186. }
  187. }
  188. /**
  189. * @return The number of slots that are unused in the buffer
  190. */
  191. inline size_t getFree() { return S - count(); }
  192. /**
  193. * @return The arithmetic mean of the contents of the buffer
  194. */
  195. inline float mean()
  196. {
  197. size_t iterator = begin;
  198. float subtotal = 0;
  199. size_t curr_cnt = count();
  200. for (size_t i=0; i<curr_cnt; i++) {
  201. iterator = (iterator + S - 1) % curr_cnt;
  202. subtotal += (float)*(buf + iterator);
  203. }
  204. return curr_cnt ? subtotal / (float)curr_cnt : 0;
  205. }
  206. /**
  207. * @return The arithmetic mean of the most recent 'n' elements of the buffer
  208. */
  209. inline float mean(size_t n)
  210. {
  211. n = n < S ? n : S;
  212. size_t iterator = begin;
  213. float subtotal = 0;
  214. size_t curr_cnt = count();
  215. for (size_t i=0; i<n; i++) {
  216. iterator = (iterator + S - 1) % curr_cnt;
  217. subtotal += (float)*(buf + iterator);
  218. }
  219. return curr_cnt ? subtotal / (float)curr_cnt : 0;
  220. }
  221. };
  222. } // namespace ZeroTier
  223. #endif