Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Side by Side Diff: test/cctest/test-global-handles.cc

Issue 14007008: New GC APIs, try 2. (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: code review (yurys) Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/cctest/test-api.cc ('k') | test/cctest/test-heap-profiler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 12 matching lines...) Expand all
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "global-handles.h" 28 #include "global-handles.h"
29 29
30 #include "cctest.h" 30 #include "cctest.h"
31 31
32 using namespace v8::internal; 32 using namespace v8::internal;
33 using v8::UniqueId;
33 34
34 static int NumberOfWeakCalls = 0;
35 static void WeakPointerCallback(v8::Isolate* isolate,
36 v8::Persistent<v8::Value> handle,
37 void* id) {
38 ASSERT(id == reinterpret_cast<void*>(1234));
39 NumberOfWeakCalls++;
40 handle.Dispose(isolate);
41 }
42 35
43 static List<Object*> skippable_objects; 36 static List<Object*> skippable_objects;
44 static List<Object*> can_skip_called_objects; 37 static List<Object*> can_skip_called_objects;
45 38
39
46 static bool CanSkipCallback(Heap* heap, Object** pointer) { 40 static bool CanSkipCallback(Heap* heap, Object** pointer) {
47 can_skip_called_objects.Add(*pointer); 41 can_skip_called_objects.Add(*pointer);
48 return skippable_objects.Contains(*pointer); 42 return skippable_objects.Contains(*pointer);
49 } 43 }
50 44
45
51 static void ResetCanSkipData() { 46 static void ResetCanSkipData() {
52 skippable_objects.Clear(); 47 skippable_objects.Clear();
53 can_skip_called_objects.Clear(); 48 can_skip_called_objects.Clear();
54 } 49 }
55 50
51
56 class TestRetainedObjectInfo : public v8::RetainedObjectInfo { 52 class TestRetainedObjectInfo : public v8::RetainedObjectInfo {
57 public: 53 public:
58 TestRetainedObjectInfo() : has_been_disposed_(false) {} 54 TestRetainedObjectInfo() : has_been_disposed_(false) {}
59 55
60 bool has_been_disposed() { return has_been_disposed_; } 56 bool has_been_disposed() { return has_been_disposed_; }
61 57
62 virtual void Dispose() { 58 virtual void Dispose() {
63 ASSERT(!has_been_disposed_); 59 ASSERT(!has_been_disposed_);
64 has_been_disposed_ = true; 60 has_been_disposed_ = true;
65 } 61 }
66 62
67 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) { 63 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) {
68 return other == this; 64 return other == this;
69 } 65 }
70 66
71 virtual intptr_t GetHash() { return 0; } 67 virtual intptr_t GetHash() { return 0; }
72 68
73 virtual const char* GetLabel() { return "whatever"; } 69 virtual const char* GetLabel() { return "whatever"; }
74 70
75 private: 71 private:
76 bool has_been_disposed_; 72 bool has_been_disposed_;
77 }; 73 };
78 74
75
79 class TestObjectVisitor : public ObjectVisitor { 76 class TestObjectVisitor : public ObjectVisitor {
80 public: 77 public:
81 virtual void VisitPointers(Object** start, Object** end) { 78 virtual void VisitPointers(Object** start, Object** end) {
82 for (Object** o = start; o != end; ++o) 79 for (Object** o = start; o != end; ++o)
83 visited.Add(*o); 80 visited.Add(*o);
84 } 81 }
85 82
86 List<Object*> visited; 83 List<Object*> visited;
87 }; 84 };
88 85
86
89 TEST(IterateObjectGroupsOldApi) { 87 TEST(IterateObjectGroupsOldApi) {
90 CcTest::InitializeVM(); 88 CcTest::InitializeVM();
91 GlobalHandles* global_handles = Isolate::Current()->global_handles(); 89 GlobalHandles* global_handles = Isolate::Current()->global_handles();
92 90
93 v8::HandleScope handle_scope(CcTest::isolate()); 91 v8::HandleScope handle_scope(CcTest::isolate());
94 92
95 Handle<Object> g1s1 = 93 Handle<Object> g1s1 =
96 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 94 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
97 Handle<Object> g1s2 = 95 Handle<Object> g1s2 =
98 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 96 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
99 global_handles->MakeWeak(g1s1.location(),
100 reinterpret_cast<void*>(1234),
101 NULL,
102 &WeakPointerCallback);
103 global_handles->MakeWeak(g1s2.location(),
104 reinterpret_cast<void*>(1234),
105 NULL,
106 &WeakPointerCallback);
107 97
108 Handle<Object> g2s1 = 98 Handle<Object> g2s1 =
109 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 99 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
110 Handle<Object> g2s2 = 100 Handle<Object> g2s2 =
111 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 101 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
112 global_handles->MakeWeak(g2s1.location(),
113 reinterpret_cast<void*>(1234),
114 NULL,
115 &WeakPointerCallback);
116 global_handles->MakeWeak(g2s2.location(),
117 reinterpret_cast<void*>(1234),
118 NULL,
119 &WeakPointerCallback);
120 102
121 TestRetainedObjectInfo info1; 103 TestRetainedObjectInfo info1;
122 TestRetainedObjectInfo info2; 104 TestRetainedObjectInfo info2;
123 { 105 {
124 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; 106 Object** g1_objects[] = { g1s1.location(), g1s2.location() };
125 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; 107 Object** g2_objects[] = { g2s1.location(), g2s2.location() };
126 108
127 global_handles->AddObjectGroup(g1_objects, 2, &info1); 109 global_handles->AddObjectGroup(g1_objects, 2, &info1);
128 global_handles->AddObjectGroup(g2_objects, 2, &info2); 110 global_handles->AddObjectGroup(g2_objects, 2, &info2);
129 } 111 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 ASSERT(!info2.has_been_disposed()); 159 ASSERT(!info2.has_been_disposed());
178 } 160 }
179 161
180 // Iterate again, don't skip anything. 162 // Iterate again, don't skip anything.
181 { 163 {
182 ResetCanSkipData(); 164 ResetCanSkipData();
183 TestObjectVisitor visitor; 165 TestObjectVisitor visitor;
184 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback); 166 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback);
185 167
186 // CanSkipCallback was called for all objects. 168 // CanSkipCallback was called for all objects.
187 fprintf(stderr, "can skip len %d\n", can_skip_called_objects.length());
188 ASSERT(can_skip_called_objects.length() == 1); 169 ASSERT(can_skip_called_objects.length() == 1);
189 ASSERT(can_skip_called_objects.Contains(*g2s1.location()) || 170 ASSERT(can_skip_called_objects.Contains(*g2s1.location()) ||
190 can_skip_called_objects.Contains(*g2s2.location())); 171 can_skip_called_objects.Contains(*g2s2.location()));
191 172
192 // The second group was visited. 173 // The second group was visited.
193 ASSERT(visitor.visited.length() == 2); 174 ASSERT(visitor.visited.length() == 2);
194 ASSERT(visitor.visited.Contains(*g2s1.location())); 175 ASSERT(visitor.visited.Contains(*g2s1.location()));
195 ASSERT(visitor.visited.Contains(*g2s2.location())); 176 ASSERT(visitor.visited.Contains(*g2s2.location()));
196 ASSERT(info2.has_been_disposed()); 177 ASSERT(info2.has_been_disposed());
197 } 178 }
198 } 179 }
180
181
182 TEST(IterateObjectGroups) {
183 CcTest::InitializeVM();
184 GlobalHandles* global_handles = Isolate::Current()->global_handles();
185
186 v8::HandleScope handle_scope(CcTest::isolate());
187
188 Handle<Object> g1s1 =
189 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
190 Handle<Object> g1s2 =
191 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
192
193 Handle<Object> g2s1 =
194 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
195 Handle<Object> g2s2 =
196 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
197
198 TestRetainedObjectInfo info1;
199 TestRetainedObjectInfo info2;
200 global_handles->SetObjectGroupId(g2s1.location(), UniqueId(2));
201 global_handles->SetObjectGroupId(g2s2.location(), UniqueId(2));
202 global_handles->SetRetainedObjectInfo(UniqueId(2), &info2);
203 global_handles->SetObjectGroupId(g1s1.location(), UniqueId(1));
204 global_handles->SetObjectGroupId(g1s2.location(), UniqueId(1));
205 global_handles->SetRetainedObjectInfo(UniqueId(1), &info1);
206
207 // Iterate the object groups. First skip all.
208 {
209 ResetCanSkipData();
210 skippable_objects.Add(*g1s1.location());
211 skippable_objects.Add(*g1s2.location());
212 skippable_objects.Add(*g2s1.location());
213 skippable_objects.Add(*g2s2.location());
214 TestObjectVisitor visitor;
215 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback);
216
217 // CanSkipCallback was called for all objects.
218 ASSERT(can_skip_called_objects.length() == 4);
219 ASSERT(can_skip_called_objects.Contains(*g1s1.location()));
220 ASSERT(can_skip_called_objects.Contains(*g1s2.location()));
221 ASSERT(can_skip_called_objects.Contains(*g2s1.location()));
222 ASSERT(can_skip_called_objects.Contains(*g2s2.location()));
223
224 // Nothing was visited.
225 ASSERT(visitor.visited.length() == 0);
226 ASSERT(!info1.has_been_disposed());
227 ASSERT(!info2.has_been_disposed());
228 }
229
230 // Iterate again, now only skip the second object group.
231 {
232 ResetCanSkipData();
233 // The first grough should still be visited, since only one object is
234 // skipped.
235 skippable_objects.Add(*g1s1.location());
236 skippable_objects.Add(*g2s1.location());
237 skippable_objects.Add(*g2s2.location());
238 TestObjectVisitor visitor;
239 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback);
240
241 // CanSkipCallback was called for all objects.
242 ASSERT(can_skip_called_objects.length() == 3 ||
243 can_skip_called_objects.length() == 4);
244 ASSERT(can_skip_called_objects.Contains(*g1s2.location()));
245 ASSERT(can_skip_called_objects.Contains(*g2s1.location()));
246 ASSERT(can_skip_called_objects.Contains(*g2s2.location()));
247
248 // The first group was visited.
249 ASSERT(visitor.visited.length() == 2);
250 ASSERT(visitor.visited.Contains(*g1s1.location()));
251 ASSERT(visitor.visited.Contains(*g1s2.location()));
252 ASSERT(info1.has_been_disposed());
253 ASSERT(!info2.has_been_disposed());
254 }
255
256 // Iterate again, don't skip anything.
257 {
258 ResetCanSkipData();
259 TestObjectVisitor visitor;
260 global_handles->IterateObjectGroups(&visitor, &CanSkipCallback);
261
262 // CanSkipCallback was called for all objects.
263 ASSERT(can_skip_called_objects.length() == 1);
264 ASSERT(can_skip_called_objects.Contains(*g2s1.location()) ||
265 can_skip_called_objects.Contains(*g2s2.location()));
266
267 // The second group was visited.
268 ASSERT(visitor.visited.length() == 2);
269 ASSERT(visitor.visited.Contains(*g2s1.location()));
270 ASSERT(visitor.visited.Contains(*g2s2.location()));
271 ASSERT(info2.has_been_disposed());
272 }
273 }
274
275
276 TEST(ImplicitReferences) {
277 CcTest::InitializeVM();
278 GlobalHandles* global_handles = Isolate::Current()->global_handles();
279
280 v8::HandleScope handle_scope(CcTest::isolate());
281
282 Handle<Object> g1s1 =
283 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
284 Handle<Object> g1c1 =
285 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
286 Handle<Object> g1c2 =
287 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
288
289
290 Handle<Object> g2s1 =
291 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
292 Handle<Object> g2s2 =
293 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
294 Handle<Object> g2c1 =
295 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
296
297 global_handles->SetObjectGroupId(g1s1.location(), UniqueId(1));
298 global_handles->SetObjectGroupId(g2s1.location(), UniqueId(2));
299 global_handles->SetObjectGroupId(g2s2.location(), UniqueId(2));
300 global_handles->SetReferenceFromGroup(UniqueId(1), g1c1.location());
301 global_handles->SetReferenceFromGroup(UniqueId(1), g1c2.location());
302 global_handles->SetReferenceFromGroup(UniqueId(2), g2c1.location());
303
304 List<ImplicitRefGroup*>* implicit_refs =
305 global_handles->implicit_ref_groups();
306 USE(implicit_refs);
307 ASSERT(implicit_refs->length() == 2);
308 ASSERT(implicit_refs->at(0)->parent ==
309 reinterpret_cast<HeapObject**>(g1s1.location()));
310 ASSERT(implicit_refs->at(0)->length == 2);
311 ASSERT(implicit_refs->at(0)->children[0] == g1c1.location());
312 ASSERT(implicit_refs->at(0)->children[1] == g1c2.location());
313 ASSERT(implicit_refs->at(1)->parent ==
314 reinterpret_cast<HeapObject**>(g2s1.location()));
315 ASSERT(implicit_refs->at(1)->length == 1);
316 ASSERT(implicit_refs->at(1)->children[0] == g2c1.location());
317 }
OLDNEW
« no previous file with comments | « test/cctest/test-api.cc ('k') | test/cctest/test-heap-profiler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698