array-buffer-tracker-inl.h 4.38 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5 6 7
#ifndef V8_HEAP_ARRAY_BUFFER_TRACKER_INL_H_
#define V8_HEAP_ARRAY_BUFFER_TRACKER_INL_H_

8 9
#include "src/conversions-inl.h"
#include "src/heap/array-buffer-tracker.h"
10 11
#include "src/heap/heap-inl.h"
#include "src/heap/spaces-inl.h"
12
#include "src/objects.h"
13
#include "src/objects/js-array-buffer-inl.h"
14 15 16 17

namespace v8 {
namespace internal {

18
void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer buffer) {
19
  if (buffer->backing_store() == nullptr) return;
20

21 22 23
  // ArrayBuffer tracking works only for small objects.
  DCHECK(!heap->IsLargeObject(buffer));

24
  const size_t length = buffer->byte_length();
25
  Page* page = Page::FromHeapObject(buffer);
26
  {
27
    base::MutexGuard guard(page->mutex());
28 29 30 31 32 33
    LocalArrayBufferTracker* tracker = page->local_tracker();
    if (tracker == nullptr) {
      page->AllocateLocalTracker();
      tracker = page->local_tracker();
    }
    DCHECK_NOT_NULL(tracker);
34
    tracker->Add(buffer, length);
35
  }
36 37 38 39 40 41

  // TODO(wez): Remove backing-store from external memory accounting.
  // 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);
42 43
}

44
void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer buffer) {
45
  if (buffer->backing_store() == nullptr) return;
46

47
  Page* page = Page::FromHeapObject(buffer);
48
  const size_t length = buffer->byte_length();
49
  {
50
    base::MutexGuard guard(page->mutex());
51 52
    LocalArrayBufferTracker* tracker = page->local_tracker();
    DCHECK_NOT_NULL(tracker);
53
    tracker->Remove(buffer, length);
54
  }
55 56 57

  // TODO(wez): Remove backing-store from external memory accounting.
  heap->update_external_memory(-static_cast<intptr_t>(length));
58 59
}

60 61
Space* LocalArrayBufferTracker::space() { return page_->owner(); }

62 63
template <typename Callback>
void LocalArrayBufferTracker::Free(Callback should_free) {
64
  size_t freed_memory = 0;
65
  Isolate* isolate = page_->heap()->isolate();
66 67
  for (TrackingData::iterator it = array_buffers_.begin();
       it != array_buffers_.end();) {
68 69
    // Unchecked cast because the map might already be dead at this point.
    JSArrayBuffer buffer = JSArrayBuffer::unchecked_cast(it->first);
70
    const size_t length = it->second.length;
71

72
    if (should_free(buffer)) {
73
      JSArrayBuffer::FreeBackingStore(isolate, it->second);
74
      it = array_buffers_.erase(it);
75
      freed_memory += length;
76 77 78 79 80
    } else {
      ++it;
    }
  }
  if (freed_memory > 0) {
81 82
    page_->DecrementExternalBackingStoreBytes(
        ExternalBackingStoreType::kArrayBuffer, freed_memory);
83 84 85 86

    // TODO(wez): Remove backing-store from external memory accounting.
    page_->heap()->update_external_memory_concurrently_freed(
        static_cast<intptr_t>(freed_memory));
87 88 89 90 91 92 93 94
  }
}

template <typename MarkingState>
void ArrayBufferTracker::FreeDead(Page* page, MarkingState* marking_state) {
  // Callers need to ensure having the page lock.
  LocalArrayBufferTracker* tracker = page->local_tracker();
  if (tracker == nullptr) return;
95
  tracker->Free([marking_state](JSArrayBuffer buffer) {
96 97 98 99 100 101 102
    return marking_state->IsWhite(buffer);
  });
  if (tracker->IsEmpty()) {
    page->ReleaseLocalTracker();
  }
}

103
void LocalArrayBufferTracker::Add(JSArrayBuffer buffer, size_t length) {
104 105
  page_->IncrementExternalBackingStoreBytes(
      ExternalBackingStoreType::kArrayBuffer, length);
106

107 108 109
  AddInternal(buffer, length);
}

110
void LocalArrayBufferTracker::AddInternal(JSArrayBuffer buffer, size_t length) {
111 112 113 114
  auto ret = array_buffers_.insert(
      {buffer,
       {buffer->backing_store(), length, buffer->backing_store(),
        buffer->is_wasm_memory()}});
115 116 117 118 119 120
  USE(ret);
  // Check that we indeed inserted a new value and did not overwrite an existing
  // one (which would be a bug).
  DCHECK(ret.second);
}

121
void LocalArrayBufferTracker::Remove(JSArrayBuffer buffer, size_t length) {
122 123
  page_->DecrementExternalBackingStoreBytes(
      ExternalBackingStoreType::kArrayBuffer, length);
124

125
  TrackingData::iterator it = array_buffers_.find(buffer);
126
  // Check that we indeed find a key to remove.
127
  DCHECK(it != array_buffers_.end());
128
  DCHECK_EQ(length, it->second.length);
129 130 131 132 133
  array_buffers_.erase(it);
}

}  // namespace internal
}  // namespace v8
134 135

#endif  // V8_HEAP_ARRAY_BUFFER_TRACKER_INL_H_