2010-07-19 16:05:18 -05:00
|
|
|
/*
|
|
|
|
* A simple resizable circular buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rust_internal.h"
|
|
|
|
|
2011-07-18 14:02:26 -05:00
|
|
|
circular_buffer::circular_buffer(rust_kernel *kernel, size_t unit_sz) :
|
|
|
|
kernel(kernel),
|
2010-07-28 01:51:04 -05:00
|
|
|
unit_sz(unit_sz),
|
2011-01-08 17:52:37 -06:00
|
|
|
_buffer_sz(initial_size()),
|
2010-07-19 16:05:18 -05:00
|
|
|
_next(0),
|
|
|
|
_unread(0),
|
2011-07-18 14:02:26 -05:00
|
|
|
_buffer((uint8_t *)kernel->malloc(_buffer_sz, "circular_buffer")) {
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
A(kernel, unit_sz, "Unit size must be larger than zero.");
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
KLOG(kernel, mem, "new circular_buffer(buffer_sz=%d, unread=%d)"
|
|
|
|
"-> circular_buffer=0x%" PRIxPTR,
|
|
|
|
_buffer_sz, _unread, this);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
A(kernel, _buffer, "Failed to allocate buffer.");
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
circular_buffer::~circular_buffer() {
|
2011-08-01 16:28:32 -05:00
|
|
|
KLOG(kernel, mem, "~circular_buffer 0x%" PRIxPTR, this);
|
|
|
|
I(kernel, _buffer);
|
|
|
|
W(kernel, _unread == 0,
|
|
|
|
"freeing circular_buffer with %d unread bytes", _unread);
|
2011-07-18 14:02:26 -05:00
|
|
|
kernel->free(_buffer);
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
|
2011-01-08 17:52:37 -06:00
|
|
|
size_t
|
|
|
|
circular_buffer::initial_size() {
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, unit_sz > 0);
|
2011-01-08 17:52:37 -06:00
|
|
|
return INITIAL_CIRCULAR_BUFFER_SIZE_IN_UNITS * unit_sz;
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
/**
|
|
|
|
* Copies the unread data from this buffer to the "dst" address.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
circular_buffer::transfer(void *dst) {
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, dst);
|
|
|
|
I(kernel, _unread <= _buffer_sz);
|
2011-01-08 17:19:55 -06:00
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
uint8_t *ptr = (uint8_t *) dst;
|
2011-01-08 17:19:55 -06:00
|
|
|
|
|
|
|
// First copy from _next to either the end of the unread
|
|
|
|
// items or the end of the buffer
|
|
|
|
size_t head_sz;
|
2011-01-08 17:52:37 -06:00
|
|
|
if (_next + _unread <= _buffer_sz) {
|
2011-01-08 17:19:55 -06:00
|
|
|
head_sz = _unread;
|
2011-01-08 17:52:37 -06:00
|
|
|
} else {
|
|
|
|
head_sz = _buffer_sz - _next;
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, _next + head_sz <= _buffer_sz);
|
2011-01-08 17:19:55 -06:00
|
|
|
memcpy(ptr, _buffer + _next, head_sz);
|
|
|
|
|
|
|
|
// Then copy any other items from the beginning of the buffer
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, _unread >= head_sz);
|
2011-01-08 17:19:55 -06:00
|
|
|
size_t tail_sz = _unread - head_sz;
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, head_sz + tail_sz <= _buffer_sz);
|
2011-01-08 17:19:55 -06:00
|
|
|
memcpy(ptr + head_sz, _buffer, tail_sz);
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the data at the "src" address into this buffer. The buffer is
|
|
|
|
* grown if it isn't large enough.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
circular_buffer::enqueue(void *src) {
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, src);
|
|
|
|
I(kernel, _unread <= _buffer_sz);
|
|
|
|
I(kernel, _buffer);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
// Grow if necessary.
|
2011-01-08 17:19:55 -06:00
|
|
|
if (_unread == _buffer_sz) {
|
2011-01-08 17:52:37 -06:00
|
|
|
grow();
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
KLOG(kernel, mem, "circular_buffer enqueue "
|
|
|
|
"unread: %d, next: %d, buffer_sz: %d, unit_sz: %d",
|
|
|
|
_unread, _next, _buffer_sz, unit_sz);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, _unread < _buffer_sz);
|
|
|
|
I(kernel, _unread + unit_sz <= _buffer_sz);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
// Copy data
|
2011-01-08 17:52:37 -06:00
|
|
|
size_t dst_idx = _next + _unread;
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, dst_idx >= _buffer_sz || dst_idx + unit_sz <= _buffer_sz);
|
2011-01-08 17:52:37 -06:00
|
|
|
if (dst_idx >= _buffer_sz) {
|
|
|
|
dst_idx -= _buffer_sz;
|
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, _next >= unit_sz);
|
|
|
|
I(kernel, dst_idx <= _next - unit_sz);
|
2011-01-08 17:19:55 -06:00
|
|
|
}
|
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, dst_idx + unit_sz <= _buffer_sz);
|
2011-01-08 17:52:37 -06:00
|
|
|
memcpy(&_buffer[dst_idx], src, unit_sz);
|
2010-07-28 01:51:04 -05:00
|
|
|
_unread += unit_sz;
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
KLOG(kernel, mem, "circular_buffer pushed data at index: %d", dst_idx);
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies data from this buffer to the "dst" address. The buffer is
|
2010-07-28 02:01:31 -05:00
|
|
|
* shrunk if possible. If the "dst" address is NULL, then the message
|
|
|
|
* is dequeued but is not copied.
|
2010-07-19 16:05:18 -05:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
circular_buffer::dequeue(void *dst) {
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, unit_sz > 0);
|
|
|
|
I(kernel, _unread >= unit_sz);
|
|
|
|
I(kernel, _unread <= _buffer_sz);
|
|
|
|
I(kernel, _buffer);
|
2010-07-19 19:33:50 -05:00
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
KLOG(kernel, mem,
|
|
|
|
"circular_buffer dequeue "
|
|
|
|
"unread: %d, next: %d, buffer_sz: %d, unit_sz: %d",
|
|
|
|
_unread, _next, _buffer_sz, unit_sz);
|
2010-07-28 02:01:31 -05:00
|
|
|
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, _next + unit_sz <= _buffer_sz);
|
2010-07-28 01:51:04 -05:00
|
|
|
if (dst != NULL) {
|
|
|
|
memcpy(dst, &_buffer[_next], unit_sz);
|
|
|
|
}
|
2011-08-01 16:28:32 -05:00
|
|
|
KLOG(kernel, mem, "shifted data from index %d", _next);
|
2010-07-28 01:51:04 -05:00
|
|
|
_unread -= unit_sz;
|
|
|
|
_next += unit_sz;
|
2010-07-19 16:05:18 -05:00
|
|
|
if (_next == _buffer_sz) {
|
|
|
|
_next = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shrink if possible.
|
2011-01-08 17:52:37 -06:00
|
|
|
if (_buffer_sz > initial_size() && _unread <= _buffer_sz / 4) {
|
|
|
|
shrink();
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-08 17:52:37 -06:00
|
|
|
void
|
|
|
|
circular_buffer::grow() {
|
|
|
|
size_t new_buffer_sz = _buffer_sz * 2;
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, new_buffer_sz <= MAX_CIRCULAR_BUFFER_SIZE);
|
|
|
|
KLOG(kernel, mem, "circular_buffer is growing to %d bytes",
|
|
|
|
new_buffer_sz);
|
2011-07-18 14:02:26 -05:00
|
|
|
void *new_buffer = kernel->malloc(new_buffer_sz,
|
|
|
|
"new circular_buffer (grow)");
|
2011-01-08 17:52:37 -06:00
|
|
|
transfer(new_buffer);
|
2011-07-18 14:02:26 -05:00
|
|
|
kernel->free(_buffer);
|
2011-01-08 17:52:37 -06:00
|
|
|
_buffer = (uint8_t *)new_buffer;
|
|
|
|
_next = 0;
|
|
|
|
_buffer_sz = new_buffer_sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
circular_buffer::shrink() {
|
|
|
|
size_t new_buffer_sz = _buffer_sz / 2;
|
2011-08-01 16:28:32 -05:00
|
|
|
I(kernel, initial_size() <= new_buffer_sz);
|
|
|
|
KLOG(kernel, mem, "circular_buffer is shrinking to %d bytes",
|
|
|
|
new_buffer_sz);
|
2011-07-18 14:02:26 -05:00
|
|
|
void *new_buffer = kernel->malloc(new_buffer_sz,
|
|
|
|
"new circular_buffer (shrink)");
|
2011-01-08 17:52:37 -06:00
|
|
|
transfer(new_buffer);
|
2011-07-18 14:02:26 -05:00
|
|
|
kernel->free(_buffer);
|
2011-01-08 17:52:37 -06:00
|
|
|
_buffer = (uint8_t *)new_buffer;
|
|
|
|
_next = 0;
|
|
|
|
_buffer_sz = new_buffer_sz;
|
|
|
|
}
|
|
|
|
|
2010-07-28 02:01:06 -05:00
|
|
|
uint8_t *
|
|
|
|
circular_buffer::peek() {
|
|
|
|
return &_buffer[_next];
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
bool
|
|
|
|
circular_buffer::is_empty() {
|
|
|
|
return _unread == 0;
|
|
|
|
}
|
2010-08-11 18:08:26 -05:00
|
|
|
|
|
|
|
size_t
|
|
|
|
circular_buffer::size() {
|
|
|
|
return _unread;
|
|
|
|
}
|
2011-01-08 17:19:55 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-07-13 15:51:20 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-01-08 17:19:55 -06:00
|
|
|
// End:
|
|
|
|
//
|