PageRenderTime 37ms CodeModel.GetById 16ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

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