RingBuffer.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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: 2026-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 <algorithm>
  16. #include <cstdint>
  17. #include <math.h>
  18. #include <memory.h>
  19. #include <stdlib.h>
  20. #include <typeinfo>
  21. namespace ZeroTier {
  22. /**
  23. * A circular buffer
  24. *
  25. * For fast handling of continuously-evolving variables (such as path quality metrics).
  26. * Using this, we can maintain longer sliding historical windows for important path
  27. * metrics without the need for potentially expensive calls to memcpy/memmove.
  28. *
  29. * Some basic statistical functionality is implemented here in an attempt
  30. * to reduce the complexity of code needed to interact with this type of buffer.
  31. */
  32. template <class T, size_t S> class RingBuffer {
  33. private:
  34. T buf[S];
  35. size_t begin;
  36. size_t end;
  37. bool wrap;
  38. public:
  39. RingBuffer() : begin(0), end(0), wrap(false)
  40. {
  41. memset(buf, 0, sizeof(T) * S);
  42. }
  43. /**
  44. * @return A pointer to the underlying buffer
  45. */
  46. inline T* get_buf()
  47. {
  48. return buf + begin;
  49. }
  50. /**
  51. * Adjust buffer index pointer as if we copied data in
  52. * @param n Number of elements to copy in
  53. * @return Number of elements we copied in
  54. */
  55. inline size_t produce(size_t n)
  56. {
  57. n = std::min(n, getFree());
  58. if (n == 0) {
  59. return n;
  60. }
  61. const size_t first_chunk = std::min(n, S - end);
  62. end = (end + first_chunk) % S;
  63. if (first_chunk < n) {
  64. const size_t second_chunk = n - first_chunk;
  65. end = (end + second_chunk) % S;
  66. }
  67. if (begin == end) {
  68. wrap = true;
  69. }
  70. return n;
  71. }
  72. /**
  73. * Fast erase, O(1).
  74. * Merely reset the buffer pointer, doesn't erase contents
  75. */
  76. inline void reset()
  77. {
  78. consume(count());
  79. }
  80. /**
  81. * adjust buffer index pointer as if we copied data out
  82. * @param n Number of elements we copied from the buffer
  83. * @return Number of elements actually available from the buffer
  84. */
  85. inline size_t consume(size_t n)
  86. {
  87. n = std::min(n, count());
  88. if (n == 0) {
  89. return n;
  90. }
  91. if (wrap) {
  92. wrap = false;
  93. }
  94. const size_t first_chunk = std::min(n, S - begin);
  95. begin = (begin + first_chunk) % S;
  96. if (first_chunk < n) {
  97. const size_t second_chunk = n - first_chunk;
  98. begin = (begin + second_chunk) % S;
  99. }
  100. return n;
  101. }
  102. /**
  103. * @param data Buffer that is to be written to the ring
  104. * @param n Number of elements to write to the buffer
  105. */
  106. inline size_t write(const T* data, size_t n)
  107. {
  108. n = std::min(n, getFree());
  109. if (n == 0) {
  110. return n;
  111. }
  112. const size_t first_chunk = std::min(n, S - end);
  113. memcpy(buf + end, data, first_chunk * sizeof(T));
  114. end = (end + first_chunk) % S;
  115. if (first_chunk < n) {
  116. const size_t second_chunk = n - first_chunk;
  117. memcpy(buf + end, data + first_chunk, second_chunk * sizeof(T));
  118. end = (end + second_chunk) % S;
  119. }
  120. if (begin == end) {
  121. wrap = true;
  122. }
  123. return n;
  124. }
  125. /**
  126. * Place a single value on the buffer. If the buffer is full, consume a value first.
  127. *
  128. * @param value A single value to be placed in the buffer
  129. */
  130. inline void push(const T value)
  131. {
  132. if (count() == S) {
  133. consume(1);
  134. }
  135. const size_t first_chunk = std::min((size_t)1, S - end);
  136. *(buf + end) = value;
  137. end = (end + first_chunk) % S;
  138. if (begin == end) {
  139. wrap = true;
  140. }
  141. }
  142. /**
  143. * @return The most recently pushed element on the buffer
  144. */
  145. inline T get_most_recent()
  146. {
  147. return *(buf + end);
  148. }
  149. /**
  150. * @param dest Destination buffer
  151. * @param n Size (in terms of number of elements) of the destination buffer
  152. * @return Number of elements read from the buffer
  153. */
  154. inline size_t read(T* dest, size_t n)
  155. {
  156. n = std::min(n, count());
  157. if (n == 0) {
  158. return n;
  159. }
  160. if (wrap) {
  161. wrap = false;
  162. }
  163. const size_t first_chunk = std::min(n, S - begin);
  164. memcpy(dest, buf + begin, first_chunk * sizeof(T));
  165. begin = (begin + first_chunk) % S;
  166. if (first_chunk < n) {
  167. const size_t second_chunk = n - first_chunk;
  168. memcpy(dest + first_chunk, buf + begin, second_chunk * sizeof(T));
  169. begin = (begin + second_chunk) % S;
  170. }
  171. return n;
  172. }
  173. /**
  174. * Return how many elements are in the buffer, O(1).
  175. *
  176. * @return The number of elements in the buffer
  177. */
  178. inline size_t count()
  179. {
  180. if (end == begin) {
  181. return wrap ? S : 0;
  182. }
  183. else if (end > begin) {
  184. return end - begin;
  185. }
  186. else {
  187. return S + end - begin;
  188. }
  189. }
  190. /**
  191. * @return The number of slots that are unused in the buffer
  192. */
  193. inline size_t getFree()
  194. {
  195. return S - count();
  196. }
  197. /**
  198. * @return The arithmetic mean of the contents of the buffer
  199. */
  200. inline float mean()
  201. {
  202. size_t iterator = begin;
  203. float subtotal = 0;
  204. size_t curr_cnt = count();
  205. for (size_t i = 0; i < curr_cnt; i++) {
  206. iterator = (iterator + S - 1) % curr_cnt;
  207. subtotal += (float)*(buf + iterator);
  208. }
  209. return curr_cnt ? subtotal / (float)curr_cnt : 0;
  210. }
  211. /**
  212. * @return The arithmetic mean of the most recent 'n' elements of the buffer
  213. */
  214. inline float mean(size_t n)
  215. {
  216. n = n < S ? n : S;
  217. size_t iterator = begin;
  218. float subtotal = 0;
  219. size_t curr_cnt = count();
  220. for (size_t i = 0; i < n; i++) {
  221. iterator = (iterator + S - 1) % curr_cnt;
  222. subtotal += (float)*(buf + iterator);
  223. }
  224. return curr_cnt ? subtotal / (float)curr_cnt : 0;
  225. }
  226. /**
  227. * @return The sum of the contents of the buffer
  228. */
  229. inline float sum()
  230. {
  231. size_t iterator = begin;
  232. float total = 0;
  233. size_t curr_cnt = count();
  234. for (size_t i = 0; i < curr_cnt; i++) {
  235. iterator = (iterator + S - 1) % curr_cnt;
  236. total += (float)*(buf + iterator);
  237. }
  238. return total;
  239. }
  240. /**
  241. * @return The sample standard deviation of element values
  242. */
  243. inline float stddev()
  244. {
  245. return sqrt(variance());
  246. }
  247. /**
  248. * @return The variance of element values
  249. */
  250. inline float variance()
  251. {
  252. size_t iterator = begin;
  253. float cached_mean = mean();
  254. size_t curr_cnt = count();
  255. T sum_of_squared_deviations = 0;
  256. for (size_t i = 0; i < curr_cnt; i++) {
  257. iterator = (iterator + S - 1) % curr_cnt;
  258. float deviation = (buf[i] - cached_mean);
  259. sum_of_squared_deviations += (T)(deviation * deviation);
  260. }
  261. float variance = (float)sum_of_squared_deviations / (float)(S - 1);
  262. return variance;
  263. }
  264. /**
  265. * @return The number of elements of zero value
  266. */
  267. inline size_t zeroCount()
  268. {
  269. size_t iterator = begin;
  270. size_t zeros = 0;
  271. size_t curr_cnt = count();
  272. for (size_t i = 0; i < curr_cnt; i++) {
  273. iterator = (iterator + S - 1) % curr_cnt;
  274. if (*(buf + iterator) == 0) {
  275. zeros++;
  276. }
  277. }
  278. return zeros;
  279. }
  280. /**
  281. * @param value Value to match against in buffer
  282. * @return The number of values held in the ring buffer which match a given value
  283. */
  284. inline size_t countValue(T value)
  285. {
  286. size_t iterator = begin;
  287. size_t cnt = 0;
  288. size_t curr_cnt = count();
  289. for (size_t i = 0; i < curr_cnt; i++) {
  290. iterator = (iterator + S - 1) % curr_cnt;
  291. if (*(buf + iterator) == value) {
  292. cnt++;
  293. }
  294. }
  295. return cnt;
  296. }
  297. /**
  298. * Print the contents of the buffer
  299. */
  300. /*
  301. inline void dump()
  302. {
  303. size_t iterator = begin;
  304. for (size_t i=0; i<S; i++) {
  305. iterator = (iterator + S - 1) % S;
  306. if (typeid(T) == typeid(int)) {
  307. fprintf(stderr, "buf[%2zu]=%2d\n", iterator, (int)*(buf + iterator));
  308. }
  309. else {
  310. fprintf(stderr, "buf[%2zu]=%2f\n", iterator, (float)*(buf + iterator));
  311. }
  312. }
  313. }
  314. */
  315. };
  316. } // namespace ZeroTier
  317. #endif