OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 5 #ifndef V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
6 #define V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 6 #define V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "src/allocation.h" | |
11 #include "src/base/platform/mutex.h" | 10 #include "src/base/platform/mutex.h" |
12 #include "src/globals.h" | 11 #include "src/globals.h" |
13 | 12 |
14 namespace v8 { | 13 namespace v8 { |
15 namespace internal { | 14 namespace internal { |
16 | 15 |
17 // Forward declarations. | 16 // Forward declarations. |
18 class Heap; | 17 class Heap; |
19 class Page; | |
20 class JSArrayBuffer; | 18 class JSArrayBuffer; |
21 | 19 |
22 // LocalArrayBufferTracker is tracker for live and dead JSArrayBuffer objects. | 20 class ArrayBufferTracker { |
23 // | |
24 // It consists of two sets, a live, and a not yet discovered set of buffers. | |
25 // Upon registration (in the ArrayBufferTracker) the buffers are added to both | |
26 // sets. When a buffer is encountered as live (or added is live) it is removed | |
27 // from the not yet discovered set. Finally, after each round (sometime during | |
28 // GC) the left over not yet discovered buffers are cleaned up. Upon starting | |
29 // a new round the not yet discovered buffers are initialized from the live set. | |
30 // | |
31 // Caveats: | |
32 // - Between cleaning up the buffers using |Free| we always need a |Reset| and | |
33 // thus another marking phase. | |
34 // - LocalArrayBufferTracker is completely unlocked. Calls need to ensure | |
35 // exclusive access. | |
36 class LocalArrayBufferTracker { | |
37 public: | 21 public: |
38 typedef std::pair<void*, size_t> Value; | 22 explicit ArrayBufferTracker(Heap* heap) : heap_(heap) {} |
39 typedef JSArrayBuffer* Key; | 23 ~ArrayBufferTracker(); |
40 | 24 |
41 enum LivenessIndicator { | 25 inline Heap* heap() { return heap_; } |
42 kForwardingPointer, | |
43 kMarkBit, | |
44 kForwardingPointerOrMarkBit | |
45 }; | |
46 enum CallbackResult { kKeepEntry, kKeepAndUpdateEntry, kRemoveEntry }; | |
47 | 26 |
48 explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap), started_(false) {} | |
49 ~LocalArrayBufferTracker(); | |
50 | |
51 void Add(Key key, const Value& value); | |
52 void AddLive(Key key, const Value& value); | |
53 Value Remove(Key key); | |
54 void MarkLive(Key key); | |
55 bool IsEmpty(); | |
56 | |
57 // Resets the tracking set, i.e., not yet discovered buffers are initialized | |
58 // from the remaining live set of buffers. | |
59 void Reset(); | |
60 | |
61 // Frees up any dead backing stores of not yet discovered array buffers. | |
62 // Requires that the buffers have been properly marked using MarkLive. | |
63 void FreeDead(); | |
64 | |
65 // Scans the whole tracker and decides based on liveness_indicator whether | |
66 // a JSArrayBuffer is still considered live. | |
67 template <LivenessIndicator liveness_indicator> | |
68 inline void ScanAndFreeDead(); | |
69 | |
70 bool IsTracked(Key key) { return live_.find(key) != live_.end(); } | |
71 | |
72 private: | |
73 // TODO(mlippautz): Switch to unordered_map once it is supported on all | |
74 // platforms. | |
75 typedef std::map<Key, Value> TrackingMap; | |
76 | |
77 // Processes buffers one by one. The CallbackResult decides whether the buffer | |
78 // will be dropped or not. | |
79 // | |
80 // Callback should be of type: | |
81 // CallbackResult fn(JSArrayBuffer*, JSArrayBuffer**); | |
82 template <typename Callback> | |
83 inline void Process(Callback callback); | |
84 | |
85 Heap* heap_; | |
86 | |
87 // |live_| maps tracked JSArrayBuffers to the internally allocated backing | |
88 // store and length. For each GC round |not_yet_discovered_| is initialized | |
89 // as a copy of |live_|. Upon finding a JSArrayBuffer during GC, the buffer | |
90 // is removed from |not_yet_discovered_|. At the end of a GC, we free up the | |
91 // remaining JSArrayBuffers in |not_yet_discovered_|. | |
92 TrackingMap live_; | |
93 TrackingMap not_yet_discovered_; | |
94 | |
95 bool started_; | |
96 }; | |
97 | |
98 class ArrayBufferTracker : public AllStatic { | |
99 public: | |
100 // The following methods are used to track raw C++ pointers to externally | 27 // The following methods are used to track raw C++ pointers to externally |
101 // allocated memory used as backing store in live array buffers. | 28 // allocated memory used as backing store in live array buffers. |
102 | 29 |
103 // Register/unregister a new JSArrayBuffer |buffer| for tracking. | 30 // A new ArrayBuffer was created with |data| as backing store. |
104 static void RegisterNew(Heap* heap, JSArrayBuffer* buffer); | 31 void RegisterNew(JSArrayBuffer* buffer); |
105 static void Unregister(Heap* heap, JSArrayBuffer* buffer); | |
106 | 32 |
107 // Frees all backing store pointers for dead JSArrayBuffers in new space. | 33 // The backing store |data| is no longer owned by V8. |
108 static void FreeDeadInNewSpace(Heap* heap); | 34 void Unregister(JSArrayBuffer* buffer); |
109 | 35 |
110 static void FreeDead(Page* page); | 36 // A live ArrayBuffer was discovered during marking/scavenge. |
| 37 void MarkLive(JSArrayBuffer* buffer); |
111 | 38 |
112 template <LocalArrayBufferTracker::LivenessIndicator liveness_indicator> | 39 // Frees all backing store pointers that weren't discovered in the previous |
113 static void ScanAndFreeDeadArrayBuffers(Page* page); | 40 // marking or scavenge phase. |
| 41 void FreeDead(bool from_scavenge); |
114 | 42 |
115 // A live JSArrayBuffer was discovered during marking. | 43 // Prepare for a new scavenge phase. A new marking phase is implicitly |
116 static void MarkLive(Heap* heap, JSArrayBuffer* buffer); | 44 // prepared by finishing the previous one. |
| 45 void PrepareDiscoveryInNewSpace(); |
117 | 46 |
118 // Resets all trackers in old space. Is required to be called from the main | 47 // An ArrayBuffer moved from new space to old space. |
119 // thread. | 48 void Promote(JSArrayBuffer* buffer); |
120 static void ResetTrackersInOldSpace(Heap* heap); | |
121 | 49 |
122 static bool IsTracked(JSArrayBuffer* buffer); | 50 private: |
| 51 base::Mutex mutex_; |
| 52 Heap* heap_; |
| 53 |
| 54 // |live_array_buffers_| maps externally allocated memory used as backing |
| 55 // store for ArrayBuffers to the length of the respective memory blocks. |
| 56 // |
| 57 // At the beginning of mark/compact, |not_yet_discovered_array_buffers_| is |
| 58 // a copy of |live_array_buffers_| and we remove pointers as we discover live |
| 59 // ArrayBuffer objects during marking. At the end of mark/compact, the |
| 60 // remaining memory blocks can be freed. |
| 61 std::map<void*, size_t> live_array_buffers_; |
| 62 std::map<void*, size_t> not_yet_discovered_array_buffers_; |
| 63 |
| 64 // To be able to free memory held by ArrayBuffers during scavenge as well, we |
| 65 // have a separate list of allocated memory held by ArrayBuffers in new space. |
| 66 // |
| 67 // Since mark/compact also evacuates the new space, all pointers in the |
| 68 // |live_array_buffers_for_scavenge_| list are also in the |
| 69 // |live_array_buffers_| list. |
| 70 std::map<void*, size_t> live_array_buffers_for_scavenge_; |
| 71 std::map<void*, size_t> not_yet_discovered_array_buffers_for_scavenge_; |
123 }; | 72 }; |
124 | |
125 } // namespace internal | 73 } // namespace internal |
126 } // namespace v8 | 74 } // namespace v8 |
127 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 75 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
OLD | NEW |