OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/heap/array-buffer-tracker.h" |
| 6 #include "src/heap/heap.h" |
| 7 #include "src/isolate.h" |
| 8 #include "src/objects.h" |
| 9 #include "src/objects-inl.h" |
| 10 #include "src/v8.h" |
| 11 |
| 12 namespace v8 { |
| 13 namespace internal { |
| 14 |
| 15 void ArrayBufferTracker::RegisterNew(bool in_new_space, void* data, |
| 16 size_t length) { |
| 17 if (!data) return; |
| 18 if (in_new_space) { |
| 19 live_array_buffers_for_scavenge_[data] = length; |
| 20 } else { |
| 21 live_array_buffers_[data] = length; |
| 22 } |
| 23 |
| 24 // We may go over the limit of externally allocated memory here. We call the |
| 25 // api function to trigger a GC in this case. |
| 26 reinterpret_cast<v8::Isolate*>(heap()->isolate()) |
| 27 ->AdjustAmountOfExternalAllocatedMemory(length); |
| 28 } |
| 29 |
| 30 |
| 31 void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) { |
| 32 bool in_new_space = heap()->InNewSpace(buffer); |
| 33 void* data = buffer->backing_store(); |
| 34 if (!data) return; |
| 35 |
| 36 std::map<void*, size_t>* live_buffers = |
| 37 in_new_space ? &live_array_buffers_for_scavenge_ : &live_array_buffers_; |
| 38 std::map<void*, size_t>* not_yet_discovered_buffers = |
| 39 in_new_space ? ¬_yet_discovered_array_buffers_for_scavenge_ |
| 40 : ¬_yet_discovered_array_buffers_; |
| 41 |
| 42 DCHECK(live_buffers->count(data) > 0); |
| 43 |
| 44 size_t length = (*live_buffers)[data]; |
| 45 live_buffers->erase(data); |
| 46 not_yet_discovered_buffers->erase(data); |
| 47 |
| 48 heap()->update_amount_of_external_allocated_memory( |
| 49 -static_cast<int64_t>(length)); |
| 50 } |
| 51 |
| 52 |
| 53 void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) { |
| 54 void* data = buffer->backing_store(); |
| 55 |
| 56 // ArrayBuffer might be in the middle of being constructed. |
| 57 if (data == heap()->undefined_value()) return; |
| 58 if (heap()->InNewSpace(buffer)) { |
| 59 not_yet_discovered_array_buffers_for_scavenge_.erase(data); |
| 60 } else { |
| 61 not_yet_discovered_array_buffers_.erase(data); |
| 62 } |
| 63 } |
| 64 |
| 65 |
| 66 void ArrayBufferTracker::FreeDead(bool from_scavenge) { |
| 67 size_t freed_memory = 0; |
| 68 Isolate* isolate = heap()->isolate(); |
| 69 for (auto& buffer : not_yet_discovered_array_buffers_for_scavenge_) { |
| 70 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| 71 freed_memory += buffer.second; |
| 72 live_array_buffers_for_scavenge_.erase(buffer.first); |
| 73 } |
| 74 |
| 75 if (!from_scavenge) { |
| 76 for (auto& buffer : not_yet_discovered_array_buffers_) { |
| 77 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| 78 freed_memory += buffer.second; |
| 79 live_array_buffers_.erase(buffer.first); |
| 80 } |
| 81 } |
| 82 |
| 83 not_yet_discovered_array_buffers_for_scavenge_ = |
| 84 live_array_buffers_for_scavenge_; |
| 85 if (!from_scavenge) not_yet_discovered_array_buffers_ = live_array_buffers_; |
| 86 |
| 87 // Do not call through the api as this code is triggered while doing a GC. |
| 88 heap()->update_amount_of_external_allocated_memory( |
| 89 -static_cast<int64_t>(freed_memory)); |
| 90 } |
| 91 |
| 92 |
| 93 void ArrayBufferTracker::PrepareDiscoveryInNewSpace() { |
| 94 not_yet_discovered_array_buffers_for_scavenge_ = |
| 95 live_array_buffers_for_scavenge_; |
| 96 } |
| 97 |
| 98 |
| 99 void ArrayBufferTracker::Promote(JSArrayBuffer* buffer) { |
| 100 if (buffer->is_external()) return; |
| 101 void* data = buffer->backing_store(); |
| 102 if (!data) return; |
| 103 // ArrayBuffer might be in the middle of being constructed. |
| 104 if (data == heap()->undefined_value()) return; |
| 105 DCHECK(live_array_buffers_for_scavenge_.count(data) > 0); |
| 106 live_array_buffers_[data] = live_array_buffers_for_scavenge_[data]; |
| 107 live_array_buffers_for_scavenge_.erase(data); |
| 108 not_yet_discovered_array_buffers_for_scavenge_.erase(data); |
| 109 } |
| 110 |
| 111 |
| 112 void ArrayBufferTracker::TearDown() { |
| 113 Isolate* isolate = heap()->isolate(); |
| 114 size_t freed_memory = 0; |
| 115 for (auto& buffer : live_array_buffers_) { |
| 116 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| 117 freed_memory += buffer.second; |
| 118 } |
| 119 for (auto& buffer : live_array_buffers_for_scavenge_) { |
| 120 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| 121 freed_memory += buffer.second; |
| 122 } |
| 123 live_array_buffers_.clear(); |
| 124 live_array_buffers_for_scavenge_.clear(); |
| 125 not_yet_discovered_array_buffers_.clear(); |
| 126 not_yet_discovered_array_buffers_for_scavenge_.clear(); |
| 127 |
| 128 if (freed_memory > 0) { |
| 129 reinterpret_cast<v8::Isolate*>(isolate) |
| 130 ->AdjustAmountOfExternalAllocatedMemory( |
| 131 -static_cast<int64_t>(freed_memory)); |
| 132 } |
| 133 } |
| 134 } |
| 135 } // namespace v8::internal |
OLD | NEW |