From f66a9863c2b5d29ee2d3c57b65a0aecc9656c59c Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Wed, 21 Oct 2015 00:42:37 +0200 Subject: ucoo/utils/fifo: ensure there is room for size elements --- ucoo/hal/uart/Config | 4 ++-- ucoo/utils/fifo.hh | 5 ++--- ucoo/utils/test/test_fifo.cc | 6 +++--- 3 files changed, 7 insertions(+), 8 deletions(-) (limited to 'ucoo') diff --git a/ucoo/hal/uart/Config b/ucoo/hal/uart/Config index 1229b76..70ad3f7 100644 --- a/ucoo/hal/uart/Config +++ b/ucoo/hal/uart/Config @@ -1,6 +1,6 @@ [ucoo/hal/uart] # Size of reception buffer. -rx_buffer = 32 +rx_buffer = 31 # Size of transmission buffer. -tx_buffer = 32 +tx_buffer = 31 diff --git a/ucoo/utils/fifo.hh b/ucoo/utils/fifo.hh index 38c1e4f..2e55eb5 100644 --- a/ucoo/utils/fifo.hh +++ b/ucoo/utils/fifo.hh @@ -32,7 +32,6 @@ namespace ucoo { /// threads. /// /// This is a rather low level container used to implement ucoolib features. -/// Be careful that there is only room for size - 1 elements in the FIFO. template class Fifo { @@ -60,7 +59,7 @@ class Fifo /// Return next index, use unsigned operation for optimisation. int next (int index) const { - return (static_cast (index) + 1) % size; + return (static_cast (index) + 1) % (size + 1); } private: /// Index of the next element to pop, always incremented. @@ -69,7 +68,7 @@ class Fifo /// always incremented. int_atomic_t tail_; /// Memory to store elements. - T buffer_[size]; + T buffer_[size + 1]; }; } // namespace ucoo diff --git a/ucoo/utils/test/test_fifo.cc b/ucoo/utils/test/test_fifo.cc index dd9aa8e..027fdd5 100644 --- a/ucoo/utils/test/test_fifo.cc +++ b/ucoo/utils/test/test_fifo.cc @@ -32,7 +32,7 @@ main (int argc, const char **argv) ucoo::TestSuite tsuite ("fifo"); { ucoo::Test test (tsuite, "push pop"); - ucoo::Fifo fifo; + ucoo::Fifo fifo; fifo.push (1); fifo.push (2); fifo.push (3); @@ -41,7 +41,7 @@ main (int argc, const char **argv) } { ucoo::Test test (tsuite, "full empty"); - ucoo::Fifo fifo; + ucoo::Fifo fifo; do { test_fail_break_unless (test, fifo.empty () && !fifo.full ()); @@ -55,7 +55,7 @@ main (int argc, const char **argv) } { ucoo::Test test (tsuite, "write read"); - ucoo::Fifo fifo; + ucoo::Fifo fifo; static const int b[] = { 1, 2, 3, 4, 5 }; int c[8]; int r; -- cgit v1.2.3