Index: src/heap/array-buffer-tracker.cc |
diff --git a/src/heap/array-buffer-tracker.cc b/src/heap/array-buffer-tracker.cc |
index 6e389c1cbf6abe216c782bae30fedcba5b283443..7f7fb622502f5679ac7077fe948c29e589822c47 100644 |
--- a/src/heap/array-buffer-tracker.cc |
+++ b/src/heap/array-buffer-tracker.cc |
@@ -5,136 +5,172 @@ |
#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/objects.h" |
#include "src/v8.h" |
namespace v8 { |
namespace internal { |
-ArrayBufferTracker::~ArrayBufferTracker() { |
- Isolate* isolate = heap()->isolate(); |
+LocalArrayBufferTracker::~LocalArrayBufferTracker() { |
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 : array_buffers_) { |
+ heap_->isolate()->array_buffer_allocator()->Free(buffer.second.first, |
+ buffer.second.second); |
+ freed_memory += buffer.second.second; |
} |
- for (auto& buffer : live_array_buffers_for_scavenge_) { |
- isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
- freed_memory += buffer.second; |
+ if (freed_memory > 0) { |
+ heap_->update_amount_of_external_allocated_freed_memory( |
+ static_cast<intptr_t>(freed_memory)); |
} |
- live_array_buffers_.clear(); |
- live_array_buffers_for_scavenge_.clear(); |
- not_yet_discovered_array_buffers_.clear(); |
- not_yet_discovered_array_buffers_for_scavenge_.clear(); |
+ array_buffers_.clear(); |
+} |
+ |
+void LocalArrayBufferTracker::Add(Key key, const Value& value) { |
+ array_buffers_[key] = value; |
+} |
+ |
+LocalArrayBufferTracker::Value LocalArrayBufferTracker::Remove(Key key) { |
+ DCHECK_EQ(array_buffers_.count(key), 1); |
+ Value value = array_buffers_[key]; |
+ array_buffers_.erase(key); |
+ return value; |
+} |
+void LocalArrayBufferTracker::FreeDead() { |
+ size_t freed_memory = 0; |
+ for (TrackingMap::iterator it = array_buffers_.begin(); |
+ it != array_buffers_.end();) { |
+ if (Marking::IsWhite(Marking::MarkBitFrom(it->first))) { |
+ heap_->isolate()->array_buffer_allocator()->Free(it->second.first, |
+ it->second.second); |
+ freed_memory += it->second.second; |
+ array_buffers_.erase(it++); |
+ } else { |
+ it++; |
+ } |
+ } |
if (freed_memory > 0) { |
- heap()->update_amount_of_external_allocated_memory( |
- -static_cast<int64_t>(freed_memory)); |
+ heap_->update_amount_of_external_allocated_freed_memory( |
+ static_cast<intptr_t>(freed_memory)); |
} |
} |
+template <typename Callback> |
+void LocalArrayBufferTracker::Process(Callback callback) { |
+ JSArrayBuffer* new_buffer = nullptr; |
+ size_t freed_memory = 0; |
+ for (TrackingMap::iterator it = array_buffers_.begin(); |
+ it != array_buffers_.end();) { |
+ switch (callback(it->first, &new_buffer)) { |
+ case kKeepEntry: |
+ it++; |
+ break; |
+ case kUpdateEntry: |
+ DCHECK_NOT_NULL(new_buffer); |
+ Page::FromAddress(new_buffer->address()) |
+ ->local_tracker<Page::kCreateIfNotPresent>() |
+ ->Add(new_buffer, it->second); |
+ array_buffers_.erase(it++); |
+ break; |
+ case kRemoveEntry: |
+ heap_->isolate()->array_buffer_allocator()->Free(it->second.first, |
+ it->second.second); |
+ freed_memory += it->second.second; |
+ array_buffers_.erase(it++); |
+ break; |
+ default: |
+ UNREACHABLE(); |
+ } |
+ } |
+ if (freed_memory > 0) { |
+ heap_->update_amount_of_external_allocated_freed_memory( |
+ static_cast<intptr_t>(freed_memory)); |
+ } |
+} |
-void ArrayBufferTracker::RegisterNew(JSArrayBuffer* buffer) { |
+void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) { |
void* data = buffer->backing_store(); |
if (!data) return; |
- bool in_new_space = heap()->InNewSpace(buffer); |
- size_t length = NumberToSize(heap()->isolate(), buffer->byte_length()); |
- if (in_new_space) { |
- live_array_buffers_for_scavenge_[data] = length; |
- } else { |
- live_array_buffers_[data] = length; |
+ size_t length = NumberToSize(heap->isolate(), buffer->byte_length()); |
+ Page* page = Page::FromAddress(buffer->address()); |
+ LocalArrayBufferTracker* tracker = |
+ page->local_tracker<Page::kCreateIfNotPresent>(); |
+ DCHECK_NOT_NULL(tracker); |
+ { |
+ base::LockGuard<base::Mutex> guard(page->mutex()); |
+ tracker->Add(buffer, std::make_pair(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()) |
+ reinterpret_cast<v8::Isolate*>(heap->isolate()) |
->AdjustAmountOfExternalAllocatedMemory(length); |
} |
- |
-void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) { |
+void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer* buffer) { |
void* data = buffer->backing_store(); |
if (!data) return; |
- bool in_new_space = heap()->InNewSpace(buffer); |
- 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) { |
- base::LockGuard<base::Mutex> guard(&mutex_); |
- 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); |
+ Page* page = Page::FromAddress(buffer->address()); |
+ LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>(); |
+ DCHECK_NOT_NULL(tracker); |
+ size_t length = 0; |
+ { |
+ base::LockGuard<base::Mutex> guard(page->mutex()); |
+ length = tracker->Remove(buffer).second; |
} |
+ heap->update_amount_of_external_allocated_memory( |
+ -static_cast<intptr_t>(length)); |
} |
- |
-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); |
+void ArrayBufferTracker::FreeDeadInNewSpace(Heap* heap) { |
+ NewSpacePageIterator from_it(heap->new_space()->FromSpaceStart(), |
+ heap->new_space()->FromSpaceEnd()); |
+ while (from_it.has_next()) { |
+ ProcessBuffers(from_it.next(), kUpdateForwardedRemoveOthers); |
} |
+ heap->account_amount_of_external_allocated_freed_memory(); |
+} |
- 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); |
- } |
+void ArrayBufferTracker::FreeDead(Page* page) { |
+ // Only called from the sweeper, which already holds the page lock. |
+ LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>(); |
+ if (tracker == nullptr) return; |
+ DCHECK(!page->SweepingDone()); |
+ tracker->FreeDead(); |
+ if (tracker->IsEmpty()) { |
+ page->ReleaseLocalTracker(); |
} |
- |
- 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::ProcessBuffers(Page* page, ProcessingMode mode) { |
+ LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>(); |
+ if (tracker == nullptr) return; |
+ { |
+ base::LockGuard<base::Mutex> guard(page->mutex()); |
+ tracker->Process( |
+ [mode](JSArrayBuffer* old_buffer, JSArrayBuffer** new_buffer) { |
+ MapWord map_word = old_buffer->map_word(); |
+ if (map_word.IsForwardingAddress()) { |
+ *new_buffer = JSArrayBuffer::cast(map_word.ToForwardingAddress()); |
+ return LocalArrayBufferTracker::kUpdateEntry; |
+ } |
+ return mode == kUpdateForwardedKeepOthers |
+ ? LocalArrayBufferTracker::kKeepEntry |
+ : LocalArrayBufferTracker::kRemoveEntry; |
+ }); |
+ } |
} |
- |
-void ArrayBufferTracker::Promote(JSArrayBuffer* buffer) { |
- base::LockGuard<base::Mutex> guard(&mutex_); |
- |
- 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); |
+bool ArrayBufferTracker::IsTracked(JSArrayBuffer* buffer) { |
+ Page* page = Page::FromAddress(buffer->address()); |
+ LocalArrayBufferTracker* tracker = |
+ page->local_tracker<Page::kCreateIfNotPresent>(); |
+ { |
+ base::LockGuard<base::Mutex> guard(page->mutex()); |
+ return tracker->IsTracked(buffer); |
+ } |
} |
} // namespace internal |