dynamic fifo bug fixed

This commit is contained in:
Robin Müller 2020-07-11 02:36:04 +02:00
parent 6a6395313f
commit 35d4267b40
5 changed files with 44 additions and 28 deletions

View File

@ -17,11 +17,26 @@
template<typename T> template<typename T>
class DynamicFIFO: public FIFOBase<T> { class DynamicFIFO: public FIFOBase<T> {
public: public:
DynamicFIFO(size_t maxCapacity): FIFOBase<T>(values.data(), maxCapacity), DynamicFIFO(size_t maxCapacity): FIFOBase<T>(nullptr, maxCapacity),
values(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<T>(other),
fifoVector(other.maxCapacity) {
this->setData(fifoVector.data());
}
private: private:
std::vector<T> values; std::vector<T> fifoVector;
}; };
#endif /* FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ */ #endif /* FRAMEWORK_CONTAINER_DYNAMICFIFO_H_ */

View File

@ -17,10 +17,18 @@
template<typename T, size_t capacity> template<typename T, size_t capacity>
class FIFO: public FIFOBase<T> { class FIFO: public FIFOBase<T> {
public: public:
FIFO(): FIFOBase<T>(values.data(), capacity) {}; FIFO(): FIFOBase<T>(fifoArray.data(), capacity) {};
/**
* @brief Custom copy constructor to set pointer correctly.
* @param other
*/
FIFO(const FIFO& other): FIFOBase<T>(other) {
this->setData(fifoArray.data());
}
private: private:
std::array<T, capacity> values; std::array<T, capacity> fifoArray;
}; };
#endif /* FRAMEWORK_CONTAINERS_STATICFIFO_H_ */ #endif /* FRAMEWORK_CONTAINERS_STATICFIFO_H_ */

View File

@ -44,29 +44,15 @@ public:
bool full(); bool full();
size_t size(); 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; size_t getMaxCapacity() const;
private: protected:
T* values; void setData(T* data);
size_t maxCapacity = 0; size_t maxCapacity = 0;
T* values;
size_t readIndex = 0; size_t readIndex = 0;
size_t writeIndex = 0; size_t writeIndex = 0;
size_t currentSize = 0; size_t currentSize = 0;

View File

@ -7,7 +7,7 @@
template<typename T> template<typename T>
inline FIFOBase<T>::FIFOBase(T* values, const size_t maxCapacity): inline FIFOBase<T>::FIFOBase(T* values, const size_t maxCapacity):
values(values), maxCapacity(maxCapacity) {}; maxCapacity(maxCapacity), values(values){};
template<typename T> template<typename T>
inline ReturnValue_t FIFOBase<T>::insert(T value) { inline ReturnValue_t FIFOBase<T>::insert(T value) {
@ -78,4 +78,10 @@ inline size_t FIFOBase<T>::getMaxCapacity() const {
return maxCapacity; return maxCapacity;
} }
template<typename T>
inline void FIFOBase<T>::setData(T *data) {
this->values = data;
}
#endif #endif

View File

@ -63,11 +63,12 @@ public:
* @return * @return
*/ */
indexSizePair getNextFifoPair(); indexSizePair getNextFifoPair();
private:
//! A FIFO is used to store information about multiple PUS packets private:
//! inside the receive buffer. The maximum number of entries is defined /** A FIFO is used to store information about multiple PUS packets
//! by the first constructor argument. * inside the receive buffer. The maximum number of entries is defined
* by the first constructor argument.
*/
DynamicFIFO<indexSizePair> indexSizePairFIFO; DynamicFIFO<indexSizePair> indexSizePairFIFO;
bool storeSplitPackets = false; bool storeSplitPackets = false;