Skip to content

Commit

Permalink
[Memory] Tweak some memory allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Jan 29, 2025
1 parent 6a95d04 commit c1d5307
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
16 changes: 13 additions & 3 deletions src/src/ESPEasyCore/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,20 @@ bool MQTTpublish(controllerIndex_t controller_idx,
if (MQTT_queueFull(controller_idx)) {
return false;
}
String topic_str;
String payload_str;
if (!reserve_special(topic_str, strlen_P(topic)) ||
!reserve_special(payload_str, strlen_P(payload))) {
return false;
}
topic_str = topic;
payload_str = payload;
const bool success =
MQTTDelayHandler->addToQueue(std::unique_ptr<MQTT_queue_element>(new (std::nothrow) MQTT_queue_element(controller_idx, taskIndex, topic,
payload, retained,
callbackTask)));
MQTTDelayHandler->addToQueue(std::unique_ptr<MQTT_queue_element>(new (std::nothrow) MQTT_queue_element(
controller_idx, taskIndex,
std::move(topic_str),
std::move(payload_str), retained,
callbackTask)));

scheduleNextMQTTdelayQueue();
return success;
Expand Down
2 changes: 1 addition & 1 deletion src/src/Helpers/ESPEasy_Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,7 @@ String LoadFromFile(const char *fname, String& data, int offset)
STOP_TIMER(LOADFILE_STATS);
delay(0);

return String();
return EMPTY_STRING;
}

/********************************************************************************************\
Expand Down
5 changes: 4 additions & 1 deletion src/src/Helpers/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ class PSRAM_String : public String {


bool String_reserve_special(String& str, size_t size) {
if (str.length() < size) {
if (!UsePSRAM()) {
return str.reserve(size);
}
if (str.length() <= size) {
// As we like to move this to PSRAM, it also makes sense
// to do this when the length equals size
PSRAM_String psram_str(size);
Expand Down
2 changes: 1 addition & 1 deletion src/src/Helpers/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ void logMemUsageAfter(const __FlashStringHelper *function, int value) {
if (loglevelActiveFor(LOG_LEVEL_DEBUG)) {
String log;

if (log.reserve(128)) {
if (reserve_special(log, 128)) {
log = F("After ");
log += function;

Expand Down
4 changes: 3 additions & 1 deletion src/src/Helpers/PortStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "../DataStructs/PinMode.h"
#include "../Globals/GlobalMapPortStatus.h"

#include "../Helpers/StringConverter.h"

#include "../../ESPEasy-Globals.h"


Expand Down Expand Up @@ -208,7 +210,7 @@ String getPinStateJSON(bool search, uint32_t key, const String& log, int16_t noS
if (!search || found)
{
String reply;
reply.reserve(128);
reserve_special(reply, 128);
reply += F("{\n\"log\": \"");
{
// truncate to 25 chars, max MQTT message size = 128 including header...
Expand Down
16 changes: 7 additions & 9 deletions src/src/Helpers/RulesHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,11 @@ void RulesHelperClass::init()
#ifndef BUILD_NO_DEBUG

if (loglevelActiveFor(LOG_LEVEL_DEBUG)) {
String log = F("Cache rules event: ");
log += filename;
log += F(" pos: ");
log += pos_start_line;
log += ' ';
log += rulesLine;
addLogMove(LOG_LEVEL_DEBUG, log);
addLogMove(LOG_LEVEL_DEBUG, strformat(
F("Cache rules event: %s pos: %u %s"),
filename.c_str(),
pos_start_line,
rulesLine.c_str()));
}
#endif // ifndef BUILD_NO_DEBUG
}
Expand Down Expand Up @@ -296,7 +294,7 @@ String RulesHelperClass::readLn(const String& filename,

while (f.available()) {
if (addChar(char(f.read()), tmpStr, firstNonSpaceRead)) {
lines.push_back(tmpStr);
lines.push_back(move_special(std::move(tmpStr)));
++readPos;

firstNonSpaceRead = false;
Expand All @@ -307,7 +305,7 @@ String RulesHelperClass::readLn(const String& filename,
if (tmpStr.length() > 0) {
rules_strip_trailing_comments(tmpStr);
check_rules_line_user_errors(tmpStr);
lines.push_back(tmpStr);
lines.push_back(move_special(std::move(tmpStr)));
tmpStr.clear();
}
# ifndef BUILD_NO_DEBUG
Expand Down
4 changes: 2 additions & 2 deletions src/src/Helpers/Scheduler_SystemEventTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void ESPEasy_Scheduler::schedule_mqtt_controller_event_timer(
const size_t topic_length = strlen_P(c_topic);

// This is being called from a callback function, so do not try to allocate this on the 2nd heap, but rather on the default heap.
if (!(event.String1.reserve(topic_length) &&
event.String2.reserve(length))) {
if (!(reserve_special(event.String1, topic_length) &&
reserve_special(event.String2, length))) {
addLog(LOG_LEVEL_ERROR, F("MQTT : Out of Memory! Cannot process MQTT message"));
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/src/PluginStructs/P028_data_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ bool P028_data_struct::updateMeasurements(taskIndex_t task_index) {
String log;

if (loglevelActiveFor(LOG_LEVEL_INFO)) {
log.reserve(120); // Prevent re-allocation
reserve_special(log, 120); // Prevent re-allocation
log = getDeviceName(sensorID);
log += ':';
}
Expand Down

0 comments on commit c1d5307

Please sign in to comment.