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/base/platform/mutex.h" | 10 #include "src/base/platform/mutex.h" |
11 #include "src/globals.h" | 11 #include "src/globals.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 | 15 |
16 // Forward declarations. | 16 // Forward declarations. |
17 class Heap; | 17 class Heap; |
18 class JSArrayBuffer; | 18 class JSArrayBuffer; |
19 | 19 |
| 20 class LocalArrayBufferTracker { |
| 21 public: |
| 22 typedef std::pair<void*, size_t> Value; |
| 23 typedef JSArrayBuffer* Key; |
| 24 |
| 25 enum CallbackResult { kKeepEntry, kRemoveEntry }; |
| 26 |
| 27 explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap) {} |
| 28 ~LocalArrayBufferTracker(); |
| 29 |
| 30 void Add(Key key, const Value& value); |
| 31 void AddLive(Key key, const Value& value); |
| 32 Value Remove(Key key); |
| 33 void MarkLive(Key key); |
| 34 bool IsEmpty(); |
| 35 |
| 36 // Frees up any dead backing stores of not yet discovered array buffers and |
| 37 // resets the tracking set, i.e., not yet discovered <- currently live. Note |
| 38 // that this method should be called at most once during a GC cycle as marking |
| 39 // and evacuation properly marks live array buffers. Also, the call can be |
| 40 // omitted, resulting in floating garbage. |
| 41 void FreeDeadAndReset(); |
| 42 |
| 43 // Callback should be of type: |
| 44 // CallbackResult fn(Key); |
| 45 template <typename Callback> |
| 46 void IterateNotYetDiscoveredEntries(Callback callback) { |
| 47 for (TrackingMap::iterator it = not_yet_discovered_.begin(); |
| 48 it != not_yet_discovered_.end();) { |
| 49 if (callback(it->first) == kKeepEntry) { |
| 50 ++it; |
| 51 } else { |
| 52 live_.erase(it++); |
| 53 } |
| 54 } |
| 55 } |
| 56 |
| 57 bool IsTrackedForTesting(Key key); |
| 58 |
| 59 private: |
| 60 typedef std::map<Key, Value> TrackingMap; |
| 61 |
| 62 Heap* heap_; |
| 63 |
| 64 // |live_| maps tracked JSArrayBuffers to the internally allocated backing |
| 65 // store and length. |
| 66 // For each GC round (Scavenger, or incremental/full MC) |
| 67 // |not_yet_discovered_| is initialized as a copy of |live_|. Upon finding |
| 68 // a JSArrayBuffer during GC, the buffer is removed from |
| 69 // |not_yet_discovered_|. At the end of a GC, we free up the remaining |
| 70 // JSArrayBuffers in |not_yet_discovered_|. |
| 71 TrackingMap live_; |
| 72 TrackingMap not_yet_discovered_; |
| 73 }; |
| 74 |
20 class ArrayBufferTracker { | 75 class ArrayBufferTracker { |
21 public: | 76 public: |
22 explicit ArrayBufferTracker(Heap* heap) : heap_(heap) {} | 77 explicit ArrayBufferTracker(Heap* heap) : heap_(heap) {} |
23 ~ArrayBufferTracker(); | 78 ~ArrayBufferTracker(); |
24 | 79 |
25 inline Heap* heap() { return heap_; } | |
26 | |
27 // The following methods are used to track raw C++ pointers to externally | 80 // The following methods are used to track raw C++ pointers to externally |
28 // allocated memory used as backing store in live array buffers. | 81 // allocated memory used as backing store in live array buffers. |
29 | 82 |
30 // A new ArrayBuffer was created with |data| as backing store. | 83 // Register/unregsiter a new JSArrayBuffer |buffer| for tracking. |
31 void RegisterNew(JSArrayBuffer* buffer); | 84 // |track_live| indicates whether marking will still visit the buffer and we |
32 | 85 // can delay marking it as live. |
33 // The backing store |data| is no longer owned by V8. | 86 void RegisterNew(JSArrayBuffer* buffer, bool track_live); |
34 void Unregister(JSArrayBuffer* buffer); | 87 void Unregister(JSArrayBuffer* buffer); |
35 | 88 |
36 // A live ArrayBuffer was discovered during marking/scavenge. | 89 // Frees all backing store pointers that weren't discovered in the previous |
| 90 // scavenge phase. |
| 91 void FreeDeadInNewSpace(); |
| 92 |
| 93 // A live JSArrayBuffer was discovered during marking/scavenge. |
37 void MarkLive(JSArrayBuffer* buffer); | 94 void MarkLive(JSArrayBuffer* buffer); |
38 | 95 |
39 // Frees all backing store pointers that weren't discovered in the previous | 96 // Update methods used to update the tracking state of given ArrayBuffers. |
40 // marking or scavenge phase. | 97 void Promote(JSArrayBuffer* new_buffer, JSArrayBuffer* old_buffer); |
41 void FreeDead(bool from_scavenge); | 98 void SemiSpaceCopy(JSArrayBuffer* new_buffer, JSArrayBuffer* old_buffer); |
42 | 99 void Compact(JSArrayBuffer* new_buffer, JSArrayBuffer* old_buffer); |
43 // Prepare for a new scavenge phase. A new marking phase is implicitly | |
44 // prepared by finishing the previous one. | |
45 void PrepareDiscoveryInNewSpace(); | |
46 | |
47 // An ArrayBuffer moved from new space to old space. | |
48 void Promote(JSArrayBuffer* buffer); | |
49 | 100 |
50 private: | 101 private: |
51 base::Mutex mutex_; | |
52 Heap* heap_; | 102 Heap* heap_; |
| 103 }; |
53 | 104 |
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_; | |
72 }; | |
73 } // namespace internal | 105 } // namespace internal |
74 } // namespace v8 | 106 } // namespace v8 |
75 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 107 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
OLD | NEW |