renormalize files

This commit is contained in:
Robin Müller 2020-09-04 15:04:53 +02:00
parent 543e2f7d5d
commit d885dddee8
2 changed files with 263 additions and 263 deletions

View File

@ -1,135 +1,135 @@
#include "../container/SimpleRingBuffer.h" #include "../container/SimpleRingBuffer.h"
#include <cstring> #include <cstring>
SimpleRingBuffer::SimpleRingBuffer(const size_t size, bool overwriteOld, SimpleRingBuffer::SimpleRingBuffer(const size_t size, bool overwriteOld,
size_t maxExcessBytes) : size_t maxExcessBytes) :
RingBufferBase<>(0, size, overwriteOld), RingBufferBase<>(0, size, overwriteOld),
maxExcessBytes(maxExcessBytes) { maxExcessBytes(maxExcessBytes) {
if(maxExcessBytes > size) { if(maxExcessBytes > size) {
this->maxExcessBytes = size; this->maxExcessBytes = size;
} }
else { else {
this->maxExcessBytes = maxExcessBytes; this->maxExcessBytes = maxExcessBytes;
} }
buffer = new uint8_t[size + maxExcessBytes]; buffer = new uint8_t[size + maxExcessBytes];
} }
SimpleRingBuffer::SimpleRingBuffer(uint8_t *buffer, const size_t size, SimpleRingBuffer::SimpleRingBuffer(uint8_t *buffer, const size_t size,
bool overwriteOld, size_t maxExcessBytes): bool overwriteOld, size_t maxExcessBytes):
RingBufferBase<>(0, size, overwriteOld), buffer(buffer) { RingBufferBase<>(0, size, overwriteOld), buffer(buffer) {
if(maxExcessBytes > size) { if(maxExcessBytes > size) {
this->maxExcessBytes = size; this->maxExcessBytes = size;
} }
else { else {
this->maxExcessBytes = maxExcessBytes; this->maxExcessBytes = maxExcessBytes;
} }
} }
SimpleRingBuffer::~SimpleRingBuffer() { SimpleRingBuffer::~SimpleRingBuffer() {
delete[] buffer; delete[] buffer;
} }
ReturnValue_t SimpleRingBuffer::getFreeElement(uint8_t **writePointer, ReturnValue_t SimpleRingBuffer::getFreeElement(uint8_t **writePointer,
size_t amount) { size_t amount) {
if ((availableWriteSpace() >= amount) or overwriteOld) { if (availableWriteSpace() >= amount or overwriteOld) {
size_t amountTillWrap = writeTillWrap(); size_t amountTillWrap = writeTillWrap();
if (amountTillWrap < amount) { if (amountTillWrap < amount) {
if((amount - amountTillWrap + excessBytes) > maxExcessBytes) { if((amount - amountTillWrap + excessBytes) > maxExcessBytes) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
excessBytes = amount - amountTillWrap; excessBytes = amount - amountTillWrap;
} }
*writePointer = &buffer[write]; *writePointer = &buffer[write];
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
else { else {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
void SimpleRingBuffer::confirmBytesWritten(size_t amount) { void SimpleRingBuffer::confirmBytesWritten(size_t amount) {
if(getExcessBytes() > 0) { if(getExcessBytes() > 0) {
moveExcessBytesToStart(); moveExcessBytesToStart();
} }
incrementWrite(amount); incrementWrite(amount);
} }
ReturnValue_t SimpleRingBuffer::writeData(const uint8_t* data, ReturnValue_t SimpleRingBuffer::writeData(const uint8_t* data,
size_t amount) { size_t amount) {
if ((availableWriteSpace() >= amount) or overwriteOld) { if (availableWriteSpace() >= amount or overwriteOld) {
size_t amountTillWrap = writeTillWrap(); size_t amountTillWrap = writeTillWrap();
if (amountTillWrap >= amount) { if (amountTillWrap >= amount) {
// remaining size in buffer is sufficient to fit full amount. // remaining size in buffer is sufficient to fit full amount.
memcpy(&buffer[write], data, amount); memcpy(&buffer[write], data, amount);
} }
else { else {
memcpy(&buffer[write], data, amountTillWrap); memcpy(&buffer[write], data, amountTillWrap);
memcpy(buffer, data + amountTillWrap, amount - amountTillWrap); memcpy(buffer, data + amountTillWrap, amount - amountTillWrap);
} }
incrementWrite(amount); incrementWrite(amount);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { } else {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
ReturnValue_t SimpleRingBuffer::readData(uint8_t* data, size_t amount, ReturnValue_t SimpleRingBuffer::readData(uint8_t* data, size_t amount,
bool incrementReadPtr, bool readRemaining, size_t* trueAmount) { bool incrementReadPtr, bool readRemaining, size_t* trueAmount) {
size_t availableData = getAvailableReadData(READ_PTR); size_t availableData = getAvailableReadData(READ_PTR);
size_t amountTillWrap = readTillWrap(READ_PTR); size_t amountTillWrap = readTillWrap(READ_PTR);
if (availableData < amount) { if (availableData < amount) {
if (readRemaining) { if (readRemaining) {
// more data available than amount specified. // more data available than amount specified.
amount = availableData; amount = availableData;
} else { } else {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
if (trueAmount != nullptr) { if (trueAmount != nullptr) {
*trueAmount = amount; *trueAmount = amount;
} }
if (amountTillWrap >= amount) { if (amountTillWrap >= amount) {
memcpy(data, &buffer[read[READ_PTR]], amount); memcpy(data, &buffer[read[READ_PTR]], amount);
} else { } else {
memcpy(data, &buffer[read[READ_PTR]], amountTillWrap); memcpy(data, &buffer[read[READ_PTR]], amountTillWrap);
memcpy(data + amountTillWrap, buffer, amount - amountTillWrap); memcpy(data + amountTillWrap, buffer, amount - amountTillWrap);
} }
if(incrementReadPtr) { if(incrementReadPtr) {
deleteData(amount, readRemaining); deleteData(amount, readRemaining);
} }
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
size_t SimpleRingBuffer::getExcessBytes() const { size_t SimpleRingBuffer::getExcessBytes() const {
return excessBytes; return excessBytes;
} }
void SimpleRingBuffer::moveExcessBytesToStart() { void SimpleRingBuffer::moveExcessBytesToStart() {
if(excessBytes > 0) { if(excessBytes > 0) {
std::memcpy(buffer, &buffer[size], excessBytes); std::memcpy(buffer, &buffer[size], excessBytes);
excessBytes = 0; excessBytes = 0;
} }
} }
ReturnValue_t SimpleRingBuffer::deleteData(size_t amount, ReturnValue_t SimpleRingBuffer::deleteData(size_t amount,
bool deleteRemaining, size_t* trueAmount) { bool deleteRemaining, size_t* trueAmount) {
size_t availableData = getAvailableReadData(READ_PTR); size_t availableData = getAvailableReadData(READ_PTR);
if (availableData < amount) { if (availableData < amount) {
if (deleteRemaining) { if (deleteRemaining) {
amount = availableData; amount = availableData;
} else { } else {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
if (trueAmount != nullptr) { if (trueAmount != nullptr) {
*trueAmount = amount; *trueAmount = amount;
} }
incrementRead(amount, READ_PTR); incrementRead(amount, READ_PTR);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }

View File

@ -1,128 +1,128 @@
#ifndef FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ #ifndef FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_
#define FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ #define FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_
#include "../container/RingBufferBase.h" #include "../container/RingBufferBase.h"
#include <cstddef> #include <cstddef>
/** /**
* @brief Circular buffer implementation, useful for buffering * @brief Circular buffer implementation, useful for buffering
* into data streams. * into data streams.
* @details * @details
* Note that the deleteData() has to be called to increment the read pointer. * Note that the deleteData() has to be called to increment the read pointer.
* This class allocated dynamically, so * This class allocated dynamically, so
* @ingroup containers * @ingroup containers
*/ */
class SimpleRingBuffer: public RingBufferBase<> { class SimpleRingBuffer: public RingBufferBase<> {
public: public:
/** /**
* This constructor allocates a new internal buffer with the supplied size. * This constructor allocates a new internal buffer with the supplied size.
* *
* @param size * @param size
* @param overwriteOld If the ring buffer is overflowing at a write * @param overwriteOld If the ring buffer is overflowing at a write
* operation, the oldest data will be overwritten. * operation, the oldest data will be overwritten.
* @param maxExcessBytes These additional bytes will be allocated in addtion * @param maxExcessBytes These additional bytes will be allocated in addtion
* to the specified size to accomodate contiguous write operations * to the specified size to accomodate contiguous write operations
* with getFreeElement. * with getFreeElement.
* *
*/ */
SimpleRingBuffer(const size_t size, bool overwriteOld, SimpleRingBuffer(const size_t size, bool overwriteOld,
size_t maxExcessBytes = 0); size_t maxExcessBytes = 0);
/** /**
* This constructor takes an external buffer with the specified size. * This constructor takes an external buffer with the specified size.
* @param buffer * @param buffer
* @param size * @param size
* @param overwriteOld * @param overwriteOld
* If the ring buffer is overflowing at a write operartion, the oldest data * If the ring buffer is overflowing at a write operartion, the oldest data
* will be overwritten. * will be overwritten.
* @param maxExcessBytes * @param maxExcessBytes
* If the buffer can accomodate additional bytes for contigous write * If the buffer can accomodate additional bytes for contigous write
* operations with getFreeElement, this is the maximum allowed additional * operations with getFreeElement, this is the maximum allowed additional
* size * size
*/ */
SimpleRingBuffer(uint8_t* buffer, const size_t size, bool overwriteOld, SimpleRingBuffer(uint8_t* buffer, const size_t size, bool overwriteOld,
size_t maxExcessBytes = 0); size_t maxExcessBytes = 0);
virtual ~SimpleRingBuffer(); virtual ~SimpleRingBuffer();
/** /**
* Write to circular buffer and increment write pointer by amount. * Write to circular buffer and increment write pointer by amount.
* @param data * @param data
* @param amount * @param amount
* @return -@c RETURN_OK if write operation was successfull * @return -@c RETURN_OK if write operation was successfull
* -@c RETURN_FAILED if * -@c RETURN_FAILED if
*/ */
ReturnValue_t writeData(const uint8_t* data, size_t amount); ReturnValue_t writeData(const uint8_t* data, size_t amount);
/** /**
* Returns a pointer to a free element. If the remaining buffer is * Returns a pointer to a free element. If the remaining buffer is
* not large enough, the data will be written past the actual size * not large enough, the data will be written past the actual size
* and the amount of excess bytes will be cached. This function * and the amount of excess bytes will be cached. This function
* does not increment the write pointer! * does not increment the write pointer!
* @param writePointer Pointer to a pointer which can be used to write * @param writePointer Pointer to a pointer which can be used to write
* contiguous blocks into the ring buffer * contiguous blocks into the ring buffer
* @param amount * @param amount
* @return * @return
*/ */
ReturnValue_t getFreeElement(uint8_t** writePointer, size_t amount); ReturnValue_t getFreeElement(uint8_t** writePointer, size_t amount);
/** /**
* This increments the write pointer and also copies the excess bytes * This increments the write pointer and also copies the excess bytes
* to the beginning. It should be called if the write operation * to the beginning. It should be called if the write operation
* conducted after calling getFreeElement() was performed. * conducted after calling getFreeElement() was performed.
* @return * @return
*/ */
void confirmBytesWritten(size_t amount); void confirmBytesWritten(size_t amount);
virtual size_t getExcessBytes() const; virtual size_t getExcessBytes() const;
/** /**
* Helper functions which moves any excess bytes to the start * Helper functions which moves any excess bytes to the start
* of the ring buffer. * of the ring buffer.
* @return * @return
*/ */
virtual void moveExcessBytesToStart(); virtual void moveExcessBytesToStart();
/** /**
* Read from circular buffer at read pointer. * Read from circular buffer at read pointer.
* @param data * @param data
* @param amount * @param amount
* @param incrementReadPtr * @param incrementReadPtr
* If this is set to true, the read pointer will be incremented. * If this is set to true, the read pointer will be incremented.
* If readRemaining is set to true, the read pointer will be incremented * If readRemaining is set to true, the read pointer will be incremented
* accordingly. * accordingly.
* @param readRemaining * @param readRemaining
* If this is set to true, the data will be read even if the amount * If this is set to true, the data will be read even if the amount
* specified exceeds the read data available. * specified exceeds the read data available.
* @param trueAmount [out] * @param trueAmount [out]
* If readRemaining was set to true, the true amount read will be assigned * If readRemaining was set to true, the true amount read will be assigned
* to the passed value. * to the passed value.
* @return * @return
* - @c RETURN_OK if data was read successfully * - @c RETURN_OK if data was read successfully
* - @c RETURN_FAILED if not enough data was available and readRemaining * - @c RETURN_FAILED if not enough data was available and readRemaining
* was set to false. * was set to false.
*/ */
ReturnValue_t readData(uint8_t* data, size_t amount, ReturnValue_t readData(uint8_t* data, size_t amount,
bool incrementReadPtr = false, bool readRemaining = false, bool incrementReadPtr = false, bool readRemaining = false,
size_t* trueAmount = nullptr); size_t* trueAmount = nullptr);
/** /**
* Delete data by incrementing read pointer. * Delete data by incrementing read pointer.
* @param amount * @param amount
* @param deleteRemaining * @param deleteRemaining
* If the amount specified is larger than the remaing size to read and this * If the amount specified is larger than the remaing size to read and this
* is set to true, the remaining amount will be deleted as well * is set to true, the remaining amount will be deleted as well
* @param trueAmount [out] * @param trueAmount [out]
* If deleteRemaining was set to true, the amount deleted will be assigned * If deleteRemaining was set to true, the amount deleted will be assigned
* to the passed value. * to the passed value.
* @return * @return
*/ */
ReturnValue_t deleteData(size_t amount, bool deleteRemaining = false, ReturnValue_t deleteData(size_t amount, bool deleteRemaining = false,
size_t* trueAmount = nullptr); size_t* trueAmount = nullptr);
private: private:
static const uint8_t READ_PTR = 0; static const uint8_t READ_PTR = 0;
uint8_t* buffer = nullptr; uint8_t* buffer = nullptr;
size_t maxExcessBytes; size_t maxExcessBytes;
size_t excessBytes = 0; size_t excessBytes = 0;
}; };
#endif /* FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ */ #endif /* FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ */