Change _unit_sz to unit_sz and make public in circular_buffer.

This commit is contained in:
Michael Bebenita 2010-07-27 23:51:04 -07:00 committed by Graydon Hoare
parent 60b0486c11
commit 17fa6bfd9a
2 changed files with 17 additions and 17 deletions

View File

@ -14,8 +14,8 @@ is_power_of_two(size_t value) {
circular_buffer::circular_buffer(rust_dom *dom, size_t unit_sz) :
dom(dom),
unit_sz(unit_sz),
_buffer_sz(INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS * unit_sz),
_unit_sz(unit_sz),
_next(0),
_unread(0),
_buffer((uint8_t *)dom->calloc(_buffer_sz)) {
@ -47,8 +47,8 @@ circular_buffer::transfer(void *dst) {
I(dom, dst);
I(dom, is_power_of_two(_buffer_sz));
uint8_t *ptr = (uint8_t *) dst;
for (size_t i = 0; i < _unread; i += _unit_sz) {
memcpy(&ptr[i], &_buffer[(_next + i) & (_buffer_sz - 1)], _unit_sz);
for (size_t i = 0; i < _unread; i += unit_sz) {
memcpy(&ptr[i], &_buffer[(_next + i) & (_buffer_sz - 1)], unit_sz);
}
}
@ -74,16 +74,16 @@ circular_buffer::enqueue(void *src) {
dom->log(rust_log::MEM | rust_log::COMM,
"circular_buffer enqueue "
"unread: %d, buffer_sz: %d, unit_sz: %d",
_unread, _buffer_sz, _unit_sz);
_unread, _buffer_sz, unit_sz);
I(dom, is_power_of_two(_buffer_sz));
I(dom, _unread < _buffer_sz);
I(dom, _unread + _unit_sz <= _buffer_sz);
I(dom, _unread + unit_sz <= _buffer_sz);
// Copy data
size_t i = (_next + _unread) & (_buffer_sz - 1);
memcpy(&_buffer[i], src, _unit_sz);
_unread += _unit_sz;
memcpy(&_buffer[i], src, unit_sz);
_unread += unit_sz;
dom->log(rust_log::MEM | rust_log::COMM,
"circular_buffer pushed data at index: %d", i);
@ -95,24 +95,25 @@ circular_buffer::enqueue(void *src) {
*/
void
circular_buffer::dequeue(void *dst) {
I(dom, dst);
I(dom, _unit_sz > 0);
I(dom, _unread >= _unit_sz);
I(dom, unit_sz > 0);
I(dom, _unread >= unit_sz);
I(dom, _unread <= _buffer_sz);
I(dom, _buffer);
memcpy(dst, &_buffer[_next], _unit_sz);
if (dst != NULL) {
memcpy(dst, &_buffer[_next], unit_sz);
}
dom->log(rust_log::MEM | rust_log::COMM,
"shifted data from index %d", _next);
_unread -= _unit_sz;
_next += _unit_sz;
_unread -= unit_sz;
_next += unit_sz;
I(dom, _next <= _buffer_sz);
if (_next == _buffer_sz) {
_next = 0;
}
// Shrink if possible.
if (_buffer_sz >= INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS * _unit_sz &&
if (_buffer_sz >= INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS * unit_sz &&
_unread <= _buffer_sz / 4) {
dom->log(rust_log::MEM | rust_log::COMM,
"circular_buffer is shrinking to %d bytes", _buffer_sz / 2);

View File

@ -12,6 +12,8 @@ circular_buffer : public dom_owned<circular_buffer> {
public:
rust_dom *dom;
// Size of the data unit in bytes.
const size_t unit_sz;
circular_buffer(rust_dom *dom, size_t unit_sz);
~circular_buffer();
void transfer(void *dst);
@ -25,9 +27,6 @@ private:
// (x & (_buffer_sz - 1)).
size_t _buffer_sz;
// Size of the data unit in bytes.
size_t _unit_sz;
// Byte offset within the buffer where to read the next unit of data.
size_t _next;