Skip to content

Commit

Permalink
Version 2.0.5: Fix mPeakCount value on buffer overflow (thanks to Kor…
Browse files Browse the repository at this point in the history
…yphon)
  • Loading branch information
pierremolinaro committed May 31, 2020
1 parent 756aca2 commit ac17c38
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 106 deletions.
Binary file modified extras/acan2515.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ACAN2515
version=2.0.4
version=2.0.5
author=Pierre Molinaro
maintainer=Pierre Molinaro <[email protected]>
sentence=Driver for MCP2515 CAN Controller
Expand Down
195 changes: 90 additions & 105 deletions src/ACANBuffer16.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,134 +18,119 @@

class ACANBuffer16 {

//······················································································································
// Default constructor
//······················································································································

public:
ACANBuffer16(void)
: mBuffer(NULL), mSize(0), mReadIndex(0), mCount(0), mPeakCount(0) {}

//······················································································································
// Destructor
//······················································································································

public:
~ACANBuffer16(void) { delete[] mBuffer; }

//······················································································································
// Private properties
//······················································································································

private:
CANMessage *mBuffer;

private:
uint16_t mSize;

private:
uint16_t mReadIndex;

private:
volatile uint16_t mCount;

private:
uint16_t mPeakCount; // > mSize if overflow did occur

//······················································································································
// Accessors
//······················································································································

public:
inline uint16_t size(void) const { return mSize; }

public:
inline uint16_t count(void) const { return mCount; }

public:
inline uint16_t peakCount(void) const { return mPeakCount; }

//······················································································································
// initWithSize
//······················································································································
//······················································································································
// Default constructor
//······················································································································

public: ACANBuffer16 (void) :
mBuffer (nullptr),
mSize (0),
mReadIndex (0),
mCount (0),
mPeakCount (0) {
}

public:
bool initWithSize(const uint16_t inSize) {
delete[] mBuffer;
mBuffer = new CANMessage[inSize];
const bool ok = mBuffer != NULL;
mSize = ok ? inSize : 0;
mReadIndex = 0;
mCount = 0;
mPeakCount = 0;
return ok;
//······················································································································
// Destructor
//······················································································································

public: ~ ACANBuffer16(void) { delete [] mBuffer ; }

//······················································································································
// Private properties
//······················································································································

private: CANMessage * mBuffer ;
private: uint16_t mSize ;
private: uint16_t mReadIndex ;
private: volatile uint16_t mCount ;
private: uint16_t mPeakCount ; // > mSize if overflow did occur

//······················································································································
// Accessors
//······················································································································

public: inline uint16_t size (void) const { return mSize ; }
public: inline uint16_t count (void) const { return mCount ; }
public: inline uint16_t peakCount (void) const { return mPeakCount ; }

//······················································································································
// initWithSize
//······················································································································

public: bool initWithSize (const uint16_t inSize) {
delete [] mBuffer ;
mBuffer = new CANMessage [inSize] ;
const bool ok = mBuffer != nullptr ;
mSize = ok ? inSize : 0 ;
mReadIndex = 0 ;
mCount = 0 ;
mPeakCount = 0 ;
return ok ;
}

//······················································································································
// append
//······················································································································
//······················································································································
// append
//······················································································································

public:
bool append(const CANMessage &inMessage) {
const bool ok = mCount < mSize;
public: bool append (const CANMessage & inMessage) {
const bool ok = mCount < mSize ;
if (ok) {
uint16_t writeIndex = mReadIndex + mCount;
uint16_t writeIndex = mReadIndex + mCount ;
if (writeIndex >= mSize) {
writeIndex -= mSize;
writeIndex -= mSize ;
}
mBuffer[writeIndex] = inMessage;
mCount++;
mBuffer[writeIndex] = inMessage ;
mCount += 1 ;
if (mPeakCount < mCount) {
mPeakCount = mCount;
mPeakCount = mCount ;
}
} else {
mPeakCount = mSize + 1;
}else{
mPeakCount = mSize + 1 ;
}
return ok;
return ok ;
}

//······················································································································
// Remove
//······················································································································
//······················································································································
// Remove
//······················································································································

public:
bool remove(CANMessage &outMessage) {
const bool ok = mCount > 0;
public: bool remove (CANMessage & outMessage) {
const bool ok = mCount > 0 ;
if (ok) {
outMessage = mBuffer[mReadIndex];
mCount -= 1;
mReadIndex += 1;
outMessage = mBuffer [mReadIndex] ;
mCount -= 1 ;
mReadIndex += 1 ;
if (mReadIndex == mSize) {
mReadIndex = 0;
mReadIndex = 0 ;
}
}
return ok;
return ok ;
}

//······················································································································
// Free
//······················································································································

public:
void free(void) {
delete[] mBuffer;
mBuffer = nullptr;
mSize = 0;
mReadIndex = 0;
mCount = 0;
mPeakCount = 0;
//······················································································································
// Free
//······················································································································

public: void free (void) {
delete [] mBuffer ;
mBuffer = nullptr ;
mSize = 0 ;
mReadIndex = 0 ;
mCount = 0 ;
mPeakCount = 0 ;
}

//······················································································································
// No copy
//······················································································································
//······················································································································
// No copy
//······················································································································

private: ACANBuffer16 (const ACANBuffer16 &) = delete ;
private: ACANBuffer16 & operator = (const ACANBuffer16 &) = delete ;

private:
ACANBuffer16(const ACANBuffer16 &);
//······················································································································

private:
ACANBuffer16 &operator=(const ACANBuffer16 &);
};
} ;

//----------------------------------------------------------------------------------------------------------------------

Expand Down

0 comments on commit ac17c38

Please sign in to comment.