aboutsummaryrefslogtreecommitdiff
path: root/src/rt
diff options
context:
space:
mode:
authorMichael Bebenita <[email protected]>2010-07-27 23:38:16 -0700
committerGraydon Hoare <[email protected]>2010-07-28 20:30:28 -0700
commit60b0486c11f3c88e0a9aa13986ea338a54468bca (patch)
treeb73975eac835da03a61145a73b17f72c99a86d1a /src/rt
parentAdd a warning interface to rust_srv. (diff)
downloadrust-60b0486c11f3c88e0a9aa13986ea338a54468bca.tar.xz
rust-60b0486c11f3c88e0a9aa13986ea338a54468bca.zip
Make circular buffer use only power-of-two sizes, cheaper arithmetic.
Diffstat (limited to 'src/rt')
-rw-r--r--src/rt/circular_buffer.cpp14
-rw-r--r--src/rt/circular_buffer.h4
2 files changed, 15 insertions, 3 deletions
diff --git a/src/rt/circular_buffer.cpp b/src/rt/circular_buffer.cpp
index 415a966b..ee9cc58e 100644
--- a/src/rt/circular_buffer.cpp
+++ b/src/rt/circular_buffer.cpp
@@ -4,6 +4,14 @@
#include "rust_internal.h"
+bool
+is_power_of_two(size_t value) {
+ if (value > 0) {
+ return (value & (value - 1)) == 0;
+ }
+ return false;
+}
+
circular_buffer::circular_buffer(rust_dom *dom, size_t unit_sz) :
dom(dom),
_buffer_sz(INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS * unit_sz),
@@ -37,9 +45,10 @@ circular_buffer::~circular_buffer() {
void
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], _unit_sz);
+ memcpy(&ptr[i], &_buffer[(_next + i) & (_buffer_sz - 1)], _unit_sz);
}
}
@@ -67,11 +76,12 @@ circular_buffer::enqueue(void *src) {
"unread: %d, buffer_sz: %d, unit_sz: %d",
_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);
// Copy data
- size_t i = (_next + _unread) % _buffer_sz;
+ size_t i = (_next + _unread) & (_buffer_sz - 1);
memcpy(&_buffer[i], src, _unit_sz);
_unread += _unit_sz;
diff --git a/src/rt/circular_buffer.h b/src/rt/circular_buffer.h
index 2ebf23b0..3d11b37f 100644
--- a/src/rt/circular_buffer.h
+++ b/src/rt/circular_buffer.h
@@ -20,7 +20,9 @@ public:
bool is_empty();
private:
- // Size of the buffer in bytes.
+ // Size of the buffer in bytes, should always be a power of two so that
+ // modulo arithmetic (x % _buffer_sz) can optimized away with
+ // (x & (_buffer_sz - 1)).
size_t _buffer_sz;
// Size of the data unit in bytes.