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 #ifndef V8_HEAP_ARRAY_BUFFER_COLLECTOR_H_
6 #define V8_HEAP_ARRAY_BUFFER_COLLECTOR_H_
7 
8 #include <vector>
9 
10 #include "src/base/platform/mutex.h"
11 #include "src/objects/js-array-buffer.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class Heap;
17 
18 // To support background processing of array buffer backing stores, we process
19 // array buffers using the ArrayBufferTracker class. The ArrayBufferCollector
20 // keeps track of garbage backing stores so that they can be freed on a
21 // background thread.
22 class ArrayBufferCollector {
23  public:
ArrayBufferCollector(Heap * heap)24   explicit ArrayBufferCollector(Heap* heap) : heap_(heap) {}
25 
~ArrayBufferCollector()26   ~ArrayBufferCollector() { FreeAllocations(); }
27 
28   // These allocations will begin to be freed once FreeAllocations() is called,
29   // or on TearDown.
30   void AddGarbageAllocations(
31       std::vector<JSArrayBuffer::Allocation> allocations);
32 
33   // Calls FreeAllocations() on a background thread.
34   void FreeAllocationsOnBackgroundThread();
35 
36  private:
37   class FreeingTask;
38 
39   // Begin freeing the allocations added through AddGarbageAllocations. Also
40   // called by TearDown.
41   void FreeAllocations();
42 
43   Heap* heap_;
44   base::Mutex allocations_mutex_;
45   std::vector<std::vector<JSArrayBuffer::Allocation>> allocations_;
46 };
47 
48 }  // namespace internal
49 }  // namespace v8
50 
51 #endif  // V8_HEAP_ARRAY_BUFFER_COLLECTOR_H_
52