Skip to content

Commit 1c93b6c

Browse files
committed
Refactor RFConfiguration for remove std:vector and use dynamic arrays for white and black lists, adding destructor for memory management
1 parent 780e05a commit 1c93b6c

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

main/config_RF.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
*/
2626
#ifndef config_RF_h
2727
#define config_RF_h
28+
#pragma once
2829

2930
#include <rf/RFConfiguration.h>
30-
extern RFConfiguration iRFConfig;
31-
32-
#include "TheengsCommon.h"
3331

3432
#ifdef ZgatewayRF
3533
extern void RFtoX();
@@ -139,6 +137,9 @@ const char parameters[51][4][24] = {
139137
# define DISCOVERY_TRACE_LOG(...)
140138
# endif
141139
#endif
140+
141+
extern RFConfiguration iRFConfig;
142+
142143
/*-------------------RF topics & parameters----------------------*/
143144
//433Mhz MQTT Subjects and keys
144145
#define subjectMQTTtoRF "/commands/MQTTto433"

main/rf/RFConfiguration.cpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ extern rtl_433_ESP rtl_433;
88
#endif
99

1010
// Constructor
11-
RFConfiguration::RFConfiguration(RFReceiver& receiver) : iRFReceiver(receiver) {
11+
RFConfiguration::RFConfiguration(RFReceiver& receiver) : iRFReceiver(receiver), whiteList(nullptr), whiteListSize(0), blackList(nullptr), blackListSize(0) {
1212
reInit();
1313
}
1414

15+
// Destructor
16+
RFConfiguration::~RFConfiguration() {
17+
delete[] whiteList;
18+
delete[] blackList;
19+
}
20+
1521
// Getters and Setters
1622
float RFConfiguration::getFrequency() const {
1723
return frequency;
@@ -61,31 +67,17 @@ void RFConfiguration::setIgnoreBlacklist(bool ignore) {
6167
ignoreBlacklist = ignore;
6268
}
6369

64-
const std::vector<uint64_t>& RFConfiguration::getWhiteList() const {
65-
return whiteList;
66-
}
67-
68-
void RFConfiguration::setWhiteList(const std::vector<uint64_t>& list) {
69-
whiteList = list;
70-
}
71-
72-
const std::vector<uint64_t>& RFConfiguration::getBlackList() const {
73-
return blackList;
74-
}
75-
76-
void RFConfiguration::setBlackList(const std::vector<uint64_t>& list) {
77-
blackList = list;
78-
}
79-
8070
// Utility methods
8171
void RFConfiguration::clearWhiteList() {
82-
whiteList.clear();
83-
whiteList.shrink_to_fit();
72+
delete[] whiteList;
73+
whiteList = nullptr;
74+
whiteListSize = 0;
8475
}
8576

8677
void RFConfiguration::clearBlackList() {
87-
blackList.clear();
88-
blackList.shrink_to_fit();
78+
delete[] blackList;
79+
blackList = nullptr;
80+
blackListSize = 0;
8981
}
9082

9183
/**
@@ -106,10 +98,8 @@ void RFConfiguration::reInit() {
10698
newOokThreshold = 0;
10799
ignoreWhitelist = false;
108100
ignoreBlacklist = false;
109-
whiteList.clear();
110-
whiteList.shrink_to_fit();
111-
blackList.clear();
112-
blackList.shrink_to_fit();
101+
clearWhiteList();
102+
clearBlackList();
113103
}
114104

115105
/**
@@ -359,13 +349,13 @@ void RFConfiguration::toJson(JsonObject& RFdata) {
359349

360350
// Add white-list vector to the JSON object
361351
JsonArray whiteListArray = RFdata.createNestedArray("white-list");
362-
for (const auto& value : whiteList) {
363-
whiteListArray.add(value);
352+
for (size_t i = 0; i < whiteListSize; ++i) {
353+
whiteListArray.add(whiteList[i]);
364354
}
365355
// Add black-list vector to the JSON object
366356
JsonArray blackListArray = RFdata.createNestedArray("black-list");
367-
for (const auto& value : blackList) {
368-
blackListArray.add(value);
357+
for (size_t i = 0; i < blackListSize; ++i) {
358+
blackListArray.add(blackList[i]);
369359
}
370360
}
371361

@@ -385,8 +375,8 @@ bool RFConfiguration::inBlackList(uint64_t MQTTvalue) {
385375
if (ignoreBlacklist) {
386376
return false;
387377
}
388-
for (const auto& value : blackList) {
389-
if (value == MQTTvalue) {
378+
for (size_t i = 0; i < blackListSize; ++i) {
379+
if (blackList[i] == MQTTvalue) {
390380
return true;
391381
}
392382
}
@@ -405,11 +395,11 @@ bool RFConfiguration::inBlackList(uint64_t MQTTvalue) {
405395
* @return false if the value is not in the whitelist.
406396
*/
407397
bool RFConfiguration::inWhiteList(uint64_t MQTTvalue) {
408-
if (ignoreWhitelist || whiteList.empty()) {
398+
if (ignoreWhitelist || whiteListSize == 0) {
409399
return true;
410400
}
411-
for (const auto& value : whiteList) {
412-
if (value == MQTTvalue) {
401+
for (size_t i = 0; i < whiteListSize; ++i) {
402+
if (whiteList[i] == MQTTvalue) {
413403
return true;
414404
}
415405
}
@@ -467,10 +457,12 @@ bool RFConfiguration::commandSetWhiteorBlackList(JsonObject& RFdata, bool isWhit
467457
if (value != NULL) {
468458
uint64_t MQTTvalue = strtoull(value, NULL, 10);
469459
if (isWhite) {
470-
whiteList.push_back(MQTTvalue);
460+
whiteList = (uint64_t*)realloc(whiteList, (whiteListSize + 1) * sizeof(uint64_t));
461+
whiteList[whiteListSize++] = MQTTvalue;
471462
Log.trace(F("[RF] White list updated with value: %s" CR), value);
472463
} else {
473-
blackList.push_back(MQTTvalue);
464+
blackList = (uint64_t*)realloc(blackList, (blackListSize + 1) * sizeof(uint64_t));
465+
blackList[blackListSize++] = MQTTvalue;
474466
Log.trace(F("[RF] Black list updated with value: %s" CR), value);
475467
}
476468
} else {

main/rf/RFConfiguration.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#include <TheengsCommon.h>
66
#include <rf/RFReceiver.h>
77

8-
#include <vector>
98

109
class RFConfiguration {
1110
public:
1211
// Constructor
1312
RFConfiguration(RFReceiver& receiver);
13+
~RFConfiguration();
1414

1515
// Getters and Setters
1616
float getFrequency() const;
@@ -31,12 +31,6 @@ class RFConfiguration {
3131
bool isBlacklistIgnored() const;
3232
void setIgnoreBlacklist(bool ignore);
3333

34-
const std::vector<uint64_t>& getWhiteList() const;
35-
void setWhiteList(const std::vector<uint64_t>& list);
36-
37-
const std::vector<uint64_t>& getBlackList() const;
38-
void setBlackList(const std::vector<uint64_t>& list);
39-
4034
// Utility methods
4135
void clearWhiteList();
4236
void clearBlackList();
@@ -150,8 +144,10 @@ class RFConfiguration {
150144
int activeReceiver;
151145
bool ignoreWhitelist;
152146
bool ignoreBlacklist;
153-
std::vector<uint64_t> whiteList;
154-
std::vector<uint64_t> blackList;
147+
uint64_t* whiteList;
148+
size_t whiteListSize;
149+
uint64_t* blackList;
150+
size_t blackListSize;
155151

156152
bool commandSetWhiteorBlackList(JsonObject& RFdata, bool isWhite);
157153
};

0 commit comments

Comments
 (0)