Make fatal out of memory messages uniform.

Add checks in a few more places with large allocations.

R=fschneider@google.com

Review URL: https://codereview.chromium.org/2418323002 .
This commit is contained in:
Ryan Macnak 2016-10-19 16:37:59 -07:00
parent c9c33d9db3
commit 61bf8abd75
7 changed files with 20 additions and 8 deletions

View file

@ -254,6 +254,9 @@ T DynamicAssertionHelper::NotNull(const T p) {
#define UNREACHABLE() \ #define UNREACHABLE() \
FATAL("unreachable code") FATAL("unreachable code")
#define OUT_OF_MEMORY() \
FATAL("Out of memory.")
#if defined(DEBUG) #if defined(DEBUG)
// DEBUG binaries use assertions in the code. // DEBUG binaries use assertions in the code.

View file

@ -166,9 +166,7 @@ void HashMap::Initialize(uint32_t capacity) {
ASSERT(dart::Utils::IsPowerOfTwo(capacity)); ASSERT(dart::Utils::IsPowerOfTwo(capacity));
map_ = new Entry[capacity]; map_ = new Entry[capacity];
if (map_ == NULL) { if (map_ == NULL) {
// TODO(sgjesse): Handle out of memory. OUT_OF_MEMORY();
FATAL("Cannot allocate memory for hashmap");
return;
} }
capacity_ = capacity; capacity_ = capacity;
occupancy_ = 0; occupancy_ = 0;

View file

@ -15,6 +15,9 @@ namespace dart {
TextBuffer::TextBuffer(intptr_t buf_size) { TextBuffer::TextBuffer(intptr_t buf_size) {
ASSERT(buf_size > 0); ASSERT(buf_size > 0);
buf_ = reinterpret_cast<char*>(malloc(buf_size)); buf_ = reinterpret_cast<char*>(malloc(buf_size));
if (buf_ == NULL) {
OUT_OF_MEMORY();
}
buf_size_ = buf_size; buf_size_ = buf_size;
Clear(); Clear();
} }
@ -152,7 +155,9 @@ void TextBuffer::EnsureCapacity(intptr_t len) {
// the debugger front-end. // the debugger front-end.
intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; intptr_t new_size = buf_size_ + len + kBufferSpareCapacity;
char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size)); char* new_buf = reinterpret_cast<char*>(realloc(buf_, new_size));
ASSERT(new_buf != NULL); if (new_buf == NULL) {
OUT_OF_MEMORY();
}
buf_ = new_buf; buf_ = new_buf;
buf_size_ = new_size; buf_size_ = new_size;
} }

View file

@ -28,7 +28,7 @@ static RawObject* AllocateUninitialized(PageSpace* old_space, intptr_t size) {
uword address = old_space->TryAllocateDataBumpLocked(size, uword address = old_space->TryAllocateDataBumpLocked(size,
PageSpace::kForceGrowth); PageSpace::kForceGrowth);
if (address == 0) { if (address == 0) {
FATAL("Out of memory"); OUT_OF_MEMORY();
} }
return reinterpret_cast<RawObject*>(address + kHeapObjectTag); return reinterpret_cast<RawObject*>(address + kHeapObjectTag);
} }

View file

@ -347,7 +347,7 @@ Scavenger::Scavenger(Heap* heap,
(FLAG_new_gen_growth_factor * FLAG_new_gen_growth_factor); (FLAG_new_gen_growth_factor * FLAG_new_gen_growth_factor);
to_ = SemiSpace::New(initial_semi_capacity_in_words); to_ = SemiSpace::New(initial_semi_capacity_in_words);
if (to_ == NULL) { if (to_ == NULL) {
FATAL("Out of memory.\n"); OUT_OF_MEMORY();
} }
// Setup local fields. // Setup local fields.
top_ = FirstObjectStart(); top_ = FirstObjectStart();
@ -393,7 +393,7 @@ SemiSpace* Scavenger::Prologue(Isolate* isolate, bool invoke_api_callbacks) {
if (to_ == NULL) { if (to_ == NULL) {
// TODO(koda): We could try to recover (collect old space, wait for another // TODO(koda): We could try to recover (collect old space, wait for another
// isolate to finish scavenge, etc.). // isolate to finish scavenge, etc.).
FATAL("Out of memory.\n"); OUT_OF_MEMORY();
} }
UpdateMaxHeapCapacity(); UpdateMaxHeapCapacity();
top_ = FirstObjectStart(); top_ = FirstObjectStart();

View file

@ -222,6 +222,9 @@ RawObject* Service::RequestAssets() {
static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size);
if (new_ptr == NULL) {
OUT_OF_MEMORY();
}
return reinterpret_cast<uint8_t*>(new_ptr); return reinterpret_cast<uint8_t*>(new_ptr);
} }
@ -1038,6 +1041,9 @@ void Service::SendEventWithData(const char* stream_id,
const intptr_t total_bytes = sizeof(uint64_t) + metadata_size + data_size; const intptr_t total_bytes = sizeof(uint64_t) + metadata_size + data_size;
uint8_t* message = static_cast<uint8_t*>(malloc(total_bytes)); uint8_t* message = static_cast<uint8_t*>(malloc(total_bytes));
if (message == NULL) {
OUT_OF_MEMORY();
}
intptr_t offset = 0; intptr_t offset = 0;
// Metadata size. // Metadata size.

View file

@ -59,7 +59,7 @@ Zone::Segment* Zone::Segment::New(intptr_t size, Zone::Segment* next) {
ASSERT(size >= 0); ASSERT(size >= 0);
Segment* result = reinterpret_cast<Segment*>(malloc(size)); Segment* result = reinterpret_cast<Segment*>(malloc(size));
if (result == NULL) { if (result == NULL) {
FATAL("Out of memory.\n"); OUT_OF_MEMORY();
} }
ASSERT(Utils::IsAligned(result->start(), Zone::kAlignment)); ASSERT(Utils::IsAligned(result->start(), Zone::kAlignment));
#ifdef DEBUG #ifdef DEBUG