OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 CHECK_EQ(gc_ends, gc_starts); | 291 CHECK_EQ(gc_ends, gc_starts); |
292 | 292 |
293 Heap::CollectGarbage(OLD_POINTER_SPACE); | 293 Heap::CollectGarbage(OLD_POINTER_SPACE); |
294 CHECK_EQ(1, gc_starts); | 294 CHECK_EQ(1, gc_starts); |
295 CHECK_EQ(gc_ends, gc_starts); | 295 CHECK_EQ(gc_ends, gc_starts); |
296 } | 296 } |
297 | 297 |
298 | 298 |
299 static int NumberOfWeakCalls = 0; | 299 static int NumberOfWeakCalls = 0; |
300 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { | 300 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { |
| 301 ASSERT(id == reinterpret_cast<void*>(1234)); |
301 NumberOfWeakCalls++; | 302 NumberOfWeakCalls++; |
302 handle.Dispose(); | 303 handle.Dispose(); |
303 } | 304 } |
304 | 305 |
305 TEST(ObjectGroups) { | 306 TEST(ObjectGroups) { |
306 InitializeVM(); | 307 InitializeVM(); |
307 | 308 |
308 NumberOfWeakCalls = 0; | 309 NumberOfWeakCalls = 0; |
309 v8::HandleScope handle_scope; | 310 v8::HandleScope handle_scope; |
310 | 311 |
311 Handle<Object> g1s1 = | 312 Handle<Object> g1s1 = |
312 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); | 313 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
313 Handle<Object> g1s2 = | 314 Handle<Object> g1s2 = |
314 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); | 315 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
| 316 Handle<Object> g1c1 = |
| 317 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
315 GlobalHandles::MakeWeak(g1s1.location(), | 318 GlobalHandles::MakeWeak(g1s1.location(), |
316 reinterpret_cast<void*>(1234), | 319 reinterpret_cast<void*>(1234), |
317 &WeakPointerCallback); | 320 &WeakPointerCallback); |
318 GlobalHandles::MakeWeak(g1s2.location(), | 321 GlobalHandles::MakeWeak(g1s2.location(), |
319 reinterpret_cast<void*>(1234), | 322 reinterpret_cast<void*>(1234), |
320 &WeakPointerCallback); | 323 &WeakPointerCallback); |
| 324 GlobalHandles::MakeWeak(g1c1.location(), |
| 325 reinterpret_cast<void*>(1234), |
| 326 &WeakPointerCallback); |
321 | 327 |
322 Handle<Object> g2s1 = | 328 Handle<Object> g2s1 = |
323 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); | 329 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
324 Handle<Object> g2s2 = | 330 Handle<Object> g2s2 = |
325 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); | 331 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
| 332 Handle<Object> g2c1 = |
| 333 GlobalHandles::Create(Heap::AllocateFixedArray(1)->ToObjectChecked()); |
326 GlobalHandles::MakeWeak(g2s1.location(), | 334 GlobalHandles::MakeWeak(g2s1.location(), |
327 reinterpret_cast<void*>(1234), | 335 reinterpret_cast<void*>(1234), |
328 &WeakPointerCallback); | 336 &WeakPointerCallback); |
329 GlobalHandles::MakeWeak(g2s2.location(), | 337 GlobalHandles::MakeWeak(g2s2.location(), |
330 reinterpret_cast<void*>(1234), | 338 reinterpret_cast<void*>(1234), |
331 &WeakPointerCallback); | 339 &WeakPointerCallback); |
| 340 GlobalHandles::MakeWeak(g2c1.location(), |
| 341 reinterpret_cast<void*>(1234), |
| 342 &WeakPointerCallback); |
332 | 343 |
333 Handle<Object> root = GlobalHandles::Create(*g1s1); // make a root. | 344 Handle<Object> root = GlobalHandles::Create(*g1s1); // make a root. |
334 | 345 |
335 // Connect group 1 and 2, make a cycle. | 346 // Connect group 1 and 2, make a cycle. |
336 Handle<FixedArray>::cast(g1s2)->set(0, *g2s2); | 347 Handle<FixedArray>::cast(g1s2)->set(0, *g2s2); |
337 Handle<FixedArray>::cast(g2s1)->set(0, *g1s1); | 348 Handle<FixedArray>::cast(g2s1)->set(0, *g1s1); |
338 | 349 |
339 { | 350 { |
340 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; | 351 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
| 352 Object** g1_children[] = { g1c1.location() }; |
341 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; | 353 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
342 GlobalHandles::AddGroup(g1_objects, 2, NULL); | 354 Object** g2_children[] = { g2c1.location() }; |
343 GlobalHandles::AddGroup(g2_objects, 2, NULL); | 355 GlobalHandles::AddObjectGroup(g1_objects, 2, NULL); |
| 356 GlobalHandles::AddImplicitRefGroup(HeapObject::cast(*g1s1), g1_children, 1); |
| 357 GlobalHandles::AddObjectGroup(g2_objects, 2, NULL); |
| 358 GlobalHandles::AddImplicitRefGroup(HeapObject::cast(*g2s2), g2_children, 1); |
344 } | 359 } |
345 // Do a full GC | 360 // Do a full GC |
346 Heap::CollectGarbage(OLD_POINTER_SPACE); | 361 Heap::CollectGarbage(OLD_POINTER_SPACE); |
347 | 362 |
348 // All object should be alive. | 363 // All object should be alive. |
349 CHECK_EQ(0, NumberOfWeakCalls); | 364 CHECK_EQ(0, NumberOfWeakCalls); |
350 | 365 |
351 // Weaken the root. | 366 // Weaken the root. |
352 GlobalHandles::MakeWeak(root.location(), | 367 GlobalHandles::MakeWeak(root.location(), |
353 reinterpret_cast<void*>(1234), | 368 reinterpret_cast<void*>(1234), |
354 &WeakPointerCallback); | 369 &WeakPointerCallback); |
| 370 // But make children strong roots---all the objects (except for children) |
| 371 // should be collectable now. |
| 372 GlobalHandles::ClearWeakness(g1c1.location()); |
| 373 GlobalHandles::ClearWeakness(g2c1.location()); |
355 | 374 |
356 // Groups are deleted, rebuild groups. | 375 // Groups are deleted, rebuild groups. |
357 { | 376 { |
358 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; | 377 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
| 378 Object** g1_children[] = { g1c1.location() }; |
359 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; | 379 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
360 GlobalHandles::AddGroup(g1_objects, 2, NULL); | 380 Object** g2_children[] = { g2c1.location() }; |
361 GlobalHandles::AddGroup(g2_objects, 2, NULL); | 381 GlobalHandles::AddObjectGroup(g1_objects, 2, NULL); |
| 382 GlobalHandles::AddImplicitRefGroup(HeapObject::cast(*g1s1), g1_children, 1); |
| 383 GlobalHandles::AddObjectGroup(g2_objects, 2, NULL); |
| 384 GlobalHandles::AddImplicitRefGroup(HeapObject::cast(*g2s2), g2_children, 1); |
362 } | 385 } |
363 | 386 |
364 Heap::CollectGarbage(OLD_POINTER_SPACE); | 387 Heap::CollectGarbage(OLD_POINTER_SPACE); |
365 | 388 |
366 // All objects should be gone. 5 global handles in total. | 389 // All objects should be gone. 5 global handles in total. |
367 CHECK_EQ(5, NumberOfWeakCalls); | 390 CHECK_EQ(5, NumberOfWeakCalls); |
| 391 |
| 392 // And now make children weak again and collect them. |
| 393 GlobalHandles::MakeWeak(g1c1.location(), |
| 394 reinterpret_cast<void*>(1234), |
| 395 &WeakPointerCallback); |
| 396 GlobalHandles::MakeWeak(g2c1.location(), |
| 397 reinterpret_cast<void*>(1234), |
| 398 &WeakPointerCallback); |
| 399 |
| 400 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 401 CHECK_EQ(7, NumberOfWeakCalls); |
368 } | 402 } |
OLD | NEW |