Small library for using circular buffers (forked from François Berder's implementation in order to add methods and fix problems)
Dependents: CircularBufferTest XBeeApi
Fork of CircularBuffer by
Test suite can be found in this application, CircularBufferTest
Diff: CircularBuffer.h
- Revision:
- 7:e2d532183250
- Parent:
- 6:242d0e9f13d1
- Child:
- 8:d318a6948091
diff -r 242d0e9f13d1 -r e2d532183250 CircularBuffer.h --- a/CircularBuffer.h Sat Jan 18 18:01:03 2014 +0000 +++ b/CircularBuffer.h Sat Jan 18 22:32:30 2014 +0000 @@ -72,6 +72,16 @@ */ bool isFull() const; + /* Remove the specified number of bytes from the front of the buffer + This is more efficient than using read() to access the bytes then + junking them. + + \param length The number of bytes to remove. If the specified + number of bytes don't exist in the buffer, the buffer will + just be emptied + */ + void chomp( uint32_t length ); + private : typedef uint16_t CircularBufferIndex_t; @@ -89,6 +99,17 @@ } template<size_t T> +void CircularBuffer<T>::chomp(uint32_t length) +{ + if( length >= getSize() ) + { + readIndex = writeIndex; + } else { + readIndex = (readIndex + length) % T; + } +} + +template<size_t T> uint32_t CircularBuffer<T>::read(uint8_t *data, uint32_t length) { uint32_t n = 0; @@ -107,7 +128,7 @@ { uint32_t n = 0; CircularBufferIndex_t src = readIndex; - while(n < length && getSize() > 0) + while((n < length ) && (n < getSize())) { if(src == T) { src = 0;