/src/rt/circular_buffer.cpp

http://github.com/jruderman/rust · C++ · 194 lines · 133 code · 30 blank · 31 comment · 17 complexity · 939d699fde671784aaf04b1a1d7ddf68 MD5 · raw file

  1. /*
  2. * A simple resizable circular buffer.
  3. */
  4. #include "circular_buffer.h"
  5. #include "rust_globals.h"
  6. #include "rust_kernel.h"
  7. circular_buffer::circular_buffer(rust_kernel *kernel, size_t unit_sz) :
  8. kernel(kernel),
  9. unit_sz(unit_sz),
  10. _buffer_sz(initial_size()),
  11. _next(0),
  12. _unread(0),
  13. _buffer((uint8_t *)kernel->malloc(_buffer_sz, "circular_buffer")) {
  14. assert(unit_sz && "Unit size must be larger than zero.");
  15. KLOG(kernel, mem, "new circular_buffer(buffer_sz=%d, unread=%d)"
  16. "-> circular_buffer=0x%" PRIxPTR,
  17. _buffer_sz, _unread, this);
  18. assert(_buffer && "Failed to allocate buffer.");
  19. }
  20. circular_buffer::~circular_buffer() {
  21. KLOG(kernel, mem, "~circular_buffer 0x%" PRIxPTR, this);
  22. assert(_buffer);
  23. assert(_unread == 0 && "didn't expect bytes in the circular buffer");
  24. kernel->free(_buffer);
  25. }
  26. size_t
  27. circular_buffer::initial_size() {
  28. assert(unit_sz > 0);
  29. return INITIAL_CIRCULAR_BUFFER_SIZE_IN_UNITS * unit_sz;
  30. }
  31. /**
  32. * Copies the unread data from this buffer to the "dst" address.
  33. */
  34. void
  35. circular_buffer::transfer(void *dst) {
  36. assert(dst);
  37. assert(_unread <= _buffer_sz);
  38. uint8_t *ptr = (uint8_t *) dst;
  39. // First copy from _next to either the end of the unread
  40. // items or the end of the buffer
  41. size_t head_sz;
  42. if (_next + _unread <= _buffer_sz) {
  43. head_sz = _unread;
  44. } else {
  45. head_sz = _buffer_sz - _next;
  46. }
  47. assert(_next + head_sz <= _buffer_sz);
  48. memcpy(ptr, _buffer + _next, head_sz);
  49. // Then copy any other items from the beginning of the buffer
  50. assert(_unread >= head_sz);
  51. size_t tail_sz = _unread - head_sz;
  52. assert(head_sz + tail_sz <= _buffer_sz);
  53. memcpy(ptr + head_sz, _buffer, tail_sz);
  54. }
  55. /**
  56. * Copies the data at the "src" address into this buffer. The buffer is
  57. * grown if it isn't large enough.
  58. */
  59. void
  60. circular_buffer::enqueue(void *src) {
  61. assert(src);
  62. assert(_unread <= _buffer_sz);
  63. assert(_buffer);
  64. // Grow if necessary.
  65. if (_unread == _buffer_sz) {
  66. grow();
  67. }
  68. KLOG(kernel, mem, "circular_buffer enqueue "
  69. "unread: %d, next: %d, buffer_sz: %d, unit_sz: %d",
  70. _unread, _next, _buffer_sz, unit_sz);
  71. assert(_unread < _buffer_sz);
  72. assert(_unread + unit_sz <= _buffer_sz);
  73. // Copy data
  74. size_t dst_idx = _next + _unread;
  75. assert(dst_idx >= _buffer_sz || dst_idx + unit_sz <= _buffer_sz);
  76. if (dst_idx >= _buffer_sz) {
  77. dst_idx -= _buffer_sz;
  78. assert(_next >= unit_sz);
  79. assert(dst_idx <= _next - unit_sz);
  80. }
  81. assert(dst_idx + unit_sz <= _buffer_sz);
  82. memcpy(&_buffer[dst_idx], src, unit_sz);
  83. _unread += unit_sz;
  84. KLOG(kernel, mem, "circular_buffer pushed data at index: %d", dst_idx);
  85. }
  86. /**
  87. * Copies data from this buffer to the "dst" address. The buffer is
  88. * shrunk if possible. If the "dst" address is NULL, then the message
  89. * is dequeued but is not copied.
  90. */
  91. void
  92. circular_buffer::dequeue(void *dst) {
  93. assert(unit_sz > 0);
  94. assert(_unread >= unit_sz);
  95. assert(_unread <= _buffer_sz);
  96. assert(_buffer);
  97. KLOG(kernel, mem,
  98. "circular_buffer dequeue "
  99. "unread: %d, next: %d, buffer_sz: %d, unit_sz: %d",
  100. _unread, _next, _buffer_sz, unit_sz);
  101. assert(_next + unit_sz <= _buffer_sz);
  102. if (dst != NULL) {
  103. memcpy(dst, &_buffer[_next], unit_sz);
  104. }
  105. KLOG(kernel, mem, "shifted data from index %d", _next);
  106. _unread -= unit_sz;
  107. _next += unit_sz;
  108. if (_next == _buffer_sz) {
  109. _next = 0;
  110. }
  111. // Shrink if possible.
  112. if (_buffer_sz > initial_size() && _unread <= _buffer_sz / 4) {
  113. shrink();
  114. }
  115. }
  116. void
  117. circular_buffer::grow() {
  118. size_t new_buffer_sz = _buffer_sz * 2;
  119. KLOG(kernel, mem, "circular_buffer is growing to %d bytes",
  120. new_buffer_sz);
  121. void *new_buffer = kernel->malloc(new_buffer_sz,
  122. "new circular_buffer (grow)");
  123. transfer(new_buffer);
  124. kernel->free(_buffer);
  125. _buffer = (uint8_t *)new_buffer;
  126. _next = 0;
  127. _buffer_sz = new_buffer_sz;
  128. }
  129. void
  130. circular_buffer::shrink() {
  131. size_t new_buffer_sz = _buffer_sz / 2;
  132. assert(initial_size() <= new_buffer_sz);
  133. KLOG(kernel, mem, "circular_buffer is shrinking to %d bytes",
  134. new_buffer_sz);
  135. void *new_buffer = kernel->malloc(new_buffer_sz,
  136. "new circular_buffer (shrink)");
  137. transfer(new_buffer);
  138. kernel->free(_buffer);
  139. _buffer = (uint8_t *)new_buffer;
  140. _next = 0;
  141. _buffer_sz = new_buffer_sz;
  142. }
  143. uint8_t *
  144. circular_buffer::peek() {
  145. return &_buffer[_next];
  146. }
  147. bool
  148. circular_buffer::is_empty() {
  149. return _unread == 0;
  150. }
  151. size_t
  152. circular_buffer::size() {
  153. return _unread;
  154. }
  155. //
  156. // Local Variables:
  157. // mode: C++
  158. // fill-column: 78;
  159. // indent-tabs-mode: nil
  160. // c-basic-offset: 4
  161. // buffer-file-coding-system: utf-8-unix
  162. // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
  163. // End:
  164. //