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