Chromium Code Reviews| 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(JSArrayBuffer* buffer) { | |
| 16 void* data = buffer->backing_store(); | |
| 17 if (!data) return; | |
| 18 | |
| 19 bool in_new_space = heap()->InNewSpace(buffer); | |
| 20 size_t length = NumberToSize(heap()->isolate(), buffer->byte_length()); | |
| 21 if (in_new_space) { | |
| 22 live_array_buffers_for_scavenge_[data] = length; | |
| 23 } else { | |
| 24 live_array_buffers_[data] = length; | |
| 25 } | |
| 26 | |
| 27 // We may go over the limit of externally allocated memory here. We call the | |
| 28 // api function to trigger a GC in this case. | |
| 29 reinterpret_cast<v8::Isolate*>(heap()->isolate()) | |
| 30 ->AdjustAmountOfExternalAllocatedMemory(length); | |
| 31 } | |
| 32 | |
| 33 | |
| 34 void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) { | |
| 35 void* data = buffer->backing_store(); | |
| 36 if (!data) return; | |
| 37 | |
| 38 bool in_new_space = heap()->InNewSpace(buffer); | |
| 39 std::map<void*, size_t>* live_buffers = | |
| 40 in_new_space ? &live_array_buffers_for_scavenge_ : &live_array_buffers_; | |
| 41 std::map<void*, size_t>* not_yet_discovered_buffers = | |
| 42 in_new_space ? ¬_yet_discovered_array_buffers_for_scavenge_ | |
| 43 : ¬_yet_discovered_array_buffers_; | |
| 44 | |
| 45 DCHECK(live_buffers->count(data) > 0); | |
| 46 | |
| 47 size_t length = (*live_buffers)[data]; | |
| 48 live_buffers->erase(data); | |
| 49 not_yet_discovered_buffers->erase(data); | |
| 50 | |
| 51 heap()->update_amount_of_external_allocated_memory( | |
| 52 -static_cast<int64_t>(length)); | |
| 53 } | |
| 54 | |
| 55 | |
| 56 void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) { | |
| 57 void* data = buffer->backing_store(); | |
| 58 | |
| 59 // ArrayBuffer might be in the middle of being constructed. | |
| 60 if (data == heap()->undefined_value()) return; | |
| 61 if (heap()->InNewSpace(buffer)) { | |
| 62 not_yet_discovered_array_buffers_for_scavenge_.erase(data); | |
| 63 } else { | |
| 64 not_yet_discovered_array_buffers_.erase(data); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 | |
| 69 void ArrayBufferTracker::FreeDead(bool from_scavenge) { | |
| 70 size_t freed_memory = 0; | |
| 71 Isolate* isolate = heap()->isolate(); | |
| 72 for (auto& buffer : not_yet_discovered_array_buffers_for_scavenge_) { | |
| 73 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); | |
| 74 freed_memory += buffer.second; | |
| 75 live_array_buffers_for_scavenge_.erase(buffer.first); | |
| 76 } | |
| 77 | |
| 78 if (!from_scavenge) { | |
| 79 for (auto& buffer : not_yet_discovered_array_buffers_) { | |
| 80 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); | |
| 81 freed_memory += buffer.second; | |
| 82 live_array_buffers_.erase(buffer.first); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 not_yet_discovered_array_buffers_for_scavenge_ = | |
| 87 live_array_buffers_for_scavenge_; | |
| 88 if (!from_scavenge) not_yet_discovered_array_buffers_ = live_array_buffers_; | |
| 89 | |
| 90 // Do not call through the api as this code is triggered while doing a GC. | |
| 91 heap()->update_amount_of_external_allocated_memory( | |
| 92 -static_cast<int64_t>(freed_memory)); | |
| 93 } | |
| 94 | |
| 95 | |
| 96 void ArrayBufferTracker::PrepareDiscoveryInNewSpace() { | |
| 97 not_yet_discovered_array_buffers_for_scavenge_ = | |
| 98 live_array_buffers_for_scavenge_; | |
| 99 } | |
| 100 | |
| 101 | |
| 102 void ArrayBufferTracker::Promote(JSArrayBuffer* buffer) { | |
| 103 if (buffer->is_external()) return; | |
| 104 void* data = buffer->backing_store(); | |
| 105 if (!data) return; | |
| 106 // ArrayBuffer might be in the middle of being constructed. | |
| 107 if (data == heap()->undefined_value()) return; | |
| 108 DCHECK(live_array_buffers_for_scavenge_.count(data) > 0); | |
| 109 live_array_buffers_[data] = live_array_buffers_for_scavenge_[data]; | |
| 110 live_array_buffers_for_scavenge_.erase(data); | |
| 111 not_yet_discovered_array_buffers_for_scavenge_.erase(data); | |
| 112 } | |
| 113 | |
| 114 | |
| 115 void ArrayBufferTracker::TearDown() { | |
| 116 Isolate* isolate = heap()->isolate(); | |
| 117 size_t freed_memory = 0; | |
| 118 for (auto& buffer : live_array_buffers_) { | |
| 119 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); | |
| 120 freed_memory += buffer.second; | |
| 121 } | |
| 122 for (auto& buffer : live_array_buffers_for_scavenge_) { | |
| 123 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); | |
| 124 freed_memory += buffer.second; | |
| 125 } | |
| 126 live_array_buffers_.clear(); | |
| 127 live_array_buffers_for_scavenge_.clear(); | |
| 128 not_yet_discovered_array_buffers_.clear(); | |
| 129 not_yet_discovered_array_buffers_for_scavenge_.clear(); | |
| 130 | |
| 131 if (freed_memory > 0) { | |
| 132 reinterpret_cast<v8::Isolate*>(isolate) | |
|
Michael Lippautz
2015/09/07 06:39:33
Use update_amount_of_external_allocated_memory().
fedor.indutny
2015/09/07 07:05:25
Acknowledged.
| |
| 133 ->AdjustAmountOfExternalAllocatedMemory( | |
| 134 -static_cast<int64_t>(freed_memory)); | |
| 135 } | |
| 136 } | |
| 137 } // namespace internal | |
| 138 } // namespace v8 | |
| OLD | NEW |