From dc8fc1a33c3a74de7fbf42a1ad062e7588582f95 Mon Sep 17 00:00:00 2001 From: Christian Prochaska Date: Tue, 8 Oct 2013 19:09:30 +0200 Subject: [PATCH] Ring_buffer: add 'avail_capacity()' function The 'avail_capacity()' function returns how many more elements would currently fit into the ring buffer. Fixes #921. --- os/include/os/ring_buffer.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/os/include/os/ring_buffer.h b/os/include/os/ring_buffer.h index 392ef31fd0..60a9b99ad4 100644 --- a/os/include/os/ring_buffer.h +++ b/os/include/os/ring_buffer.h @@ -89,7 +89,18 @@ class Ring_buffer /** * Return true if ring buffer is empty */ - bool empty() { return _tail == _head; } + bool empty() const { return _tail == _head; } + + /** + * Return the remaining capacity + */ + int avail_capacity() const + { + if (_head >= _tail) + return QUEUE_SIZE - _head + _tail - 1; + else + return _tail - _head - 1; + } }; #endif /* _INCLUDE__OS__RING_BUFFER_H_ */