Commit f17d44a2 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Cleanup memory allocation for array buffers

Instead of calling {TryAllocateBackingStore} with
{enable_guard_regions==false}, we just directly call
{array_buffer_allocator->Allocate}.
Drive-by optimization: Don't allocate if the size is 0.

R=titzer@chromium.org

Change-Id: Iabf7af7e0f1bc970c03efcd9ee4c23e5307a7095
Reviewed-on: https://chromium-review.googlesource.com/548398Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46218}
parent 1ec6671f
......@@ -948,10 +948,8 @@ MaybeHandle<WasmInstanceObject> InstanceBuilder::Build() {
//--------------------------------------------------------------------------
for (WasmDataSegment& seg : module_->data_segments) {
uint32_t base = EvalUint32InitExpr(seg.dest_addr);
uint32_t mem_size =
memory_.is_null()
? 0
: static_cast<uint32_t>(memory_->byte_length()->Number());
uint32_t mem_size = 0;
if (!memory_.is_null()) CHECK(memory_->byte_length()->ToUint32(&mem_size));
if (!in_bounds(base, seg.source.length(), mem_size)) {
thrower_->LinkError("data segment is out of bounds");
return {};
......
......@@ -80,7 +80,8 @@ void* TryAllocateBackingStore(Isolate* isolate, size_t size,
return memory;
} else {
void* memory = isolate->array_buffer_allocator()->Allocate(size);
void* memory =
size == 0 ? nullptr : isolate->array_buffer_allocator()->Allocate(size);
allocation_base = memory;
allocation_length = size;
return memory;
......@@ -276,7 +277,7 @@ Handle<JSArrayBuffer> wasm::NewArrayBuffer(Isolate* isolate, size_t size,
void* memory = TryAllocateBackingStore(isolate, size, enable_guard_regions,
allocation_base, allocation_length);
if (memory == nullptr) {
if (size > 0 && memory == nullptr) {
return Handle<JSArrayBuffer>::null();
}
......@@ -288,7 +289,7 @@ Handle<JSArrayBuffer> wasm::NewArrayBuffer(Isolate* isolate, size_t size,
}
#endif
const bool is_external = false;
constexpr bool is_external = false;
return SetupArrayBuffer(isolate, allocation_base, allocation_length, memory,
size, is_external, enable_guard_regions);
}
......@@ -706,34 +707,26 @@ Handle<JSArray> wasm::GetCustomSections(Isolate* isolate,
if (!name->Equals(*section_name.ToHandleChecked())) continue;
// Make a copy of the payload data in the section.
void* allocation_base = nullptr; // Set by TryAllocateBackingStore
size_t allocation_length = 0; // Set by TryAllocateBackingStore
const bool enable_guard_regions = false;
void* memory = TryAllocateBackingStore(isolate, section.payload.length(),
enable_guard_regions,
allocation_base, allocation_length);
Handle<Object> section_data = factory->undefined_value();
if (memory) {
Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
const bool is_external = false;
JSArrayBuffer::Setup(buffer, isolate, is_external, allocation_base,
allocation_length, memory,
static_cast<int>(section.payload.length()));
DisallowHeapAllocation no_gc; // for raw access to string bytes.
Handle<SeqOneByteString> module_bytes(compiled_module->module_bytes(),
isolate);
const byte* start =
reinterpret_cast<const byte*>(module_bytes->GetCharsAddress());
memcpy(memory, start + section.payload.offset(),
section.payload.length());
section_data = buffer;
} else {
size_t size = section.payload.length();
void* memory =
size == 0 ? nullptr : isolate->array_buffer_allocator()->Allocate(size);
if (size && !memory) {
thrower->RangeError("out of memory allocating custom section data");
return Handle<JSArray>();
}
Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
constexpr bool is_external = false;
JSArrayBuffer::Setup(buffer, isolate, is_external, memory, size, memory,
size);
DisallowHeapAllocation no_gc; // for raw access to string bytes.
Handle<SeqOneByteString> module_bytes(compiled_module->module_bytes(),
isolate);
const byte* start =
reinterpret_cast<const byte*>(module_bytes->GetCharsAddress());
memcpy(memory, start + section.payload.offset(), section.payload.length());
matching_sections.push_back(section_data);
matching_sections.push_back(buffer);
}
int num_custom_sections = static_cast<int>(matching_sections.size());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment