Bug 1839396 part 11 - Make Sprinter put functions infallible. r=mgaudet

As functions are made infallible, only the 2 release functions are reporting
errors. The advantage of this approach is that the error reporting and checking
would only happen in the release functions calls in the future.  This enables
the upcoming set of patches to change the return type of put functions to make
them infallible, reduce the number of visible branches in debugging code.

This makes the Sprinter class more like a "Sink allocator", where the smell of
allocation failures does not propagate immediatly through the sinking water until
all the water has been through.

Differential Revision: https://phabricator.services.mozilla.com/D181496
This commit is contained in:
Nicolas B. Pierron 2023-09-14 11:41:03 +00:00
parent 7bb9625ab6
commit ec386fb696
4 changed files with 8 additions and 21 deletions

View File

@ -135,11 +135,6 @@ class JS_PUBLIC_API Sprinter final : public GenericPrinter {
size_t length() const;
// Report that a string operation failed to get the memory it requested. The
// first call to this function calls JS_ReportOutOfMemory, and sets this
// Sprinter's outOfMemory flag; subsequent calls do nothing.
virtual void reportOutOfMemory() override;
// When an OOM has already been reported on the Sprinter, this function will
// forward this error to the JSContext given in the Sprinter initialization.
//

View File

@ -3490,7 +3490,7 @@ static bool DisassembleToSprinter(JSContext* cx, unsigned argc, Value* vp,
}
}
return !sprinter->hadOutOfMemory();
return true;
}
static bool DisassembleToString(JSContext* cx, unsigned argc, Value* vp) {

View File

@ -3103,10 +3103,6 @@ static bool GenerateLcovInfo(JSContext* cx, JS::Realm* realm,
bool isEmpty = true;
lcovRealm->exportInto(out, &isEmpty);
if (out.hadOutOfMemory()) {
return false;
}
return true;
}

View File

@ -87,6 +87,9 @@ const size_t Sprinter::DefaultSize = 64;
bool Sprinter::realloc_(size_t newSize) {
MOZ_ASSERT(newSize > (size_t)offset);
if (hadOOM_) {
return false;
}
char* newBuf = (char*)js_realloc(base, newSize);
if (!newBuf) {
reportOutOfMemory();
@ -123,6 +126,7 @@ bool Sprinter::init() {
base = js_pod_malloc<char>(DefaultSize);
if (!base) {
reportOutOfMemory();
forwardOutOfMemory();
return false;
}
#ifdef DEBUG
@ -232,7 +236,7 @@ bool Sprinter::put(const char* s, size_t len) {
char* bp = reserve(len);
if (!bp) {
return false;
return true;
}
// s is within the buffer already
@ -255,14 +259,14 @@ bool Sprinter::putString(JSString* s) {
JSLinearString* linear = s->ensureLinear(maybeCx);
if (!linear) {
return false;
return true;
}
size_t length = JS::GetDeflatedUTF8StringLength(linear);
char* buffer = reserve(length);
if (!buffer) {
return false;
return true;
}
mozilla::DebugOnly<size_t> written =
@ -273,14 +277,6 @@ bool Sprinter::putString(JSString* s) {
return true;
}
void Sprinter::reportOutOfMemory() {
if (hadOOM_) {
return;
}
hadOOM_ = true;
forwardOutOfMemory();
}
size_t Sprinter::length() const { return size_t(offset); }
void Sprinter::forwardOutOfMemory() {