summaryrefslogtreecommitdiff
path: root/ucoo
diff options
context:
space:
mode:
authorNicolas Schodet2015-10-21 00:42:37 +0200
committerNicolas Schodet2019-10-07 00:44:50 +0200
commitf66a9863c2b5d29ee2d3c57b65a0aecc9656c59c (patch)
tree6814152e490144ccf7f2d86bf384e934798b0012 /ucoo
parent5487719b70fa89fdffae413b2d00f2a928cfc2a7 (diff)
ucoo/utils/fifo: ensure there is room for size elements
Diffstat (limited to 'ucoo')
-rw-r--r--ucoo/hal/uart/Config4
-rw-r--r--ucoo/utils/fifo.hh5
-rw-r--r--ucoo/utils/test/test_fifo.cc6
3 files changed, 7 insertions, 8 deletions
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<typename T, int size>
class Fifo
{
@@ -60,7 +59,7 @@ class Fifo
/// Return next index, use unsigned operation for optimisation.
int next (int index) const
{
- return (static_cast<unsigned int> (index) + 1) % size;
+ return (static_cast<unsigned int> (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<int, 16> fifo;
+ ucoo::Fifo<int, 15> 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<int, 4> fifo;
+ ucoo::Fifo<int, 3> 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<int, 8> fifo;
+ ucoo::Fifo<int, 7> fifo;
static const int b[] = { 1, 2, 3, 4, 5 };
int c[8];
int r;