| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
| 8 #include "src/runtime/runtime.h" | 8 #include "src/runtime/runtime.h" |
| 9 #include "src/runtime/runtime-utils.h" | 9 #include "src/runtime/runtime-utils.h" |
| 10 | 10 |
| 11 | 11 |
| 12 namespace v8 { | 12 namespace v8 { |
| 13 namespace internal { | 13 namespace internal { |
| 14 | 14 |
| 15 void Runtime::FreeArrayBuffer(Isolate* isolate, | |
| 16 JSArrayBuffer* phantom_array_buffer) { | |
| 17 if (phantom_array_buffer->is_external()) return; | |
| 18 | |
| 19 size_t allocated_length = | |
| 20 NumberToSize(isolate, phantom_array_buffer->byte_length()); | |
| 21 | |
| 22 reinterpret_cast<v8::Isolate*>(isolate) | |
| 23 ->AdjustAmountOfExternalAllocatedMemory( | |
| 24 -static_cast<int64_t>(allocated_length)); | |
| 25 CHECK(isolate->array_buffer_allocator() != NULL); | |
| 26 isolate->array_buffer_allocator()->Free(phantom_array_buffer->backing_store(), | |
| 27 allocated_length); | |
| 28 } | |
| 29 | |
| 30 | |
| 31 void Runtime::SetupArrayBuffer(Isolate* isolate, | 15 void Runtime::SetupArrayBuffer(Isolate* isolate, |
| 32 Handle<JSArrayBuffer> array_buffer, | 16 Handle<JSArrayBuffer> array_buffer, |
| 33 bool is_external, void* data, | 17 bool is_external, void* data, |
| 34 size_t allocated_length) { | 18 size_t allocated_length) { |
| 35 DCHECK(array_buffer->GetInternalFieldCount() == | 19 DCHECK(array_buffer->GetInternalFieldCount() == |
| 36 v8::ArrayBuffer::kInternalFieldCount); | 20 v8::ArrayBuffer::kInternalFieldCount); |
| 37 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { | 21 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { |
| 38 array_buffer->SetInternalField(i, Smi::FromInt(0)); | 22 array_buffer->SetInternalField(i, Smi::FromInt(0)); |
| 39 } | 23 } |
| 40 array_buffer->set_backing_store(data); | 24 array_buffer->set_backing_store(data); |
| 41 array_buffer->set_bit_field(0); | 25 array_buffer->set_bit_field(0); |
| 42 array_buffer->set_is_external(is_external); | 26 array_buffer->set_is_external(is_external); |
| 43 array_buffer->set_is_neuterable(true); | 27 array_buffer->set_is_neuterable(true); |
| 44 | 28 |
| 45 Handle<Object> byte_length = | 29 Handle<Object> byte_length = |
| 46 isolate->factory()->NewNumberFromSize(allocated_length); | 30 isolate->factory()->NewNumberFromSize(allocated_length); |
| 47 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); | 31 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); |
| 48 array_buffer->set_byte_length(*byte_length); | 32 array_buffer->set_byte_length(*byte_length); |
| 49 | 33 |
| 50 if (isolate->heap()->InNewSpace(*array_buffer) || | 34 if (data && !is_external) { |
| 51 isolate->heap()->array_buffers_list()->IsUndefined()) { | 35 isolate->heap()->RegisterNewArrayBuffer(data, allocated_length); |
| 52 array_buffer->set_weak_next(isolate->heap()->array_buffers_list()); | |
| 53 isolate->heap()->set_array_buffers_list(*array_buffer); | |
| 54 if (isolate->heap()->last_array_buffer_in_list()->IsUndefined()) { | |
| 55 isolate->heap()->set_last_array_buffer_in_list(*array_buffer); | |
| 56 } | |
| 57 } else { | |
| 58 JSArrayBuffer::cast(isolate->heap()->last_array_buffer_in_list()) | |
| 59 ->set_weak_next(*array_buffer); | |
| 60 isolate->heap()->set_last_array_buffer_in_list(*array_buffer); | |
| 61 } | 36 } |
| 62 } | 37 } |
| 63 | 38 |
| 64 | 39 |
| 65 bool Runtime::SetupArrayBufferAllocatingData(Isolate* isolate, | 40 bool Runtime::SetupArrayBufferAllocatingData(Isolate* isolate, |
| 66 Handle<JSArrayBuffer> array_buffer, | 41 Handle<JSArrayBuffer> array_buffer, |
| 67 size_t allocated_length, | 42 size_t allocated_length, |
| 68 bool initialize) { | 43 bool initialize) { |
| 69 void* data; | 44 void* data; |
| 70 CHECK(isolate->array_buffer_allocator() != NULL); | 45 CHECK(isolate->array_buffer_allocator() != NULL); |
| 71 // Prevent creating array buffers when serializing. | 46 // Prevent creating array buffers when serializing. |
| 72 DCHECK(!isolate->serializer_enabled()); | 47 DCHECK(!isolate->serializer_enabled()); |
| 73 if (allocated_length != 0) { | 48 if (allocated_length != 0) { |
| 74 if (initialize) { | 49 if (initialize) { |
| 75 data = isolate->array_buffer_allocator()->Allocate(allocated_length); | 50 data = isolate->array_buffer_allocator()->Allocate(allocated_length); |
| 76 } else { | 51 } else { |
| 77 data = isolate->array_buffer_allocator()->AllocateUninitialized( | 52 data = isolate->array_buffer_allocator()->AllocateUninitialized( |
| 78 allocated_length); | 53 allocated_length); |
| 79 } | 54 } |
| 80 if (data == NULL) return false; | 55 if (data == NULL) return false; |
| 81 } else { | 56 } else { |
| 82 data = NULL; | 57 data = NULL; |
| 83 } | 58 } |
| 84 | 59 |
| 85 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length); | 60 SetupArrayBuffer(isolate, array_buffer, false, data, allocated_length); |
| 86 | |
| 87 reinterpret_cast<v8::Isolate*>(isolate) | |
| 88 ->AdjustAmountOfExternalAllocatedMemory(allocated_length); | |
| 89 | |
| 90 return true; | 61 return true; |
| 91 } | 62 } |
| 92 | 63 |
| 93 | 64 |
| 94 void Runtime::NeuterArrayBuffer(Handle<JSArrayBuffer> array_buffer) { | 65 void Runtime::NeuterArrayBuffer(Handle<JSArrayBuffer> array_buffer) { |
| 95 array_buffer->Neuter(); | 66 array_buffer->Neuter(); |
| 96 } | 67 } |
| 97 | 68 |
| 98 | 69 |
| 99 RUNTIME_FUNCTION(Runtime_ArrayBufferInitialize) { | 70 RUNTIME_FUNCTION(Runtime_ArrayBufferInitialize) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); | 137 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, array_buffer, 0); |
| 167 if (array_buffer->backing_store() == NULL) { | 138 if (array_buffer->backing_store() == NULL) { |
| 168 CHECK(Smi::FromInt(0) == array_buffer->byte_length()); | 139 CHECK(Smi::FromInt(0) == array_buffer->byte_length()); |
| 169 return isolate->heap()->undefined_value(); | 140 return isolate->heap()->undefined_value(); |
| 170 } | 141 } |
| 171 DCHECK(!array_buffer->is_external()); | 142 DCHECK(!array_buffer->is_external()); |
| 172 void* backing_store = array_buffer->backing_store(); | 143 void* backing_store = array_buffer->backing_store(); |
| 173 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length()); | 144 size_t byte_length = NumberToSize(isolate, array_buffer->byte_length()); |
| 174 array_buffer->set_is_external(true); | 145 array_buffer->set_is_external(true); |
| 175 Runtime::NeuterArrayBuffer(array_buffer); | 146 Runtime::NeuterArrayBuffer(array_buffer); |
| 147 isolate->heap()->UnregisterArrayBuffer(backing_store); |
| 176 isolate->array_buffer_allocator()->Free(backing_store, byte_length); | 148 isolate->array_buffer_allocator()->Free(backing_store, byte_length); |
| 177 return isolate->heap()->undefined_value(); | 149 return isolate->heap()->undefined_value(); |
| 178 } | 150 } |
| 179 | 151 |
| 180 | 152 |
| 181 void Runtime::ArrayIdToTypeAndSize(int arrayId, ExternalArrayType* array_type, | 153 void Runtime::ArrayIdToTypeAndSize(int arrayId, ExternalArrayType* array_type, |
| 182 ElementsKind* external_elements_kind, | 154 ElementsKind* external_elements_kind, |
| 183 ElementsKind* fixed_elements_kind, | 155 ElementsKind* fixed_elements_kind, |
| 184 size_t* element_size) { | 156 size_t* element_size) { |
| 185 switch (arrayId) { | 157 switch (arrayId) { |
| (...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 DATA_VIEW_SETTER(Uint16, uint16_t) | 717 DATA_VIEW_SETTER(Uint16, uint16_t) |
| 746 DATA_VIEW_SETTER(Int16, int16_t) | 718 DATA_VIEW_SETTER(Int16, int16_t) |
| 747 DATA_VIEW_SETTER(Uint32, uint32_t) | 719 DATA_VIEW_SETTER(Uint32, uint32_t) |
| 748 DATA_VIEW_SETTER(Int32, int32_t) | 720 DATA_VIEW_SETTER(Int32, int32_t) |
| 749 DATA_VIEW_SETTER(Float32, float) | 721 DATA_VIEW_SETTER(Float32, float) |
| 750 DATA_VIEW_SETTER(Float64, double) | 722 DATA_VIEW_SETTER(Float64, double) |
| 751 | 723 |
| 752 #undef DATA_VIEW_SETTER | 724 #undef DATA_VIEW_SETTER |
| 753 } | 725 } |
| 754 } // namespace v8::internal | 726 } // namespace v8::internal |
| OLD | NEW |