• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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_TRACKER_H_
6 #define V8_HEAP_ARRAY_BUFFER_TRACKER_H_
7 
8 #include <unordered_map>
9 
10 #include "src/allocation.h"
11 #include "src/base/platform/mutex.h"
12 #include "src/globals.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 class Heap;
18 class JSArrayBuffer;
19 class Page;
20 
21 class ArrayBufferTracker : public AllStatic {
22  public:
23   enum ProcessingMode {
24     kUpdateForwardedRemoveOthers,
25     kUpdateForwardedKeepOthers,
26   };
27 
28   // The following methods are used to track raw C++ pointers to externally
29   // allocated memory used as backing store in live array buffers.
30 
31   // Register/unregister a new JSArrayBuffer |buffer| for tracking. Guards all
32   // access to the tracker by taking the page lock for the corresponding page.
33   inline static void RegisterNew(Heap* heap, JSArrayBuffer* buffer);
34   inline static void Unregister(Heap* heap, JSArrayBuffer* buffer);
35 
36   // Frees all backing store pointers for dead JSArrayBuffers in new space.
37   // Does not take any locks and can only be called during Scavenge.
38   static void FreeDeadInNewSpace(Heap* heap);
39 
40   // Frees all backing store pointers for dead JSArrayBuffer on a given page.
41   // Requires marking information to be present. Requires the page lock to be
42   // taken by the caller.
43   static void FreeDead(Page* page);
44 
45   // Frees all remaining, live or dead, array buffers on a page. Only useful
46   // during tear down.
47   static void FreeAll(Page* page);
48 
49   // Processes all array buffers on a given page. |mode| specifies the action
50   // to perform on the buffers. Returns whether the tracker is empty or not.
51   static bool ProcessBuffers(Page* page, ProcessingMode mode);
52 
53   // Returns whether a buffer is currently tracked.
54   static bool IsTracked(JSArrayBuffer* buffer);
55 };
56 
57 // LocalArrayBufferTracker tracks internalized array buffers.
58 //
59 // Never use directly but instead always call through |ArrayBufferTracker|.
60 class LocalArrayBufferTracker {
61  public:
62   typedef std::pair<void*, size_t> Value;
63   typedef JSArrayBuffer* Key;
64 
65   enum CallbackResult { kKeepEntry, kUpdateEntry, kRemoveEntry };
66   enum FreeMode { kFreeDead, kFreeAll };
67 
LocalArrayBufferTracker(Heap * heap)68   explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap) {}
69   ~LocalArrayBufferTracker();
70 
71   inline void Add(Key key, const Value& value);
72   inline Value Remove(Key key);
73 
74   // Frees up array buffers determined by |free_mode|.
75   template <FreeMode free_mode>
76   void Free();
77 
78   // Processes buffers one by one. The CallbackResult of the callback decides
79   // what action to take on the buffer.
80   //
81   // Callback should be of type:
82   //   CallbackResult fn(JSArrayBuffer* buffer, JSArrayBuffer** new_buffer);
83   template <typename Callback>
84   inline void Process(Callback callback);
85 
IsEmpty()86   bool IsEmpty() { return array_buffers_.empty(); }
87 
IsTracked(Key key)88   bool IsTracked(Key key) {
89     return array_buffers_.find(key) != array_buffers_.end();
90   }
91 
92  private:
93   typedef std::unordered_map<Key, Value> TrackingMap;
94 
95   Heap* heap_;
96   TrackingMap array_buffers_;
97 };
98 
99 }  // namespace internal
100 }  // namespace v8
101 #endif  // V8_HEAP_ARRAY_BUFFER_TRACKER_H_
102