OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 25 matching lines...) Expand all Loading... |
36 #endif | 36 #endif |
37 | 37 |
38 #include "v8.h" | 38 #include "v8.h" |
39 | 39 |
40 #include "global-handles.h" | 40 #include "global-handles.h" |
41 #include "snapshot.h" | 41 #include "snapshot.h" |
42 #include "cctest.h" | 42 #include "cctest.h" |
43 | 43 |
44 using namespace v8::internal; | 44 using namespace v8::internal; |
45 | 45 |
| 46 using v8::UniqueId; |
| 47 |
46 | 48 |
47 TEST(MarkingDeque) { | 49 TEST(MarkingDeque) { |
48 CcTest::InitializeVM(); | 50 CcTest::InitializeVM(); |
49 int mem_size = 20 * kPointerSize; | 51 int mem_size = 20 * kPointerSize; |
50 byte* mem = NewArray<byte>(20*kPointerSize); | 52 byte* mem = NewArray<byte>(20*kPointerSize); |
51 Address low = reinterpret_cast<Address>(mem); | 53 Address low = reinterpret_cast<Address>(mem); |
52 Address high = low + mem_size; | 54 Address high = low + mem_size; |
53 MarkingDeque s; | 55 MarkingDeque s; |
54 s.Initialize(low, high); | 56 s.Initialize(low, high); |
55 | 57 |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 | 301 |
300 static int NumberOfWeakCalls = 0; | 302 static int NumberOfWeakCalls = 0; |
301 static void WeakPointerCallback(v8::Isolate* isolate, | 303 static void WeakPointerCallback(v8::Isolate* isolate, |
302 v8::Persistent<v8::Value> handle, | 304 v8::Persistent<v8::Value> handle, |
303 void* id) { | 305 void* id) { |
304 ASSERT(id == reinterpret_cast<void*>(1234)); | 306 ASSERT(id == reinterpret_cast<void*>(1234)); |
305 NumberOfWeakCalls++; | 307 NumberOfWeakCalls++; |
306 handle.Dispose(isolate); | 308 handle.Dispose(isolate); |
307 } | 309 } |
308 | 310 |
309 TEST(ObjectGroups) { | 311 TEST(ObjectGroupsOldApi) { |
310 FLAG_incremental_marking = false; | 312 FLAG_incremental_marking = false; |
311 CcTest::InitializeVM(); | 313 CcTest::InitializeVM(); |
312 GlobalHandles* global_handles = Isolate::Current()->global_handles(); | 314 GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
313 | 315 |
314 NumberOfWeakCalls = 0; | 316 NumberOfWeakCalls = 0; |
315 v8::HandleScope handle_scope(CcTest::isolate()); | 317 v8::HandleScope handle_scope(CcTest::isolate()); |
316 | 318 |
317 Handle<Object> g1s1 = | 319 Handle<Object> g1s1 = |
318 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | 320 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
319 Handle<Object> g1s2 = | 321 Handle<Object> g1s2 = |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 &WeakPointerCallback); | 414 &WeakPointerCallback); |
413 global_handles->MakeWeak(g2c1.location(), | 415 global_handles->MakeWeak(g2c1.location(), |
414 reinterpret_cast<void*>(1234), | 416 reinterpret_cast<void*>(1234), |
415 NULL, | 417 NULL, |
416 &WeakPointerCallback); | 418 &WeakPointerCallback); |
417 | 419 |
418 HEAP->CollectGarbage(OLD_POINTER_SPACE); | 420 HEAP->CollectGarbage(OLD_POINTER_SPACE); |
419 CHECK_EQ(7, NumberOfWeakCalls); | 421 CHECK_EQ(7, NumberOfWeakCalls); |
420 } | 422 } |
421 | 423 |
| 424 TEST(ObjectGroups) { |
| 425 FLAG_incremental_marking = false; |
| 426 CcTest::InitializeVM(); |
| 427 GlobalHandles* global_handles = reinterpret_cast<v8::internal::Isolate*>( |
| 428 CcTest::isolate())->global_handles(); |
| 429 |
| 430 NumberOfWeakCalls = 0; |
| 431 v8::HandleScope handle_scope(CcTest::isolate()); |
| 432 |
| 433 Heap* heap = reinterpret_cast<v8::internal::Isolate*>( |
| 434 CcTest::isolate())->heap(); |
| 435 Handle<Object> g1s1 = |
| 436 global_handles->Create(heap->AllocateFixedArray(1)->ToObjectChecked()); |
| 437 Handle<Object> g1s2 = |
| 438 global_handles->Create(heap->AllocateFixedArray(1)->ToObjectChecked()); |
| 439 Handle<Object> g1c1 = |
| 440 global_handles->Create(heap->AllocateFixedArray(1)->ToObjectChecked()); |
| 441 global_handles->MakeWeak(g1s1.location(), |
| 442 reinterpret_cast<void*>(1234), |
| 443 NULL, |
| 444 &WeakPointerCallback); |
| 445 global_handles->MakeWeak(g1s2.location(), |
| 446 reinterpret_cast<void*>(1234), |
| 447 NULL, |
| 448 &WeakPointerCallback); |
| 449 global_handles->MakeWeak(g1c1.location(), |
| 450 reinterpret_cast<void*>(1234), |
| 451 NULL, |
| 452 &WeakPointerCallback); |
| 453 |
| 454 Handle<Object> g2s1 = |
| 455 global_handles->Create(heap->AllocateFixedArray(1)->ToObjectChecked()); |
| 456 Handle<Object> g2s2 = |
| 457 global_handles->Create(heap->AllocateFixedArray(1)->ToObjectChecked()); |
| 458 Handle<Object> g2c1 = |
| 459 global_handles->Create(heap->AllocateFixedArray(1)->ToObjectChecked()); |
| 460 global_handles->MakeWeak(g2s1.location(), |
| 461 reinterpret_cast<void*>(1234), |
| 462 NULL, |
| 463 &WeakPointerCallback); |
| 464 global_handles->MakeWeak(g2s2.location(), |
| 465 reinterpret_cast<void*>(1234), |
| 466 NULL, |
| 467 &WeakPointerCallback); |
| 468 global_handles->MakeWeak(g2c1.location(), |
| 469 reinterpret_cast<void*>(1234), |
| 470 NULL, |
| 471 &WeakPointerCallback); |
| 472 |
| 473 Handle<Object> root = global_handles->Create(*g1s1); // make a root. |
| 474 |
| 475 // Connect group 1 and 2, make a cycle. |
| 476 Handle<FixedArray>::cast(g1s2)->set(0, *g2s2); |
| 477 Handle<FixedArray>::cast(g2s1)->set(0, *g1s1); |
| 478 |
| 479 { |
| 480 Object** g1_children[] = { g1c1.location() }; |
| 481 Object** g2_children[] = { g2c1.location() }; |
| 482 global_handles->SetObjectGroupId(g1s1.location(), v8::UniqueId(1)); |
| 483 global_handles->SetObjectGroupId(g1s2.location(), v8::UniqueId(1)); |
| 484 global_handles->AddImplicitReferences( |
| 485 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1); |
| 486 global_handles->SetObjectGroupId(g2s1.location(), v8::UniqueId(2)); |
| 487 global_handles->SetObjectGroupId(g2s2.location(), v8::UniqueId(2)); |
| 488 global_handles->AddImplicitReferences( |
| 489 Handle<HeapObject>::cast(g2s2).location(), g2_children, 1); |
| 490 } |
| 491 // Do a full GC |
| 492 heap->CollectGarbage(OLD_POINTER_SPACE); |
| 493 |
| 494 // All object should be alive. |
| 495 CHECK_EQ(0, NumberOfWeakCalls); |
| 496 |
| 497 // Weaken the root. |
| 498 global_handles->MakeWeak(root.location(), |
| 499 reinterpret_cast<void*>(1234), |
| 500 NULL, |
| 501 &WeakPointerCallback); |
| 502 // But make children strong roots---all the objects (except for children) |
| 503 // should be collectable now. |
| 504 global_handles->ClearWeakness(g1c1.location()); |
| 505 global_handles->ClearWeakness(g2c1.location()); |
| 506 |
| 507 // Groups are deleted, rebuild groups. |
| 508 { |
| 509 Object** g1_children[] = { g1c1.location() }; |
| 510 Object** g2_children[] = { g2c1.location() }; |
| 511 global_handles->SetObjectGroupId(g1s1.location(), v8::UniqueId(1)); |
| 512 global_handles->SetObjectGroupId(g1s2.location(), v8::UniqueId(1)); |
| 513 global_handles->AddImplicitReferences( |
| 514 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1); |
| 515 global_handles->SetObjectGroupId(g2s1.location(), v8::UniqueId(2)); |
| 516 global_handles->SetObjectGroupId(g2s2.location(), v8::UniqueId(2)); |
| 517 global_handles->AddImplicitReferences( |
| 518 Handle<HeapObject>::cast(g2s2).location(), g2_children, 1); |
| 519 } |
| 520 |
| 521 heap->CollectGarbage(OLD_POINTER_SPACE); |
| 522 |
| 523 // All objects should be gone. 5 global handles in total. |
| 524 CHECK_EQ(5, NumberOfWeakCalls); |
| 525 |
| 526 // And now make children weak again and collect them. |
| 527 global_handles->MakeWeak(g1c1.location(), |
| 528 reinterpret_cast<void*>(1234), |
| 529 NULL, |
| 530 &WeakPointerCallback); |
| 531 global_handles->MakeWeak(g2c1.location(), |
| 532 reinterpret_cast<void*>(1234), |
| 533 NULL, |
| 534 &WeakPointerCallback); |
| 535 |
| 536 heap->CollectGarbage(OLD_POINTER_SPACE); |
| 537 CHECK_EQ(7, NumberOfWeakCalls); |
| 538 } |
| 539 |
422 | 540 |
423 class TestRetainedObjectInfo : public v8::RetainedObjectInfo { | 541 class TestRetainedObjectInfo : public v8::RetainedObjectInfo { |
424 public: | 542 public: |
425 TestRetainedObjectInfo() : has_been_disposed_(false) {} | 543 TestRetainedObjectInfo() : has_been_disposed_(false) {} |
426 | 544 |
427 bool has_been_disposed() { return has_been_disposed_; } | 545 bool has_been_disposed() { return has_been_disposed_; } |
428 | 546 |
429 virtual void Dispose() { | 547 virtual void Dispose() { |
430 ASSERT(!has_been_disposed_); | 548 ASSERT(!has_been_disposed_); |
431 has_been_disposed_ = true; | 549 has_been_disposed_ = true; |
432 } | 550 } |
433 | 551 |
434 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) { | 552 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) { |
435 return other == this; | 553 return other == this; |
436 } | 554 } |
437 | 555 |
438 virtual intptr_t GetHash() { return 0; } | 556 virtual intptr_t GetHash() { return 0; } |
439 | 557 |
440 virtual const char* GetLabel() { return "whatever"; } | 558 virtual const char* GetLabel() { return "whatever"; } |
441 | 559 |
442 private: | 560 private: |
443 bool has_been_disposed_; | 561 bool has_been_disposed_; |
444 }; | 562 }; |
445 | 563 |
446 | 564 |
447 TEST(EmptyObjectGroups) { | 565 TEST(EmptyObjectGroupsOldApi) { |
448 CcTest::InitializeVM(); | 566 CcTest::InitializeVM(); |
449 GlobalHandles* global_handles = Isolate::Current()->global_handles(); | 567 GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
450 | 568 |
451 v8::HandleScope handle_scope(CcTest::isolate()); | 569 v8::HandleScope handle_scope(CcTest::isolate()); |
452 | 570 |
453 Handle<Object> object = | 571 Handle<Object> object = |
454 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); | 572 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); |
455 | 573 |
456 TestRetainedObjectInfo info; | 574 TestRetainedObjectInfo info; |
457 global_handles->AddObjectGroup(NULL, 0, &info); | 575 global_handles->AddObjectGroup(NULL, 0, &info); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 if (v8::internal::Snapshot::IsEnabled()) { | 676 if (v8::internal::Snapshot::IsEnabled()) { |
559 CHECK_LE(delta, 2900 * 1024); | 677 CHECK_LE(delta, 2900 * 1024); |
560 } else { | 678 } else { |
561 CHECK_LE(delta, 3400 * 1024); | 679 CHECK_LE(delta, 3400 * 1024); |
562 } | 680 } |
563 } | 681 } |
564 } | 682 } |
565 } | 683 } |
566 | 684 |
567 #endif // __linux__ and !USE_SIMULATOR | 685 #endif // __linux__ and !USE_SIMULATOR |
OLD | NEW |