Skip to content

Commit 4440021

Browse files
authored
OOM debug: warn about String reallocation (esp8266#7908)
* OOM debug: warn about String reallocation * threshold set to 128 bytes, +defines * warn only when capacity had already reached the threshold
1 parent e07542d commit 4440021

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

cores/esp8266/WString.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#include "WString.h"
2626
#include "stdlib_noniso.h"
2727

28+
#define OOM_STRING_BORDER_DISPLAY 10
29+
#define OOM_STRING_THRESHOLD_REALLOC_WARN 128
30+
31+
#define __STRHELPER(x) #x
32+
#define STR(x) __STRHELPER(x) // stringifier
33+
2834
/*********************************************/
2935
/* Constructors */
3036
/*********************************************/
@@ -178,6 +184,14 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
178184
}
179185
// Fallthrough to normal allocator
180186
size_t newSize = (maxStrLen + 16) & (~0xf);
187+
#ifdef DEBUG_ESP_OOM
188+
if (!isSSO() && capacity() >= OOM_STRING_THRESHOLD_REALLOC_WARN && maxStrLen > capacity()) {
189+
// warn when badly re-allocating
190+
DEBUGV("[offending String op %d->%d ('%." STR(OOM_STRING_BORDER_DISPLAY) "s ... %." STR(OOM_STRING_BORDER_DISPLAY) "s')]\n",
191+
len(), maxStrLen, c_str(),
192+
len() > OOM_STRING_BORDER_DISPLAY? c_str() + std::max((int)len() - OOM_STRING_BORDER_DISPLAY, OOM_STRING_BORDER_DISPLAY): "");
193+
}
194+
#endif
181195
// Make sure we can fit newsize in the buffer
182196
if (newSize > CAPACITY_MAX) {
183197
return 0;

0 commit comments

Comments
 (0)