aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/src/heap/array-buffer-collector.cc
blob: b6d7df8191f0f60c757e361b49ae089953d15a16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2017 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-collector.h"

#include "src/base/template-utils.h"
#include "src/heap/array-buffer-tracker.h"
#include "src/heap/gc-tracer.h"
#include "src/heap/heap-inl.h"
#include "src/tasks/cancelable-task.h"
#include "src/tasks/task-utils.h"

namespace v8 {
namespace internal {

namespace {

void FreeAllocationsHelper(
    Heap* heap, const std::vector<JSArrayBuffer::Allocation>& allocations) {
  for (JSArrayBuffer::Allocation alloc : allocations) {
    JSArrayBuffer::FreeBackingStore(heap->isolate(), alloc);
  }
}

}  // namespace

void ArrayBufferCollector::QueueOrFreeGarbageAllocations(
    std::vector<JSArrayBuffer::Allocation> allocations) {
  if (heap_->ShouldReduceMemory()) {
    FreeAllocationsHelper(heap_, allocations);
  } else {
    base::MutexGuard guard(&allocations_mutex_);
    allocations_.push_back(std::move(allocations));
  }
}

void ArrayBufferCollector::PerformFreeAllocations() {
  base::MutexGuard guard(&allocations_mutex_);
  for (const std::vector<JSArrayBuffer::Allocation>& allocations :
       allocations_) {
    FreeAllocationsHelper(heap_, allocations);
  }
  allocations_.clear();
}

void ArrayBufferCollector::FreeAllocations() {
  // TODO(wez): Remove backing-store from external memory accounting.
  heap_->account_external_memory_concurrently_freed();
  if (!heap_->IsTearingDown() && !heap_->ShouldReduceMemory() &&
      FLAG_concurrent_array_buffer_freeing) {
    V8::GetCurrentPlatform()->CallOnWorkerThread(
        MakeCancelableTask(heap_->isolate(), [this] {
          TRACE_BACKGROUND_GC(
              heap_->tracer(),
              GCTracer::BackgroundScope::BACKGROUND_ARRAY_BUFFER_FREE);
          PerformFreeAllocations();
        }));
  } else {
    // Fallback for when concurrency is disabled/restricted. This is e.g. the
    // case when the GC should reduce memory. For such GCs the
    // QueueOrFreeGarbageAllocations() call would immediately free the
    // allocations and this call would free already queued ones.
    PerformFreeAllocations();
  }
}

}  // namespace internal
}  // namespace v8