diff --git a/libraries/AP_HAL/utility/RingBuffer.cpp b/libraries/AP_HAL/utility/RingBuffer.cpp index 4116cfc7b9..0c9f36d236 100644 --- a/libraries/AP_HAL/utility/RingBuffer.cpp +++ b/libraries/AP_HAL/utility/RingBuffer.cpp @@ -41,6 +41,20 @@ bool ByteBuffer::set_size(uint32_t _size) return true; } +/* + set buffer size, accepting a smaller size if desired size isn't achievable + */ +bool ByteBuffer::set_size_best(uint32_t _size) +{ + while (_size > 0) { + if (set_size(_size)) { + return true; + } + _size = (3 * _size)/4; + } + return false; +} + uint32_t ByteBuffer::available(void) const { /* use a copy on stack to avoid race conditions of @tail being updated by diff --git a/libraries/AP_HAL/utility/RingBuffer.h b/libraries/AP_HAL/utility/RingBuffer.h index 08492cfadf..bddfc54283 100644 --- a/libraries/AP_HAL/utility/RingBuffer.h +++ b/libraries/AP_HAL/utility/RingBuffer.h @@ -52,6 +52,9 @@ public: // set size of ringbuffer, caller responsible for locking bool set_size(uint32_t size); + // set size of ringbuffer, reducing down if size can't be achieved + bool set_size_best(uint32_t size); + // advance the read pointer (discarding bytes) bool advance(uint32_t n);