1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-07 16:23:38 +03:00

OOM debug: warn about String reallocation (#7908)

* OOM debug: warn about String reallocation

* threshold set to 128 bytes, +defines

* warn only when capacity had already reached the threshold
This commit is contained in:
david gauchard 2021-03-14 07:57:23 +01:00 committed by GitHub
parent e07542d701
commit 444002180b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,6 +25,12 @@
#include "WString.h" #include "WString.h"
#include "stdlib_noniso.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 */ /* Constructors */
/*********************************************/ /*********************************************/
@ -178,6 +184,14 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
} }
// Fallthrough to normal allocator // Fallthrough to normal allocator
size_t newSize = (maxStrLen + 16) & (~0xf); size_t newSize = (maxStrLen + 16) & (~0xf);
#ifdef DEBUG_ESP_OOM
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(),
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 // Make sure we can fit newsize in the buffer
if (newSize > CAPACITY_MAX) { if (newSize > CAPACITY_MAX) {
return 0; return 0;