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 ArrayBufferTracker { | 20 class ArrayBufferTracker { |
21 public: | 21 public: |
| 22 typedef void* Key; |
| 23 |
| 24 enum CallbackResult { kKeepEntry, kRemoveEntry }; |
| 25 enum ListType { kNewSpace, kOldSpace }; |
| 26 |
22 explicit ArrayBufferTracker(Heap* heap) : heap_(heap) {} | 27 explicit ArrayBufferTracker(Heap* heap) : heap_(heap) {} |
23 ~ArrayBufferTracker(); | 28 ~ArrayBufferTracker(); |
24 | 29 |
25 inline Heap* heap() { return heap_; } | |
26 | |
27 // The following methods are used to track raw C++ pointers to externally | 30 // The following methods are used to track raw C++ pointers to externally |
28 // allocated memory used as backing store in live array buffers. | 31 // allocated memory used as backing store in live array buffers. |
29 | 32 |
30 // A new ArrayBuffer was created with |data| as backing store. | 33 // A new ArrayBuffer was created with |data| as backing store. |
31 void RegisterNew(JSArrayBuffer* buffer); | 34 void RegisterNew(JSArrayBuffer* buffer); |
32 | 35 |
33 // The backing store |data| is no longer owned by V8. | 36 // The backing store |data| is no longer owned by V8. |
34 void Unregister(JSArrayBuffer* buffer); | 37 void Unregister(JSArrayBuffer* buffer); |
35 | 38 |
36 // A live ArrayBuffer was discovered during marking/scavenge. | 39 // A live ArrayBuffer was discovered during marking/scavenge. |
37 void MarkLive(JSArrayBuffer* buffer); | 40 void MarkLive(JSArrayBuffer* buffer); |
38 | 41 |
39 // Frees all backing store pointers that weren't discovered in the previous | 42 // Frees all backing store pointers that weren't discovered in the previous |
40 // marking or scavenge phase. | 43 // marking or scavenge phase. |
41 void FreeDead(bool from_scavenge); | 44 void FreeDead(bool from_scavenge); |
42 | 45 |
43 // Prepare for a new scavenge phase. A new marking phase is implicitly | 46 // Update methods used to update the tracking state of given ArrayBuffers. |
44 // prepared by finishing the previous one. | 47 void Promote(JSArrayBuffer* new_buffer, JSArrayBuffer* old_buffer); |
45 void PrepareDiscoveryInNewSpace(); | 48 void SemiSpaceCopy(JSArrayBuffer* new_buffer, JSArrayBuffer* old_buffer); |
| 49 void Compact(JSArrayBuffer* new_buffer, JSArrayBuffer* old_buffer); |
46 | 50 |
47 // An ArrayBuffer moved from new space to old space. | 51 // Callback should be of type: |
48 void Promote(JSArrayBuffer* buffer); | 52 // CallbackResult fn(Key); |
| 53 template <typename Callback> |
| 54 void IterateNotYetDiscoveredEntries(ListType list, Key from, Key to, |
| 55 Callback callback) { |
| 56 TrackingMap::iterator it = |
| 57 list == kNewSpace ? not_yet_discovered_young_gen_.lower_bound(from) |
| 58 : not_yet_discovered_old_gen_.lower_bound(from); |
| 59 const TrackingMap::iterator end = |
| 60 list == kNewSpace ? not_yet_discovered_young_gen_.upper_bound(to) |
| 61 : not_yet_discovered_old_gen_.upper_bound(to); |
| 62 { |
| 63 base::LockGuard<base::Mutex> guard(&mutex_); |
| 64 while (it != end) { |
| 65 if (callback(it->first) == kKeepEntry) { |
| 66 ++it; |
| 67 } else { |
| 68 live_old_gen_.erase(it++); |
| 69 } |
| 70 } |
| 71 } |
| 72 } |
| 73 |
| 74 bool IsTrackedInOldGenForTesting(JSArrayBuffer* buffer); |
| 75 bool IsTrackedInYoungGenForTesting(JSArrayBuffer* buffer); |
49 | 76 |
50 private: | 77 private: |
| 78 typedef std::map<Key, std::pair<void*, size_t>> TrackingMap; |
| 79 |
| 80 inline Heap* heap() { return heap_; } |
| 81 |
51 base::Mutex mutex_; | 82 base::Mutex mutex_; |
52 Heap* heap_; | 83 Heap* heap_; |
53 | 84 |
54 // |live_array_buffers_| maps externally allocated memory used as backing | 85 // |live_*| maps tracked JSArrayBuffers to the internally allocated backing |
55 // store for ArrayBuffers to the length of the respective memory blocks. | 86 // store and length. |
56 // | 87 // For each GC round (Scavenger, or incremental/full MC) |
57 // At the beginning of mark/compact, |not_yet_discovered_array_buffers_| is | 88 // |not_yet_discovered_*| is initialized as a copy of |live_*|. Upon finding |
58 // a copy of |live_array_buffers_| and we remove pointers as we discover live | 89 // a JSArrayBuffer during GC, the buffer is removed from |
59 // ArrayBuffer objects during marking. At the end of mark/compact, the | 90 // |not_yet_discovered_*|. At the end of a GC, we free up the remaining |
60 // remaining memory blocks can be freed. | 91 // JSArrayBuffers in |not_yet_discovered_*|. |
61 std::map<void*, size_t> live_array_buffers_; | 92 TrackingMap live_old_gen_; |
62 std::map<void*, size_t> not_yet_discovered_array_buffers_; | 93 TrackingMap not_yet_discovered_old_gen_; |
| 94 TrackingMap live_young_gen_; |
| 95 TrackingMap not_yet_discovered_young_gen_; |
| 96 }; |
63 | 97 |
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 | 98 } // namespace internal |
74 } // namespace v8 | 99 } // namespace v8 |
75 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ | 100 #endif // V8_HEAP_ARRAY_BUFFER_TRACKER_H_ |
OLD | NEW |