| 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::AddImplicitReferences(HeapObject::cast(*g1s1), |
| 357 g1_children, 1); |
| 358 GlobalHandles::AddObjectGroup(g2_objects, 2, NULL); |
| 359 GlobalHandles::AddImplicitReferences(HeapObject::cast(*g2s2), |
| 360 g2_children, 1); |
| 344 } | 361 } |
| 345 // Do a full GC | 362 // Do a full GC |
| 346 Heap::CollectGarbage(OLD_POINTER_SPACE); | 363 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 347 | 364 |
| 348 // All object should be alive. | 365 // All object should be alive. |
| 349 CHECK_EQ(0, NumberOfWeakCalls); | 366 CHECK_EQ(0, NumberOfWeakCalls); |
| 350 | 367 |
| 351 // Weaken the root. | 368 // Weaken the root. |
| 352 GlobalHandles::MakeWeak(root.location(), | 369 GlobalHandles::MakeWeak(root.location(), |
| 353 reinterpret_cast<void*>(1234), | 370 reinterpret_cast<void*>(1234), |
| 354 &WeakPointerCallback); | 371 &WeakPointerCallback); |
| 372 // But make children strong roots---all the objects (except for children) |
| 373 // should be collectable now. |
| 374 GlobalHandles::ClearWeakness(g1c1.location()); |
| 375 GlobalHandles::ClearWeakness(g2c1.location()); |
| 355 | 376 |
| 356 // Groups are deleted, rebuild groups. | 377 // Groups are deleted, rebuild groups. |
| 357 { | 378 { |
| 358 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; | 379 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; |
| 380 Object** g1_children[] = { g1c1.location() }; |
| 359 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; | 381 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; |
| 360 GlobalHandles::AddGroup(g1_objects, 2, NULL); | 382 Object** g2_children[] = { g2c1.location() }; |
| 361 GlobalHandles::AddGroup(g2_objects, 2, NULL); | 383 GlobalHandles::AddObjectGroup(g1_objects, 2, NULL); |
| 384 GlobalHandles::AddImplicitReferences(HeapObject::cast(*g1s1), |
| 385 g1_children, 1); |
| 386 GlobalHandles::AddObjectGroup(g2_objects, 2, NULL); |
| 387 GlobalHandles::AddImplicitReferences(HeapObject::cast(*g2s2), |
| 388 g2_children, 1); |
| 362 } | 389 } |
| 363 | 390 |
| 364 Heap::CollectGarbage(OLD_POINTER_SPACE); | 391 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 365 | 392 |
| 366 // All objects should be gone. 5 global handles in total. | 393 // All objects should be gone. 5 global handles in total. |
| 367 CHECK_EQ(5, NumberOfWeakCalls); | 394 CHECK_EQ(5, NumberOfWeakCalls); |
| 395 |
| 396 // And now make children weak again and collect them. |
| 397 GlobalHandles::MakeWeak(g1c1.location(), |
| 398 reinterpret_cast<void*>(1234), |
| 399 &WeakPointerCallback); |
| 400 GlobalHandles::MakeWeak(g2c1.location(), |
| 401 reinterpret_cast<void*>(1234), |
| 402 &WeakPointerCallback); |
| 403 |
| 404 Heap::CollectGarbage(OLD_POINTER_SPACE); |
| 405 CHECK_EQ(7, NumberOfWeakCalls); |
| 368 } | 406 } |
| OLD | NEW |