From 625f47def736294ee32d9778e19be35494a819db Mon Sep 17 00:00:00 2001 From: Murilo Belluzzo Date: Thu, 23 Jun 2016 18:43:04 -0300 Subject: [PATCH] AP_HAL: Improve ByteBuffer::set_size So it doesn't delete and re-create the buffer if the size happens to be the same. Still resets the buffer content. --- libraries/AP_HAL/utility/RingBuffer.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libraries/AP_HAL/utility/RingBuffer.cpp b/libraries/AP_HAL/utility/RingBuffer.cpp index f9151c6fec..2608b168be 100644 --- a/libraries/AP_HAL/utility/RingBuffer.cpp +++ b/libraries/AP_HAL/utility/RingBuffer.cpp @@ -19,15 +19,19 @@ ByteBuffer::~ByteBuffer(void) } /* - caller is responsible for locking in set_size() + * Caller is responsible for locking in set_size() */ void ByteBuffer::set_size(uint32_t _size) { - uint8_t *oldbuf = buf; + uint8_t *oldbuf; + head = tail = 0; - size = _size; - buf = new uint8_t[size]; - delete [] oldbuf; + if (_size != size) { + size = _size; + oldbuf = buf; + buf = new uint8_t[size]; + delete[] oldbuf; + } } uint32_t ByteBuffer::available(void) const