From b856d26087a82caaddc139d979558db45553b8fa Mon Sep 17 00:00:00 2001 From: Murilo Belluzzo Date: Mon, 1 Aug 2016 22:48:35 -0300 Subject: [PATCH] RingBuffer: ::set_size now returns true or false --- libraries/AP_HAL/utility/RingBuffer.cpp | 16 ++++++++++------ libraries/AP_HAL/utility/RingBuffer.h | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/libraries/AP_HAL/utility/RingBuffer.cpp b/libraries/AP_HAL/utility/RingBuffer.cpp index f281fa1bf7..db4735150e 100644 --- a/libraries/AP_HAL/utility/RingBuffer.cpp +++ b/libraries/AP_HAL/utility/RingBuffer.cpp @@ -17,17 +17,21 @@ ByteBuffer::~ByteBuffer(void) /* * Caller is responsible for locking in set_size() */ -void ByteBuffer::set_size(uint32_t _size) +bool ByteBuffer::set_size(uint32_t _size) { - uint8_t *oldbuf; - head = tail = 0; if (_size != size) { - oldbuf = buf; + free(buf); buf = (uint8_t*)malloc(_size); - size = buf ? _size : 0; - free(oldbuf); + if (!buf) { + size = 0; + return false; + } + + size = _size; } + + return true; } uint32_t ByteBuffer::available(void) const diff --git a/libraries/AP_HAL/utility/RingBuffer.h b/libraries/AP_HAL/utility/RingBuffer.h index c7bca44163..f8d5636de0 100644 --- a/libraries/AP_HAL/utility/RingBuffer.h +++ b/libraries/AP_HAL/utility/RingBuffer.h @@ -60,7 +60,7 @@ public: uint32_t get_size(void) const { return size; } // set size of ringbuffer, caller responsible for locking - void set_size(uint32_t size); + bool set_size(uint32_t size); // advance the read pointer (discarding bytes) bool advance(uint32_t n);