Skip to content

Commit

Permalink
Memory Optimization: Add function to List class to pre-allocate a giv…
Browse files Browse the repository at this point in the history
…en size
  • Loading branch information
kainhofer authored and user2684 committed Dec 31, 2017
1 parent 7b1b23b commit 3be293b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 10 additions & 5 deletions NodeManagerLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,12 @@ template<typename T> class List {
_internalArray = NULL;
_endPosition = 0;
_allocBlocks = 0;
_preAllocBlocks = 0;
}
~List() {
delete[] _internalArray;
_internalArray = NULL;
_endPosition = 0;
_allocBlocks = 0;
_preAllocBlocks = 0;
}
void push(T item) {
if (_endPosition == _allocBlocks) _AllocOneBlock(false);
Expand All @@ -186,7 +184,7 @@ template<typename T> class List {
void pop() {
if (_endPosition == 0) return;
--_endPosition;
if (_allocBlocks > _preAllocBlocks) _DeAllocOneBlock(false);
_DeAllocOneBlock(false);
}
T get(int position) {
position = position -1;
Expand All @@ -197,11 +195,18 @@ template<typename T> class List {
inline iterator end() { return _internalArray + _endPosition; }
inline bool empty() { return (_endPosition == 0); }
inline unsigned int size() { return _endPosition; }
void allocateBlocks(int alloc) {
_allocBlocks = alloc;
T* newArray = new T[_allocBlocks];
for (int i = 0; i < _endPosition; ++i) newArray[i] = _internalArray[i];
delete[] _internalArray;
_internalArray = newArray;
}

private:
T* _internalArray;
int _endPosition;
int _allocBlocks;
int _preAllocBlocks;
void _AllocOneBlock(bool shiftItems) {
++_allocBlocks;
T* newArray = new T[_allocBlocks];
Expand Down Expand Up @@ -1367,7 +1372,7 @@ class SensorVL53L0X: public Sensor {
*/
class NodeManager {
public:
NodeManager();
NodeManager(int sensorcount = 0);
// [10] send the same message multiple times (default: 1)
void setRetries(int value);
int getRetries();
Expand Down
7 changes: 6 additions & 1 deletion NodeManagerLibrary.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2995,6 +2995,8 @@ SensorPlantowerPMS::SensorPlantowerPMS(NodeManager& node_manager, int rxpin, int

// what to do during before
void SensorPlantowerPMS::onBefore() {
// Allocate memory for all children at once (to prevent memory fragmentation)
children.allocateBlocks(3);
// register the child
new ChildInt(this, _node->getAvailableChildId(), S_DUST, V_LEVEL, "PM1.0");
new ChildInt(this, _node->getAvailableChildId(), S_DUST, V_LEVEL, "PM2.5");
Expand Down Expand Up @@ -3425,9 +3427,12 @@ void SensorConfiguration::onReceive(MyMessage* message) {
*/

// initialize the node manager
NodeManager::NodeManager() {
NodeManager::NodeManager(int sensorcount) {
// setup the message container
_message = MyMessage();
if (sensorcount>0) {
sensors.allocateBlocks(sensorcount);
}
}

int NodeManager::_last_interrupt_pin = -1;
Expand Down

0 comments on commit 3be293b

Please sign in to comment.