• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/heap/array-buffer-collector.h"
6 
7 #include "src/base/template-utils.h"
8 #include "src/heap/array-buffer-tracker.h"
9 #include "src/heap/gc-tracer.h"
10 #include "src/heap/heap-inl.h"
11 
12 namespace v8 {
13 namespace internal {
14 
AddGarbageAllocations(std::vector<JSArrayBuffer::Allocation> allocations)15 void ArrayBufferCollector::AddGarbageAllocations(
16     std::vector<JSArrayBuffer::Allocation> allocations) {
17   base::LockGuard<base::Mutex> guard(&allocations_mutex_);
18   allocations_.push_back(std::move(allocations));
19 }
20 
FreeAllocations()21 void ArrayBufferCollector::FreeAllocations() {
22   base::LockGuard<base::Mutex> guard(&allocations_mutex_);
23   for (const std::vector<JSArrayBuffer::Allocation>& allocations :
24        allocations_) {
25     for (JSArrayBuffer::Allocation alloc : allocations) {
26       JSArrayBuffer::FreeBackingStore(heap_->isolate(), alloc);
27     }
28   }
29   allocations_.clear();
30 }
31 
32 class ArrayBufferCollector::FreeingTask final : public CancelableTask {
33  public:
FreeingTask(Heap * heap)34   explicit FreeingTask(Heap* heap)
35       : CancelableTask(heap->isolate()), heap_(heap) {}
36 
~FreeingTask()37   virtual ~FreeingTask() {}
38 
39  private:
RunInternal()40   void RunInternal() final {
41     TRACE_BACKGROUND_GC(
42         heap_->tracer(),
43         GCTracer::BackgroundScope::BACKGROUND_ARRAY_BUFFER_FREE);
44     heap_->array_buffer_collector()->FreeAllocations();
45   }
46 
47   Heap* heap_;
48 };
49 
FreeAllocationsOnBackgroundThread()50 void ArrayBufferCollector::FreeAllocationsOnBackgroundThread() {
51   // TODO(wez): Remove backing-store from external memory accounting.
52   heap_->account_external_memory_concurrently_freed();
53   if (!heap_->IsTearingDown() && FLAG_concurrent_array_buffer_freeing) {
54     V8::GetCurrentPlatform()->CallOnWorkerThread(
55         base::make_unique<FreeingTask>(heap_));
56   } else {
57     // Fallback for when concurrency is disabled/restricted.
58     FreeAllocations();
59   }
60 }
61 
62 }  // namespace internal
63 }  // namespace v8
64