aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian Anderson <[email protected]>2011-01-07 23:09:16 -0500
committerGraydon Hoare <[email protected]>2011-01-10 11:31:33 -0800
commit97aa599b0d8cd19c6535c7a9e4a1e72d70c0c16f (patch)
treea2b1165f0abe4419f35ae4e3d5a4d62b3bd7bbac /src
parentRemove tabs (diff)
downloadrust-97aa599b0d8cd19c6535c7a9e4a1e72d70c0c16f.tar.xz
rust-97aa599b0d8cd19c6535c7a9e4a1e72d70c0c16f.zip
Fix circular_buffer growth when _next != 0
Diffstat (limited to 'src')
-rw-r--r--src/rt/circular_buffer.cpp13
-rw-r--r--src/test/run-pass/chan-poweroftwo.rs33
2 files changed, 36 insertions, 10 deletions
diff --git a/src/rt/circular_buffer.cpp b/src/rt/circular_buffer.cpp
index c3b068ac..54898fb7 100644
--- a/src/rt/circular_buffer.cpp
+++ b/src/rt/circular_buffer.cpp
@@ -63,7 +63,7 @@ circular_buffer::enqueue(void *src) {
I(dom, _unread <= _buffer_sz);
// Grow if necessary.
- if (_unread + unit_sz > _buffer_sz) {
+ if (_next + _unread + unit_sz > _buffer_sz) {
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);
@@ -76,15 +76,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: %d, next: %d, buffer_sz: %d, unit_sz: %d",
+ _unread, _next, _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, _next + _unread + unit_sz <= _buffer_sz);
// Copy data
size_t i = (_next + _unread) & (_buffer_sz - 1);
+ I(dom, i + unit_sz <= _buffer_sz);
memcpy(&_buffer[i], src, unit_sz);
_unread += unit_sz;
@@ -106,8 +107,8 @@ circular_buffer::dequeue(void *dst) {
dom->log(rust_log::MEM | rust_log::COMM,
"circular_buffer dequeue "
- "unread: %d, buffer_sz: %d, unit_sz: %d",
- _unread, _buffer_sz, unit_sz);
+ "unread: %d, next: %d, buffer_sz: %d, unit_sz: %d",
+ _unread, _next, _buffer_sz, unit_sz);
if (dst != NULL) {
memcpy(dst, &_buffer[_next], unit_sz);
diff --git a/src/test/run-pass/chan-poweroftwo.rs b/src/test/run-pass/chan-poweroftwo.rs
index be7c18ad..044d8238 100644
--- a/src/test/run-pass/chan-poweroftwo.rs
+++ b/src/test/run-pass/chan-poweroftwo.rs
@@ -10,7 +10,7 @@ import std._uint;
import std._vec;
// A 12-byte unit to send over the channel
-type record = rec(i32 val1, i32 val2, i32 val3);
+type record = rec(u32 val1, u32 val2, u32 val3);
// Assuming that the default buffer size needs to hold 8 units,
// then the minimum buffer size needs to be 96. That's not a
@@ -20,7 +20,7 @@ impure fn test_init() {
let port[record] myport = port();
auto mychan = chan(myport);
- let record val = rec(val1=0i32, val2=0i32, val3=0i32);
+ let record val = rec(val1=0u32, val2=0u32, val3=0u32);
mychan <| val;
}
@@ -31,7 +31,7 @@ impure fn test_grow() {
let port[record] myport = port();
auto mychan = chan(myport);
- let record val = rec(val1=0i32, val2=0i32, val3=0i32);
+ let record val = rec(val1=0u32, val2=0u32, val3=0u32);
for each (uint i in _uint.range(0u, 100u)) {
mychan <| val;
@@ -51,7 +51,7 @@ impure fn test_shrink2() {
let port[record] myport = port();
auto mychan = chan(myport);
- let record val = rec(val1=0i32, val2=0i32, val3=0i32);
+ let record val = rec(val1=0u32, val2=0u32, val3=0u32);
for each (uint i in _uint.range(0u, 100u)) {
mychan <| val;
@@ -62,11 +62,36 @@ impure fn test_shrink2() {
}
}
+// Test rotating the buffer when the unit size is not a power of two
+impure fn test_rotate() {
+ let port[record] myport = port();
+ auto mychan = chan(myport);
+
+ let record val = rec(val1=0u32, val2=0u32, val3=0u32);
+
+ for each (uint j in _uint.range(0u, 10u)) {
+ for each (uint i in _uint.range(0u, 10u)) {
+ let record val = rec(val1=i as u32,
+ val2=i as u32,
+ val3=i as u32);
+ mychan <| val;
+ }
+
+ for each (uint i in _uint.range(0u, 10u)) {
+ auto x <- myport;
+ check (x.val1 == i as u32);
+ check (x.val2 == i as u32);
+ check (x.val3 == i as u32);
+ }
+ }
+}
+
impure fn main() {
test_init();
test_grow();
test_shrink1();
test_shrink2();
+ test_rotate();
}
// Local Variables: