Ring_buffer: add 'avail_capacity()' function

The 'avail_capacity()' function returns how many more elements would
currently fit into the ring buffer.

Fixes #921.
This commit is contained in:
Christian Prochaska
2013-10-08 19:09:30 +02:00
committed by Christian Helmuth
parent 7296735816
commit dc8fc1a33c

View File

@ -89,7 +89,18 @@ class Ring_buffer
/** /**
* Return true if ring buffer is empty * 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_ */ #endif /* _INCLUDE__OS__RING_BUFFER_H_ */