diff --git a/libraries/AP_HAL/utility/RingBuffer.cpp b/libraries/AP_HAL/utility/RingBuffer.cpp index 620712323f..f9151c6fec 100644 --- a/libraries/AP_HAL/utility/RingBuffer.cpp +++ b/libraries/AP_HAL/utility/RingBuffer.cpp @@ -176,18 +176,14 @@ uint32_t ByteBuffer::read(uint8_t *data, uint32_t len) } /* - return a pointer to a contiguous read buffer + * Returns the pointer and size to a contiguous read in the buffer */ const uint8_t *ByteBuffer::readptr(uint32_t &available_bytes) { - available_bytes = available(); - if (available_bytes == 0) { - return nullptr; - } - if (head+available_bytes > size) { - available_bytes = size - head; - } - return &buf[head]; + uint32_t _tail = tail; + available_bytes = (head > _tail) ? size - head : _tail - head; + + return available_bytes ? &buf[head] : nullptr; } int16_t ByteBuffer::peek(uint32_t ofs) const diff --git a/libraries/AP_HAL/utility/RingBuffer.h b/libraries/AP_HAL/utility/RingBuffer.h index eb63eff081..b821f14ea1 100644 --- a/libraries/AP_HAL/utility/RingBuffer.h +++ b/libraries/AP_HAL/utility/RingBuffer.h @@ -61,7 +61,7 @@ public: // advance the read pointer (discarding bytes) bool advance(uint32_t n); - // return a pointer to the next available data + // Returns the pointer and size to a contiguous read of the next available data const uint8_t *readptr(uint32_t &available_bytes); // peek one byte without advancing read pointer. Return byte