2010-07-19 16:05:18 -05:00
|
|
|
/*
|
|
|
|
* A simple resizable circular buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rust_internal.h"
|
|
|
|
|
2010-07-28 01:38:16 -05:00
|
|
|
bool
|
|
|
|
is_power_of_two(size_t value) {
|
|
|
|
if (value > 0) {
|
|
|
|
return (value & (value - 1)) == 0;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
circular_buffer::circular_buffer(rust_dom *dom, size_t unit_sz) :
|
|
|
|
dom(dom),
|
2010-07-28 01:51:04 -05:00
|
|
|
unit_sz(unit_sz),
|
2010-07-19 16:05:18 -05:00
|
|
|
_buffer_sz(INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS * unit_sz),
|
|
|
|
_next(0),
|
|
|
|
_unread(0),
|
|
|
|
_buffer((uint8_t *)dom->calloc(_buffer_sz)) {
|
|
|
|
|
|
|
|
A(dom, unit_sz, "Unit size must be larger than zero.");
|
|
|
|
|
|
|
|
dom->log(rust_log::MEM | rust_log::COMM,
|
|
|
|
"new circular_buffer(buffer_sz=%d, unread=%d)"
|
|
|
|
"-> circular_buffer=0x%" PRIxPTR,
|
|
|
|
_buffer_sz, _unread, this);
|
|
|
|
|
|
|
|
A(dom, _buffer, "Failed to allocate buffer.");
|
|
|
|
}
|
|
|
|
|
|
|
|
circular_buffer::~circular_buffer() {
|
2010-08-09 09:52:07 -05:00
|
|
|
dom->log(rust_log::MEM, "~circular_buffer 0x%" PRIxPTR, this);
|
2010-07-19 16:05:18 -05:00
|
|
|
I(dom, _buffer);
|
2010-08-16 16:13:08 -05:00
|
|
|
W(dom, _unread == 0, "~circular_buffer with %d unread bytes", _unread);
|
2010-07-19 16:05:18 -05:00
|
|
|
dom->free(_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the unread data from this buffer to the "dst" address.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
circular_buffer::transfer(void *dst) {
|
|
|
|
I(dom, dst);
|
2010-07-28 01:38:16 -05:00
|
|
|
I(dom, is_power_of_two(_buffer_sz));
|
2010-07-19 16:05:18 -05:00
|
|
|
uint8_t *ptr = (uint8_t *) dst;
|
2010-07-28 01:51:04 -05:00
|
|
|
for (size_t i = 0; i < _unread; i += unit_sz) {
|
|
|
|
memcpy(&ptr[i], &_buffer[(_next + i) & (_buffer_sz - 1)], unit_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) {
|
|
|
|
I(dom, src);
|
|
|
|
I(dom, _unread <= _buffer_sz);
|
|
|
|
|
|
|
|
// Grow if necessary.
|
|
|
|
if (_unread == _buffer_sz) {
|
2010-07-28 02:03:47 -05:00
|
|
|
size_t new_buffer_sz = _buffer_sz << 1;
|
|
|
|
I(dom, new_buffer_sz <= MAX_CIRCULAR_BUFFFER_SIZE);
|
|
|
|
void *new_buffer = dom->malloc(new_buffer_sz);
|
|
|
|
transfer(new_buffer);
|
2010-07-19 16:05:18 -05:00
|
|
|
dom->free(_buffer);
|
2010-07-28 02:03:47 -05:00
|
|
|
_buffer = (uint8_t *)new_buffer;
|
|
|
|
_next = 0;
|
|
|
|
_buffer_sz = new_buffer_sz;
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dom->log(rust_log::MEM | rust_log::COMM,
|
|
|
|
"circular_buffer enqueue "
|
|
|
|
"unread: %d, buffer_sz: %d, unit_sz: %d",
|
2010-07-28 01:51:04 -05:00
|
|
|
_unread, _buffer_sz, unit_sz);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2010-07-28 01:38:16 -05:00
|
|
|
I(dom, is_power_of_two(_buffer_sz));
|
2010-07-19 16:05:18 -05:00
|
|
|
I(dom, _unread < _buffer_sz);
|
2010-07-28 01:51:04 -05:00
|
|
|
I(dom, _unread + unit_sz <= _buffer_sz);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
// Copy data
|
2010-07-28 01:38:16 -05:00
|
|
|
size_t i = (_next + _unread) & (_buffer_sz - 1);
|
2010-07-28 01:51:04 -05:00
|
|
|
memcpy(&_buffer[i], src, unit_sz);
|
|
|
|
_unread += unit_sz;
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
dom->log(rust_log::MEM | rust_log::COMM,
|
|
|
|
"circular_buffer pushed data at index: %d", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2010-07-28 01:51:04 -05:00
|
|
|
I(dom, unit_sz > 0);
|
|
|
|
I(dom, _unread >= unit_sz);
|
2010-07-19 16:05:18 -05:00
|
|
|
I(dom, _unread <= _buffer_sz);
|
|
|
|
I(dom, _buffer);
|
2010-07-19 19:33:50 -05:00
|
|
|
|
2010-07-28 02:01:31 -05:00
|
|
|
dom->log(rust_log::MEM | rust_log::COMM,
|
|
|
|
"circular_buffer dequeue "
|
|
|
|
"unread: %d, buffer_sz: %d, unit_sz: %d",
|
|
|
|
_unread, _buffer_sz, unit_sz);
|
|
|
|
|
2010-07-28 01:51:04 -05:00
|
|
|
if (dst != NULL) {
|
|
|
|
memcpy(dst, &_buffer[_next], unit_sz);
|
|
|
|
}
|
2010-07-19 16:05:18 -05:00
|
|
|
dom->log(rust_log::MEM | rust_log::COMM,
|
2010-07-19 19:33:50 -05:00
|
|
|
"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
|
|
|
I(dom, _next <= _buffer_sz);
|
|
|
|
if (_next == _buffer_sz) {
|
|
|
|
_next = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shrink if possible.
|
2010-07-28 01:51:04 -05:00
|
|
|
if (_buffer_sz >= INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS * unit_sz &&
|
2010-07-19 16:05:18 -05:00
|
|
|
_unread <= _buffer_sz / 4) {
|
2010-07-19 19:33:50 -05:00
|
|
|
dom->log(rust_log::MEM | rust_log::COMM,
|
|
|
|
"circular_buffer is shrinking to %d bytes", _buffer_sz / 2);
|
2010-07-19 16:05:18 -05:00
|
|
|
void *tmp = dom->malloc(_buffer_sz / 2);
|
|
|
|
transfer(tmp);
|
|
|
|
_buffer_sz >>= 1;
|
|
|
|
dom->free(_buffer);
|
|
|
|
_buffer = (uint8_t *)tmp;
|
2010-07-19 19:33:50 -05:00
|
|
|
_next = 0;
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
2010-07-19 19:33:50 -05:00
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|