Chromium Code Reviews| Index: src/heap/array-buffer-tracker.cc |
| diff --git a/src/heap/array-buffer-tracker.cc b/src/heap/array-buffer-tracker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f7b6c19d36d49c8950b44f095a6820a91062526d |
| --- /dev/null |
| +++ b/src/heap/array-buffer-tracker.cc |
| @@ -0,0 +1,135 @@ |
| +// Copyright 2015 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "src/heap/array-buffer-tracker.h" |
| +#include "src/heap/heap.h" |
| +#include "src/isolate.h" |
| +#include "src/objects.h" |
| +#include "src/objects-inl.h" |
| +#include "src/v8.h" |
| + |
| +namespace v8 { |
| +namespace internal { |
| + |
| +void ArrayBufferTracker::RegisterNew(bool in_new_space, void* data, |
| + size_t length) { |
| + if (!data) return; |
| + if (in_new_space) { |
| + live_array_buffers_for_scavenge_[data] = length; |
| + } else { |
| + live_array_buffers_[data] = length; |
| + } |
| + |
| + // We may go over the limit of externally allocated memory here. We call the |
| + // api function to trigger a GC in this case. |
| + reinterpret_cast<v8::Isolate*>(heap()->isolate()) |
| + ->AdjustAmountOfExternalAllocatedMemory(length); |
| +} |
| + |
| + |
| +void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) { |
| + bool in_new_space = heap()->InNewSpace(buffer); |
|
Michael Lippautz
2015/09/04 09:09:56
Move below !data check.
fedor.indutny
2015/09/04 17:21:01
Acknowledged.
|
| + void* data = buffer->backing_store(); |
| + if (!data) return; |
| + |
| + std::map<void*, size_t>* live_buffers = |
| + in_new_space ? &live_array_buffers_for_scavenge_ : &live_array_buffers_; |
| + std::map<void*, size_t>* not_yet_discovered_buffers = |
| + in_new_space ? ¬_yet_discovered_array_buffers_for_scavenge_ |
| + : ¬_yet_discovered_array_buffers_; |
| + |
| + DCHECK(live_buffers->count(data) > 0); |
| + |
| + size_t length = (*live_buffers)[data]; |
| + live_buffers->erase(data); |
| + not_yet_discovered_buffers->erase(data); |
| + |
| + heap()->update_amount_of_external_allocated_memory( |
| + -static_cast<int64_t>(length)); |
| +} |
| + |
| + |
| +void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) { |
| + void* data = buffer->backing_store(); |
| + |
| + // ArrayBuffer might be in the middle of being constructed. |
| + if (data == heap()->undefined_value()) return; |
| + if (heap()->InNewSpace(buffer)) { |
| + not_yet_discovered_array_buffers_for_scavenge_.erase(data); |
| + } else { |
| + not_yet_discovered_array_buffers_.erase(data); |
| + } |
| +} |
| + |
| + |
| +void ArrayBufferTracker::FreeDead(bool from_scavenge) { |
| + size_t freed_memory = 0; |
| + Isolate* isolate = heap()->isolate(); |
| + for (auto& buffer : not_yet_discovered_array_buffers_for_scavenge_) { |
| + isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| + freed_memory += buffer.second; |
| + live_array_buffers_for_scavenge_.erase(buffer.first); |
| + } |
| + |
| + if (!from_scavenge) { |
| + for (auto& buffer : not_yet_discovered_array_buffers_) { |
| + isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| + freed_memory += buffer.second; |
| + live_array_buffers_.erase(buffer.first); |
| + } |
| + } |
| + |
| + not_yet_discovered_array_buffers_for_scavenge_ = |
| + live_array_buffers_for_scavenge_; |
| + if (!from_scavenge) not_yet_discovered_array_buffers_ = live_array_buffers_; |
| + |
| + // Do not call through the api as this code is triggered while doing a GC. |
| + heap()->update_amount_of_external_allocated_memory( |
| + -static_cast<int64_t>(freed_memory)); |
| +} |
| + |
| + |
| +void ArrayBufferTracker::PrepareDiscoveryInNewSpace() { |
| + not_yet_discovered_array_buffers_for_scavenge_ = |
| + live_array_buffers_for_scavenge_; |
| +} |
| + |
| + |
| +void ArrayBufferTracker::Promote(JSArrayBuffer* buffer) { |
| + if (buffer->is_external()) return; |
| + void* data = buffer->backing_store(); |
| + if (!data) return; |
| + // ArrayBuffer might be in the middle of being constructed. |
| + if (data == heap()->undefined_value()) return; |
| + DCHECK(live_array_buffers_for_scavenge_.count(data) > 0); |
| + live_array_buffers_[data] = live_array_buffers_for_scavenge_[data]; |
| + live_array_buffers_for_scavenge_.erase(data); |
| + not_yet_discovered_array_buffers_for_scavenge_.erase(data); |
| +} |
| + |
| + |
| +void ArrayBufferTracker::TearDown() { |
| + Isolate* isolate = heap()->isolate(); |
| + size_t freed_memory = 0; |
| + for (auto& buffer : live_array_buffers_) { |
| + isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| + freed_memory += buffer.second; |
| + } |
| + for (auto& buffer : live_array_buffers_for_scavenge_) { |
| + isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| + freed_memory += buffer.second; |
| + } |
| + live_array_buffers_.clear(); |
| + live_array_buffers_for_scavenge_.clear(); |
| + not_yet_discovered_array_buffers_.clear(); |
| + not_yet_discovered_array_buffers_for_scavenge_.clear(); |
| + |
| + if (freed_memory > 0) { |
| + reinterpret_cast<v8::Isolate*>(isolate) |
| + ->AdjustAmountOfExternalAllocatedMemory( |
| + -static_cast<int64_t>(freed_memory)); |
| + } |
| +} |
|
Michael Lippautz
2015/09/04 09:09:56
nit:
}
} // namespace internal
} // namespace v
fedor.indutny
2015/09/04 17:21:01
Acknowledged.
|
| +} |
| +} // namespace v8::internal |