5 #ifndef V8_HEAP_ARRAY_BUFFER_TRACKER_INL_H_ 6 #define V8_HEAP_ARRAY_BUFFER_TRACKER_INL_H_ 8 #include "src/conversions-inl.h" 9 #include "src/heap/array-buffer-tracker.h" 10 #include "src/heap/heap.h" 11 #include "src/heap/spaces.h" 12 #include "src/objects.h" 13 #include "src/objects/js-array-buffer-inl.h" 18 void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) {
19 if (buffer->backing_store() ==
nullptr)
return;
21 const size_t length = buffer->byte_length();
22 Page* page = Page::FromAddress(buffer->address());
24 base::MutexGuard guard(page->mutex());
25 LocalArrayBufferTracker* tracker = page->local_tracker();
26 if (tracker ==
nullptr) {
27 page->AllocateLocalTracker();
28 tracker = page->local_tracker();
30 DCHECK_NOT_NULL(tracker);
31 tracker->Add(buffer, length);
37 reinterpret_cast<v8::Isolate*
>(heap->isolate())
38 ->AdjustAmountOfExternalAllocatedMemory(length);
41 void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer* buffer) {
42 if (buffer->backing_store() ==
nullptr)
return;
44 Page* page = Page::FromAddress(buffer->address());
45 const size_t length = buffer->byte_length();
47 base::MutexGuard guard(page->mutex());
48 LocalArrayBufferTracker* tracker = page->local_tracker();
49 DCHECK_NOT_NULL(tracker);
50 tracker->Remove(buffer, length);
54 heap->update_external_memory(-static_cast<intptr_t>(length));
57 Space* LocalArrayBufferTracker::space() {
return page_->owner(); }
59 template <
typename Callback>
60 void LocalArrayBufferTracker::Free(Callback should_free) {
61 size_t freed_memory = 0;
62 Isolate* isolate = page_->heap()->isolate();
63 for (TrackingData::iterator it = array_buffers_.begin();
64 it != array_buffers_.end();) {
65 JSArrayBuffer* buffer =
reinterpret_cast<JSArrayBuffer*
>(it->first);
66 const size_t length = it->second.length;
68 if (should_free(buffer)) {
69 JSArrayBuffer::FreeBackingStore(isolate, it->second);
70 it = array_buffers_.erase(it);
71 freed_memory += length;
76 if (freed_memory > 0) {
77 page_->DecrementExternalBackingStoreBytes(
78 ExternalBackingStoreType::kArrayBuffer, freed_memory);
81 page_->heap()->update_external_memory_concurrently_freed(
82 static_cast<intptr_t>(freed_memory));
86 template <
typename MarkingState>
87 void ArrayBufferTracker::FreeDead(Page* page, MarkingState* marking_state) {
89 LocalArrayBufferTracker* tracker = page->local_tracker();
90 if (tracker ==
nullptr)
return;
91 tracker->Free([marking_state](JSArrayBuffer* buffer) {
92 return marking_state->IsWhite(buffer);
94 if (tracker->IsEmpty()) {
95 page->ReleaseLocalTracker();
99 void LocalArrayBufferTracker::Add(JSArrayBuffer* buffer,
size_t length) {
100 page_->IncrementExternalBackingStoreBytes(
101 ExternalBackingStoreType::kArrayBuffer, length);
103 AddInternal(buffer, length);
106 void LocalArrayBufferTracker::AddInternal(JSArrayBuffer* buffer,
108 auto ret = array_buffers_.insert(
110 {buffer->backing_store(), length, buffer->backing_store(),
111 buffer->is_wasm_memory()}});
118 void LocalArrayBufferTracker::Remove(JSArrayBuffer* buffer,
size_t length) {
119 page_->DecrementExternalBackingStoreBytes(
120 ExternalBackingStoreType::kArrayBuffer, length);
122 TrackingData::iterator it = array_buffers_.find(buffer);
124 DCHECK(it != array_buffers_.end());
125 DCHECK_EQ(length, it->second.length);
126 array_buffers_.erase(it);
132 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_INL_H_