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 #include "src/heap/array-buffer-tracker.h" | 5 #include "src/heap/array-buffer-tracker.h" |
6 #include "src/heap/heap.h" | 6 #include "src/heap/heap.h" |
7 #include "src/isolate.h" | 7 #include "src/isolate.h" |
8 #include "src/objects.h" | 8 #include "src/objects.h" |
9 #include "src/objects-inl.h" | 9 #include "src/objects-inl.h" |
10 #include "src/v8.h" | 10 #include "src/v8.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 ArrayBufferTracker::~ArrayBufferTracker() { | 15 ArrayBufferTracker::~ArrayBufferTracker() { |
16 Isolate* isolate = heap()->isolate(); | 16 Isolate* isolate = heap()->isolate(); |
17 size_t freed_memory = 0; | 17 size_t freed_memory = 0; |
18 for (auto& buffer : live_old_gen_) { | 18 for (auto& buffer : live_array_buffers_) { |
19 isolate->array_buffer_allocator()->Free(buffer.second.first, | 19 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
20 buffer.second.second); | 20 freed_memory += buffer.second; |
21 freed_memory += buffer.second.second; | |
22 } | 21 } |
23 for (auto& buffer : live_young_gen_) { | 22 for (auto& buffer : live_array_buffers_for_scavenge_) { |
24 isolate->array_buffer_allocator()->Free(buffer.second.first, | 23 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
25 buffer.second.second); | 24 freed_memory += buffer.second; |
26 freed_memory += buffer.second.second; | |
27 } | 25 } |
28 live_old_gen_.clear(); | 26 live_array_buffers_.clear(); |
29 live_young_gen_.clear(); | 27 live_array_buffers_for_scavenge_.clear(); |
30 not_yet_discovered_old_gen_.clear(); | 28 not_yet_discovered_array_buffers_.clear(); |
31 not_yet_discovered_young_gen_.clear(); | 29 not_yet_discovered_array_buffers_for_scavenge_.clear(); |
32 | 30 |
33 if (freed_memory > 0) { | 31 if (freed_memory > 0) { |
34 heap()->update_amount_of_external_allocated_memory( | 32 heap()->update_amount_of_external_allocated_memory( |
35 -static_cast<int64_t>(freed_memory)); | 33 -static_cast<int64_t>(freed_memory)); |
36 } | 34 } |
37 } | 35 } |
38 | 36 |
39 | 37 |
40 void ArrayBufferTracker::RegisterNew(JSArrayBuffer* buffer) { | 38 void ArrayBufferTracker::RegisterNew(JSArrayBuffer* buffer) { |
41 void* data = buffer->backing_store(); | 39 void* data = buffer->backing_store(); |
42 if (!data) return; | 40 if (!data) return; |
43 | 41 |
44 bool in_new_space = heap()->InNewSpace(buffer); | 42 bool in_new_space = heap()->InNewSpace(buffer); |
45 size_t length = NumberToSize(heap()->isolate(), buffer->byte_length()); | 43 size_t length = NumberToSize(heap()->isolate(), buffer->byte_length()); |
46 if (in_new_space) { | 44 if (in_new_space) { |
47 live_young_gen_[buffer->address()] = std::make_pair(data, length); | 45 live_array_buffers_for_scavenge_[data] = length; |
48 not_yet_discovered_young_gen_[buffer->address()] = | |
49 std::make_pair(data, length); | |
50 } else { | 46 } else { |
51 live_old_gen_[buffer->address()] = std::make_pair(data, length); | 47 live_array_buffers_[data] = length; |
52 not_yet_discovered_old_gen_[buffer->address()] = | |
53 std::make_pair(data, length); | |
54 } | 48 } |
55 | 49 |
56 // We may go over the limit of externally allocated memory here. We call the | 50 // We may go over the limit of externally allocated memory here. We call the |
57 // api function to trigger a GC in this case. | 51 // api function to trigger a GC in this case. |
58 reinterpret_cast<v8::Isolate*>(heap()->isolate()) | 52 reinterpret_cast<v8::Isolate*>(heap()->isolate()) |
59 ->AdjustAmountOfExternalAllocatedMemory(length); | 53 ->AdjustAmountOfExternalAllocatedMemory(length); |
60 } | 54 } |
61 | 55 |
62 | 56 |
63 void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) { | 57 void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) { |
64 void* data = buffer->backing_store(); | 58 void* data = buffer->backing_store(); |
65 if (!data) return; | 59 if (!data) return; |
66 | 60 |
67 bool in_new_space = heap()->InNewSpace(buffer); | 61 bool in_new_space = heap()->InNewSpace(buffer); |
68 Key key = buffer->address(); | 62 std::map<void*, size_t>* live_buffers = |
69 TrackingMap* live_buffers = in_new_space ? &live_young_gen_ : &live_old_gen_; | 63 in_new_space ? &live_array_buffers_for_scavenge_ : &live_array_buffers_; |
70 TrackingMap* not_yet_discovered_buffers = in_new_space | 64 std::map<void*, size_t>* not_yet_discovered_buffers = |
71 ? ¬_yet_discovered_young_gen_ | 65 in_new_space ? ¬_yet_discovered_array_buffers_for_scavenge_ |
72 : ¬_yet_discovered_old_gen_; | 66 : ¬_yet_discovered_array_buffers_; |
73 | 67 |
74 DCHECK(live_buffers->count(key) > 0); | 68 DCHECK(live_buffers->count(data) > 0); |
75 | 69 |
76 size_t length = (*live_buffers)[key].second; | 70 size_t length = (*live_buffers)[data]; |
77 live_buffers->erase(key); | 71 live_buffers->erase(data); |
78 not_yet_discovered_buffers->erase(key); | 72 not_yet_discovered_buffers->erase(data); |
79 | 73 |
80 heap()->update_amount_of_external_allocated_memory( | 74 heap()->update_amount_of_external_allocated_memory( |
81 -static_cast<int64_t>(length)); | 75 -static_cast<int64_t>(length)); |
82 } | 76 } |
83 | 77 |
| 78 |
| 79 void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) { |
| 80 base::LockGuard<base::Mutex> guard(&mutex_); |
| 81 void* data = buffer->backing_store(); |
| 82 |
| 83 // ArrayBuffer might be in the middle of being constructed. |
| 84 if (data == heap()->undefined_value()) return; |
| 85 if (heap()->InNewSpace(buffer)) { |
| 86 not_yet_discovered_array_buffers_for_scavenge_.erase(data); |
| 87 } else { |
| 88 not_yet_discovered_array_buffers_.erase(data); |
| 89 } |
| 90 } |
| 91 |
| 92 |
84 void ArrayBufferTracker::FreeDead(bool from_scavenge) { | 93 void ArrayBufferTracker::FreeDead(bool from_scavenge) { |
85 size_t freed_memory = 0; | 94 size_t freed_memory = 0; |
86 Isolate* isolate = heap()->isolate(); | 95 Isolate* isolate = heap()->isolate(); |
87 for (auto& buffer : not_yet_discovered_young_gen_) { | 96 for (auto& buffer : not_yet_discovered_array_buffers_for_scavenge_) { |
88 isolate->array_buffer_allocator()->Free(buffer.second.first, | 97 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
89 buffer.second.second); | 98 freed_memory += buffer.second; |
90 freed_memory += buffer.second.second; | 99 live_array_buffers_for_scavenge_.erase(buffer.first); |
91 live_young_gen_.erase(buffer.first); | |
92 } | 100 } |
93 | 101 |
94 if (!from_scavenge) { | 102 if (!from_scavenge) { |
95 for (auto& buffer : not_yet_discovered_old_gen_) { | 103 for (auto& buffer : not_yet_discovered_array_buffers_) { |
96 isolate->array_buffer_allocator()->Free(buffer.second.first, | 104 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
97 buffer.second.second); | 105 freed_memory += buffer.second; |
98 freed_memory += buffer.second.second; | 106 live_array_buffers_.erase(buffer.first); |
99 live_old_gen_.erase(buffer.first); | |
100 } | 107 } |
101 } | 108 } |
102 | 109 |
103 not_yet_discovered_young_gen_ = live_young_gen_; | 110 not_yet_discovered_array_buffers_for_scavenge_ = |
104 if (!from_scavenge) not_yet_discovered_old_gen_ = live_old_gen_; | 111 live_array_buffers_for_scavenge_; |
| 112 if (!from_scavenge) not_yet_discovered_array_buffers_ = live_array_buffers_; |
105 | 113 |
106 // Do not call through the api as this code is triggered while doing a GC. | 114 // Do not call through the api as this code is triggered while doing a GC. |
107 heap()->update_amount_of_external_allocated_memory( | 115 heap()->update_amount_of_external_allocated_memory( |
108 -static_cast<int64_t>(freed_memory)); | 116 -static_cast<int64_t>(freed_memory)); |
109 } | 117 } |
110 | 118 |
111 #define UPDATE_GUARD(buffer, data) \ | 119 |
112 if (buffer->is_external()) return; \ | 120 void ArrayBufferTracker::PrepareDiscoveryInNewSpace() { |
113 data = buffer->backing_store(); \ | 121 not_yet_discovered_array_buffers_for_scavenge_ = |
114 if (data == nullptr) return; \ | 122 live_array_buffers_for_scavenge_; |
115 if (data == heap()->undefined_value()) return; \ | 123 } |
| 124 |
| 125 |
| 126 void ArrayBufferTracker::Promote(JSArrayBuffer* buffer) { |
116 base::LockGuard<base::Mutex> guard(&mutex_); | 127 base::LockGuard<base::Mutex> guard(&mutex_); |
117 | 128 |
118 void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) { | 129 if (buffer->is_external()) return; |
119 void* data = nullptr; | 130 void* data = buffer->backing_store(); |
120 UPDATE_GUARD(buffer, data); | 131 if (!data) return; |
121 | 132 // ArrayBuffer might be in the middle of being constructed. |
122 if (heap()->InNewSpace(buffer)) { | 133 if (data == heap()->undefined_value()) return; |
123 not_yet_discovered_young_gen_.erase(buffer->address()); | 134 DCHECK(live_array_buffers_for_scavenge_.count(data) > 0); |
124 } else { | 135 live_array_buffers_[data] = live_array_buffers_for_scavenge_[data]; |
125 not_yet_discovered_old_gen_.erase(buffer->address()); | 136 live_array_buffers_for_scavenge_.erase(data); |
126 } | 137 not_yet_discovered_array_buffers_for_scavenge_.erase(data); |
127 } | |
128 | |
129 void ArrayBufferTracker::Promote(JSArrayBuffer* new_buffer, | |
130 JSArrayBuffer* old_buffer) { | |
131 void* data = nullptr; | |
132 UPDATE_GUARD(new_buffer, data); | |
133 | |
134 Key new_key = new_buffer->address(); | |
135 Key old_key = old_buffer->address(); | |
136 DCHECK(live_young_gen_.count(old_key) > 0); | |
137 live_old_gen_[new_key] = live_young_gen_[old_key]; | |
138 live_young_gen_.erase(old_key); | |
139 not_yet_discovered_young_gen_.erase(old_key); | |
140 } | |
141 | |
142 void ArrayBufferTracker::Compact(JSArrayBuffer* new_buffer, | |
143 JSArrayBuffer* old_buffer) { | |
144 void* data = nullptr; | |
145 UPDATE_GUARD(new_buffer, data); | |
146 | |
147 Key new_key = new_buffer->address(); | |
148 Key old_key = old_buffer->address(); | |
149 DCHECK_NE(new_key, old_key); | |
150 DCHECK(live_old_gen_.count(old_key) > 0); | |
151 live_old_gen_[new_key] = live_old_gen_[old_key]; | |
152 live_old_gen_.erase(old_key); | |
153 not_yet_discovered_old_gen_.erase(old_key); | |
154 } | |
155 | |
156 void ArrayBufferTracker::SemiSpaceCopy(JSArrayBuffer* new_buffer, | |
157 JSArrayBuffer* old_buffer) { | |
158 void* data = nullptr; | |
159 UPDATE_GUARD(new_buffer, data); | |
160 | |
161 Key new_key = new_buffer->address(); | |
162 Key old_key = old_buffer->address(); | |
163 DCHECK(live_young_gen_.count(old_key) > 0); | |
164 live_young_gen_[new_key] = live_young_gen_[old_key]; | |
165 live_young_gen_.erase(old_key); | |
166 not_yet_discovered_young_gen_.erase(old_key); | |
167 } | |
168 | |
169 #undef UPDATE_GUARD | |
170 | |
171 bool ArrayBufferTracker::IsTrackedInOldGenForTesting(JSArrayBuffer* buffer) { | |
172 return live_old_gen_.find(buffer->address()) != live_old_gen_.end(); | |
173 } | |
174 | |
175 bool ArrayBufferTracker::IsTrackedInYoungGenForTesting(JSArrayBuffer* buffer) { | |
176 return live_young_gen_.find(buffer->address()) != live_young_gen_.end(); | |
177 } | 138 } |
178 | 139 |
179 } // namespace internal | 140 } // namespace internal |
180 } // namespace v8 | 141 } // namespace v8 |
OLD | NEW |