Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow use of statically allocated buffers #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions src/ACAN2515.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,32 @@ uint16_t ACAN2515::internalBeginOperation (const ACAN2515Settings & inSettings,
errorCode |= kInconsistentBitRateSettings ;
}
//----------------------------------- Allocate buffer
if (!mReceiveBuffer.initWithSize (inSettings.mReceiveBufferSize)) {
errorCode |= kCannotAllocateReceiveBuffer ;
}
if (!mTransmitBuffer [0].initWithSize (inSettings.mTransmitBuffer0Size)) {
errorCode |= kCannotAllocateTransmitBuffer0 ;
}
if (!mTransmitBuffer [1].initWithSize (inSettings.mTransmitBuffer1Size)) {
errorCode |= kCannotAllocateTransmitBuffer1 ;
}
if (!mTransmitBuffer [2].initWithSize (inSettings.mTransmitBuffer2Size)) {
errorCode |= kCannotAllocateTransmitBuffer2 ;
if (inSettings.mReceiveAndTxBuffersStatic) {
if (!mReceiveBuffer.initWithStaticSize (inSettings.mReceiveBuffer, inSettings.mReceiveBufferSize)) {
errorCode |= kCannotAllocateReceiveBuffer ;
}
if (!mTransmitBuffer [0].initWithStaticSize (inSettings.mTxb0Buffer, inSettings.mTransmitBuffer0Size)) {
errorCode |= kCannotAllocateTransmitBuffer0 ;
}
if (inSettings.mTransmitBuffer1Size > 0 && !mTransmitBuffer [1].initWithStaticSize (inSettings.mTxb1Buffer, inSettings.mTransmitBuffer1Size)) {
errorCode |= kCannotAllocateTransmitBuffer1 ;
}
if (inSettings.mTransmitBuffer2Size > 0 && !mTransmitBuffer [2].initWithStaticSize (inSettings.mTxb2Buffer, inSettings.mTransmitBuffer2Size)) {
errorCode |= kCannotAllocateTransmitBuffer2 ;
}
} else {
if (!mReceiveBuffer.initWithSize (inSettings.mReceiveBufferSize)) {
errorCode |= kCannotAllocateReceiveBuffer ;
}
if (!mTransmitBuffer [0].initWithSize (inSettings.mTransmitBuffer0Size)) {
errorCode |= kCannotAllocateTransmitBuffer0 ;
}
if (!mTransmitBuffer [1].initWithSize (inSettings.mTransmitBuffer1Size)) {
errorCode |= kCannotAllocateTransmitBuffer1 ;
}
if (!mTransmitBuffer [2].initWithSize (inSettings.mTransmitBuffer2Size)) {
errorCode |= kCannotAllocateTransmitBuffer2 ;
}
}
mTXBIsFree [0] = true ;
mTXBIsFree [1] = true ;
Expand Down
12 changes: 12 additions & 0 deletions src/ACAN2515Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include <stdint.h>


class CANMessage ;

//··································································································

class ACAN2515Settings {
Expand Down Expand Up @@ -155,6 +158,15 @@ class ACAN2515Settings {

public: uint16_t CANBitSettingConsistency (void) const ;

//··································································································
// Static buffer allocation
//··································································································

public: bool mReceiveAndTxBuffersStatic = false ;
public: CANMessage * mReceiveBuffer = nullptr ;
public: CANMessage * mTxb0Buffer = nullptr ;
public: CANMessage * mTxb1Buffer = nullptr ;
public: CANMessage * mTxb2Buffer = nullptr ;

//··································································································
// Constants returned by CANBitSettingConsistency
Expand Down
35 changes: 31 additions & 4 deletions src/ACAN2515_Buffer16.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ class ACAN2515_Buffer16 {
mSize (0),
mReadIndex (0),
mCount (0),
mPeakCount (0) {
mPeakCount (0),
mDynamicAllocate (true) {
}

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

public: ~ ACAN2515_Buffer16 (void) {
delete [] mBuffer ;
if (mDynamicAllocate) {
delete [] mBuffer ;
}
}

//································································································
Expand All @@ -39,6 +42,7 @@ class ACAN2515_Buffer16 {
private: uint16_t mReadIndex ;
private: uint16_t mCount ;
private: uint16_t mPeakCount ; // > mSize if overflow did occur
private: bool mDynamicAllocate ;

//································································································
// Accessors
Expand All @@ -54,13 +58,34 @@ class ACAN2515_Buffer16 {
//································································································

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

//································································································
// initWithStaticSize
//································································································

public: bool initWithStaticSize (CANMessage * buffer, const uint16_t inSize) {
if (mDynamicAllocate) {
delete [] mBuffer ;
}
mBuffer = buffer ;
const bool ok = mBuffer != NULL ;
mSize = ok ? inSize : 0 ;
mReadIndex = 0 ;
mCount = 0 ;
mPeakCount = 0 ;
mDynamicAllocate = false;
return ok ;
}

Expand Down Expand Up @@ -106,7 +131,9 @@ class ACAN2515_Buffer16 {
//································································································

public: void free (void) {
delete [] mBuffer ; mBuffer = nullptr ;
if (mDynamicAllocate) {
delete [] mBuffer ; mBuffer = nullptr ;
}
mSize = 0 ;
mReadIndex = 0 ;
mCount = 0 ;
Expand Down