From 61bf8abd754a7ee5e2fba49db1b192ee18e1c540 Mon Sep 17 00:00:00 2001 From: Ryan Macnak Date: Wed, 19 Oct 2016 16:37:59 -0700 Subject: [PATCH] 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 . --- runtime/platform/assert.h | 3 +++ runtime/platform/hashmap.cc | 4 +--- runtime/platform/text_buffer.cc | 7 ++++++- runtime/vm/clustered_snapshot.cc | 2 +- runtime/vm/scavenger.cc | 4 ++-- runtime/vm/service.cc | 6 ++++++ runtime/vm/zone.cc | 2 +- 7 files changed, 20 insertions(+), 8 deletions(-) diff --git a/runtime/platform/assert.h b/runtime/platform/assert.h index 9c810c5b332..81cb7f11039 100644 --- a/runtime/platform/assert.h +++ b/runtime/platform/assert.h @@ -254,6 +254,9 @@ T DynamicAssertionHelper::NotNull(const T p) { #define UNREACHABLE() \ FATAL("unreachable code") +#define OUT_OF_MEMORY() \ + FATAL("Out of memory.") + #if defined(DEBUG) // DEBUG binaries use assertions in the code. diff --git a/runtime/platform/hashmap.cc b/runtime/platform/hashmap.cc index 34f7ea1284f..46115339812 100644 --- a/runtime/platform/hashmap.cc +++ b/runtime/platform/hashmap.cc @@ -166,9 +166,7 @@ void HashMap::Initialize(uint32_t capacity) { ASSERT(dart::Utils::IsPowerOfTwo(capacity)); map_ = new Entry[capacity]; if (map_ == NULL) { - // TODO(sgjesse): Handle out of memory. - FATAL("Cannot allocate memory for hashmap"); - return; + OUT_OF_MEMORY(); } capacity_ = capacity; occupancy_ = 0; diff --git a/runtime/platform/text_buffer.cc b/runtime/platform/text_buffer.cc index 44152e6a2b1..b06c49fd80b 100644 --- a/runtime/platform/text_buffer.cc +++ b/runtime/platform/text_buffer.cc @@ -15,6 +15,9 @@ namespace dart { TextBuffer::TextBuffer(intptr_t buf_size) { ASSERT(buf_size > 0); buf_ = reinterpret_cast(malloc(buf_size)); + if (buf_ == NULL) { + OUT_OF_MEMORY(); + } buf_size_ = buf_size; Clear(); } @@ -152,7 +155,9 @@ void TextBuffer::EnsureCapacity(intptr_t len) { // the debugger front-end. intptr_t new_size = buf_size_ + len + kBufferSpareCapacity; char* new_buf = reinterpret_cast(realloc(buf_, new_size)); - ASSERT(new_buf != NULL); + if (new_buf == NULL) { + OUT_OF_MEMORY(); + } buf_ = new_buf; buf_size_ = new_size; } diff --git a/runtime/vm/clustered_snapshot.cc b/runtime/vm/clustered_snapshot.cc index 1799db88ae9..baff5439384 100644 --- a/runtime/vm/clustered_snapshot.cc +++ b/runtime/vm/clustered_snapshot.cc @@ -28,7 +28,7 @@ static RawObject* AllocateUninitialized(PageSpace* old_space, intptr_t size) { uword address = old_space->TryAllocateDataBumpLocked(size, PageSpace::kForceGrowth); if (address == 0) { - FATAL("Out of memory"); + OUT_OF_MEMORY(); } return reinterpret_cast(address + kHeapObjectTag); } diff --git a/runtime/vm/scavenger.cc b/runtime/vm/scavenger.cc index 50319095dd6..e5e21e14c7b 100644 --- a/runtime/vm/scavenger.cc +++ b/runtime/vm/scavenger.cc @@ -347,7 +347,7 @@ Scavenger::Scavenger(Heap* heap, (FLAG_new_gen_growth_factor * FLAG_new_gen_growth_factor); to_ = SemiSpace::New(initial_semi_capacity_in_words); if (to_ == NULL) { - FATAL("Out of memory.\n"); + OUT_OF_MEMORY(); } // Setup local fields. top_ = FirstObjectStart(); @@ -393,7 +393,7 @@ SemiSpace* Scavenger::Prologue(Isolate* isolate, bool invoke_api_callbacks) { if (to_ == NULL) { // TODO(koda): We could try to recover (collect old space, wait for another // isolate to finish scavenge, etc.). - FATAL("Out of memory.\n"); + OUT_OF_MEMORY(); } UpdateMaxHeapCapacity(); top_ = FirstObjectStart(); diff --git a/runtime/vm/service.cc b/runtime/vm/service.cc index abd42717465..c01256e9ffa 100644 --- a/runtime/vm/service.cc +++ b/runtime/vm/service.cc @@ -222,6 +222,9 @@ RawObject* Service::RequestAssets() { static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { void* new_ptr = realloc(reinterpret_cast(ptr), new_size); + if (new_ptr == NULL) { + OUT_OF_MEMORY(); + } return reinterpret_cast(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; uint8_t* message = static_cast(malloc(total_bytes)); + if (message == NULL) { + OUT_OF_MEMORY(); + } intptr_t offset = 0; // Metadata size. diff --git a/runtime/vm/zone.cc b/runtime/vm/zone.cc index ac79f356b6c..bace1f3534d 100644 --- a/runtime/vm/zone.cc +++ b/runtime/vm/zone.cc @@ -59,7 +59,7 @@ Zone::Segment* Zone::Segment::New(intptr_t size, Zone::Segment* next) { ASSERT(size >= 0); Segment* result = reinterpret_cast(malloc(size)); if (result == NULL) { - FATAL("Out of memory.\n"); + OUT_OF_MEMORY(); } ASSERT(Utils::IsAligned(result->start(), Zone::kAlignment)); #ifdef DEBUG