Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String: informative messages on issues #8821

Merged
merged 4 commits into from
Jan 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ bool String::reserve(unsigned int size) {
return false;
}

#ifdef DEBUG_ESP_PORT
static void identifyString (const String& badOne)
{
DEBUGV("[String] '%." STR(OOM_STRING_BORDER_DISPLAY) "s ... %." STR(OOM_STRING_BORDER_DISPLAY) "s': ",
badOne.c_str(),
badOne.length() > OOM_STRING_BORDER_DISPLAY? badOne.c_str() + std::max((int)badOne.length() - OOM_STRING_BORDER_DISPLAY, OOM_STRING_BORDER_DISPLAY): "");
}
#endif

bool String::changeBuffer(unsigned int maxStrLen) {
// Can we use SSO here to avoid allocation?
if (maxStrLen < sizeof(sso.buff) - 1) {
Expand All @@ -218,16 +227,19 @@ bool String::changeBuffer(unsigned int maxStrLen) {
}
// Fallthrough to normal allocator
size_t newSize = (maxStrLen + 16) & (~0xf);
#ifdef DEBUG_ESP_OOM
#ifdef DEBUG_ESP_PORT
if (!isSSO() && capacity() >= OOM_STRING_THRESHOLD_REALLOC_WARN && maxStrLen > capacity()) {
// warn when badly re-allocating
DEBUGV("[String] Reallocating large String(%d -> %d bytes) '%." 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): "");
identifyString(*this);
DEBUGV("Reallocating large String(%d -> %d bytes)\n", len(), maxStrLen);
}
#endif
// Make sure we can fit newsize in the buffer
if (newSize > CAPACITY_MAX) {
#ifdef DEBUG_ESP_PORT
identifyString(*this);
DEBUGV("Maximum capacity reached (" STR(CAPACITY_MAX) ")\n");
#endif
return false;
}
uint16_t oldLen = len();
Expand All @@ -247,6 +259,10 @@ bool String::changeBuffer(unsigned int maxStrLen) {
setBuffer(newbuffer);
return true;
}
#ifdef DEBUG_ESP_PORT
identifyString(*this);
DEBUGV("OOM: %d -> %d bytes\n", isSSO() ? 0: capacity(), newSize);
#endif
return false;
}

Expand Down Expand Up @@ -804,7 +820,7 @@ void String::replace(const String &find, const String &replace) {
if (size == len())
return;
if (size > capacity() && !changeBuffer(size))
return; // XXX: tell user!
return;
int index = len() - 1;
while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
readFrom = wbuffer() + index + find.len();
Expand Down