From d030f2888b89030a46d21003fbc1b821462d4ee3 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 18 Jan 2018 09:37:01 +1100 Subject: [PATCH] AP_HAL: added multi-object push to RingBuffer this is much more efficient than pushing them one at a time --- libraries/AP_HAL/utility/RingBuffer.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/libraries/AP_HAL/utility/RingBuffer.h b/libraries/AP_HAL/utility/RingBuffer.h index 3855bee4ba..9cb24fcb49 100644 --- a/libraries/AP_HAL/utility/RingBuffer.h +++ b/libraries/AP_HAL/utility/RingBuffer.h @@ -133,6 +133,14 @@ public: return buffer->write((uint8_t*)&object, sizeof(T)) == sizeof(T); } + // push N objects + bool push(const T *object, uint32_t n) { + if (buffer->space() < n*sizeof(T)) { + return false; + } + return buffer->write((uint8_t*)object, n*sizeof(T)) == n*sizeof(T); + } + /* throw away an object */ @@ -162,6 +170,17 @@ public: return push(object); } + /* + * push_force() N objects + */ + bool push_force(const T *object, uint32_t n) { + uint32_t _space = buffer->space(); + if (_space < sizeof(T)*n) { + buffer->advance(sizeof(T)*(n-_space)); + } + return push(object, n); + } + /* peek copies an object out without advancing the read pointer */