From 35d4267b400a950847447d2adc24e53096e4ef86 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Sat, 11 Jul 2020 02:36:04 +0200 Subject: [PATCH] dynamic fifo bug fixed --- container/DynamicFIFO.h | 21 ++++++++++++++++++--- container/FIFO.h | 12 ++++++++++-- container/FIFOBase.h | 22 ++++------------------ container/FIFOBase.tpp | 8 +++++++- tmtcservices/PusParser.h | 9 +++++---- 5 files changed, 44 insertions(+), 28 deletions(-) diff --git a/container/DynamicFIFO.h b/container/DynamicFIFO.h index fca4c8e8..59adfb3a 100644 --- a/container/DynamicFIFO.h +++ b/container/DynamicFIFO.h @@ -17,11 +17,26 @@ template class DynamicFIFO: public FIFOBase { public: - DynamicFIFO(size_t maxCapacity): FIFOBase(values.data(), maxCapacity), - values(maxCapacity) {}; + DynamicFIFO(size_t maxCapacity): FIFOBase(nullptr, maxCapacity), + fifoVector(maxCapacity) { + // trying to pass the pointer of the uninitialized vector + // to the FIFOBase constructor directly lead to a super evil bug. + // So we do it like this now. + this->setData(fifoVector.data()); + }; + + /** + * @brief Custom copy constructor which prevents setting the + * underlying pointer wrong. + */ + DynamicFIFO(const DynamicFIFO& other): FIFOBase(other), + fifoVector(other.maxCapacity) { + this->setData(fifoVector.data()); + } + private: - std::vector values; + std::vector fifoVector; }; #endif /* FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ */ diff --git a/container/FIFO.h b/container/FIFO.h index ecb218fd..2e332dc2 100644 --- a/container/FIFO.h +++ b/container/FIFO.h @@ -17,10 +17,18 @@ template class FIFO: public FIFOBase { public: - FIFO(): FIFOBase(values.data(), capacity) {}; + FIFO(): FIFOBase(fifoArray.data(), capacity) {}; + + /** + * @brief Custom copy constructor to set pointer correctly. + * @param other + */ + FIFO(const FIFO& other): FIFOBase(other) { + this->setData(fifoArray.data()); + } private: - std::array values; + std::array fifoArray; }; #endif /* FRAMEWORK_CONTAINERS_STATICFIFO_H_ */ diff --git a/container/FIFOBase.h b/container/FIFOBase.h index 082c596a..7f8bde96 100644 --- a/container/FIFOBase.h +++ b/container/FIFOBase.h @@ -44,29 +44,15 @@ public: bool full(); size_t size(); - FIFOBase(const FIFOBase& other): readIndex(other.readIndex), - writeIndex(other.writeIndex), currentSize(other.currentSize), - maxCapacity(other.maxCapacity) { - std::memcpy(values, other.values, sizeof(T) * currentSize); - } - - FIFOBase& operator=(const FIFOBase& other) { - if(&other == this) - return *this; - maxCapacity = other.maxCapacity; - readIndex = other.readIndex; - writeIndex = other.writeIndex; - currentSize = other.currentSize; - std::memcpy(values, other.values, sizeof(T) * currentSize); - return *this; - } size_t getMaxCapacity() const; -private: - T* values; +protected: + void setData(T* data); size_t maxCapacity = 0; + T* values; + size_t readIndex = 0; size_t writeIndex = 0; size_t currentSize = 0; diff --git a/container/FIFOBase.tpp b/container/FIFOBase.tpp index 48a060ff..c73e9d59 100644 --- a/container/FIFOBase.tpp +++ b/container/FIFOBase.tpp @@ -7,7 +7,7 @@ template inline FIFOBase::FIFOBase(T* values, const size_t maxCapacity): - values(values), maxCapacity(maxCapacity) {}; + maxCapacity(maxCapacity), values(values){}; template inline ReturnValue_t FIFOBase::insert(T value) { @@ -78,4 +78,10 @@ inline size_t FIFOBase::getMaxCapacity() const { return maxCapacity; } + +template +inline void FIFOBase::setData(T *data) { + this->values = data; +} + #endif diff --git a/tmtcservices/PusParser.h b/tmtcservices/PusParser.h index 88570bb2..d59673f6 100644 --- a/tmtcservices/PusParser.h +++ b/tmtcservices/PusParser.h @@ -63,11 +63,12 @@ public: * @return */ indexSizePair getNextFifoPair(); -private: - //! A FIFO is used to store information about multiple PUS packets - //! inside the receive buffer. The maximum number of entries is defined - //! by the first constructor argument. +private: + /** A FIFO is used to store information about multiple PUS packets + * inside the receive buffer. The maximum number of entries is defined + * by the first constructor argument. + */ DynamicFIFO indexSizePairFIFO; bool storeSplitPackets = false;