From 447a79f179ee621cc23dc4174e4c0f4b36dfdda0 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Thu, 4 Mar 2021 21:49:23 +0100 Subject: [PATCH 1/3] OOM debug: warn about String reallocation --- cores/esp8266/WString.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 0793633aee..8d191b4f78 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -178,6 +178,15 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) { } // Fallthrough to normal allocator size_t newSize = (maxStrLen + 16) & (~0xf); +#ifdef DEBUG_ESP_OOM + if (!isSSO() && maxStrLen > length()) + { + // warn when badly re-allocating + DEBUGV("[String realloc %d->%d ('%.10s ... %.10s')]\n", + len(), maxStrLen, c_str(), + len() > 10? c_str() + std::max((int)len() - 10, 10): ""); + } +#endif // Make sure we can fit newsize in the buffer if (newSize > CAPACITY_MAX) { return 0; From 5eb6d66edeb125c8190343cf37f8219743be76bd Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 5 Mar 2021 22:22:26 +0100 Subject: [PATCH 2/3] threshold set to 128 bytes, +defines --- cores/esp8266/WString.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 8d191b4f78..ea8abc44a3 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -25,6 +25,12 @@ #include "WString.h" #include "stdlib_noniso.h" +#define OOM_STRING_BORDER_DISPLAY 10 +#define OOM_STRING_THRESHOLD_REALLOC_WARN 128 + +#define __STRHELPER(x) #x +#define STR(x) __STRHELPER(x) // stringifier + /*********************************************/ /* Constructors */ /*********************************************/ @@ -179,12 +185,11 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) { // Fallthrough to normal allocator size_t newSize = (maxStrLen + 16) & (~0xf); #ifdef DEBUG_ESP_OOM - if (!isSSO() && maxStrLen > length()) - { + if (!isSSO() && maxStrLen >= OOM_STRING_THRESHOLD_REALLOC_WARN && maxStrLen > capacity()) { // warn when badly re-allocating - DEBUGV("[String realloc %d->%d ('%.10s ... %.10s')]\n", + DEBUGV("[offending String op %d->%d ('%." STR(OOM_STRING_BORDER_DISPLAY) "s ... %." STR(OOM_STRING_BORDER_DISPLAY) "s')]\n", len(), maxStrLen, c_str(), - len() > 10? c_str() + std::max((int)len() - 10, 10): ""); + len() > OOM_STRING_BORDER_DISPLAY? c_str() + std::max((int)len() - OOM_STRING_BORDER_DISPLAY, OOM_STRING_BORDER_DISPLAY): ""); } #endif // Make sure we can fit newsize in the buffer From 85e2e52bc85990e2bafdf7d6175765499230e6b6 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 5 Mar 2021 22:27:03 +0100 Subject: [PATCH 3/3] warn only when capacity had already reached the threshold --- cores/esp8266/WString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index ea8abc44a3..19a4143ecf 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -185,7 +185,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) { // Fallthrough to normal allocator size_t newSize = (maxStrLen + 16) & (~0xf); #ifdef DEBUG_ESP_OOM - if (!isSSO() && maxStrLen >= OOM_STRING_THRESHOLD_REALLOC_WARN && maxStrLen > capacity()) { + if (!isSSO() && capacity() >= OOM_STRING_THRESHOLD_REALLOC_WARN && maxStrLen > capacity()) { // warn when badly re-allocating DEBUGV("[offending String op %d->%d ('%." STR(OOM_STRING_BORDER_DISPLAY) "s ... %." STR(OOM_STRING_BORDER_DISPLAY) "s')]\n", len(), maxStrLen, c_str(),