From 2bc107032cf0b6304f3e0da1599526ccc8ca4ea5 Mon Sep 17 00:00:00 2001 From: Gregg Date: Tue, 21 Nov 2023 18:36:51 -0600 Subject: [PATCH] Added HS_REALLOC macro for generalized realloc() when using PSRAM When using PSRAM, changing the number of Accessories no longer creates any change to heap usage on internal RAM. The entire Accessory will be stored in PSRAM. --- src/HomeSpan.cpp | 4 ++-- src/HomeSpan.h | 8 +++++--- src/src.ino | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 18a2051..981e3a8 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -2071,7 +2071,7 @@ SpanCharacteristic *SpanCharacteristic::setValidValues(int n, ...){ va_end(vl); s+="]"; - validValues=(char *)realloc(validValues, strlen(s.c_str()) + 1); + validValues=(char *)HS_REALLOC(validValues, strlen(s.c_str()) + 1); strcpy(validValues,s.c_str()); return(this); @@ -2202,7 +2202,7 @@ void SpanWebLog::vLog(boolean sysMsg, const char *fmt, va_list ap){ else log[index].clockTime.tm_year=0; - log[index].message=(char *)realloc(log[index].message, strlen(buf) + 1); + log[index].message=(char *)HS_REALLOC(log[index].message, strlen(buf) + 1); strcpy(log[index].message, buf); log[index].clientIP=homeSpan.lastClientIP; diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 05d73b5..6f41c02 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -34,10 +34,12 @@ #if defined(BOARD_HAS_PSRAM) #define HS_MALLOC ps_malloc #define HS_CALLOC ps_calloc +#define HS_REALLOC ps_realloc #define ps_new(X) new(ps_malloc(sizeof(X)))X #else #define HS_MALLOC malloc #define HS_CALLOC calloc +#define HS_REALLOC realloc #define ps_new(X) new X #endif @@ -552,7 +554,7 @@ class SpanCharacteristic{ } void uvSet(UVal &u, const char *val){ - u.STRING = (char *)realloc(u.STRING, strlen(val) + 1); + u.STRING = (char *)HS_REALLOC(u.STRING, strlen(val) + 1); strcpy(u.STRING, val); } @@ -830,13 +832,13 @@ class SpanCharacteristic{ } SpanCharacteristic *setDescription(const char *c){ - desc = (char *)realloc(desc, strlen(c) + 1); + desc = (char *)HS_REALLOC(desc, strlen(c) + 1); strcpy(desc, c); return(this); } SpanCharacteristic *setUnit(const char *c){ - unit = (char *)realloc(unit, strlen(c) + 1); + unit = (char *)HS_REALLOC(unit, strlen(c) + 1); strcpy(unit, c); return(this); } diff --git a/src/src.ino b/src/src.ino index 16f300a..bad272e 100644 --- a/src/src.ino +++ b/src/src.ino @@ -40,7 +40,7 @@ void setup() { ps_new(Service::AccessoryInformation)(); ps_new(Characteristic::Identify)(); - for(int i=0;i<80;i++){ + for(int i=0;i<100;i++){ ps_new(SpanAccessory)(); ps_new(Service::AccessoryInformation)(); ps_new(Characteristic::Identify)();