| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 , m_weakBar(weakBar) | 553 , m_weakBar(weakBar) |
| 554 { | 554 { |
| 555 } | 555 } |
| 556 | 556 |
| 557 Member<Bar> m_strongBar; | 557 Member<Bar> m_strongBar; |
| 558 WeakMember<Bar> m_weakBar; | 558 WeakMember<Bar> m_weakBar; |
| 559 }; | 559 }; |
| 560 | 560 |
| 561 TEST(HeapTest, Threading) | 561 TEST(HeapTest, Threading) |
| 562 { | 562 { |
| 563 Heap::init(); | |
| 564 ThreadedHeapTester::test(); | 563 ThreadedHeapTester::test(); |
| 565 Heap::shutdown(); | |
| 566 } | |
| 567 | |
| 568 TEST(HeapTest, Init) | |
| 569 { | |
| 570 // FIXME: init and shutdown should be called via Blink | |
| 571 // initialization in the test runner. This test can be removed | |
| 572 // when that is done. | |
| 573 Heap::init(); | |
| 574 Heap::shutdown(); | |
| 575 } | 564 } |
| 576 | 565 |
| 577 TEST(HeapTest, SimpleAllocation) | 566 TEST(HeapTest, SimpleAllocation) |
| 578 { | 567 { |
| 579 // FIXME: init and shutdown should be called via Blink | |
| 580 // initialization in the test runner. | |
| 581 Heap::init(); | |
| 582 | |
| 583 // Get initial heap stats. | 568 // Get initial heap stats. |
| 584 HeapStats initialHeapStats; | 569 HeapStats initialHeapStats; |
| 585 getHeapStats(&initialHeapStats); | 570 getHeapStats(&initialHeapStats); |
| 586 EXPECT_EQ(0ul, initialHeapStats.totalObjectSpace()); | 571 EXPECT_EQ(0ul, initialHeapStats.totalObjectSpace()); |
| 587 | 572 |
| 588 // Allocate an object in the heap. | 573 // Allocate an object in the heap. |
| 589 HeapAllocatedArray* array = new HeapAllocatedArray(); | 574 HeapAllocatedArray* array = new HeapAllocatedArray(); |
| 590 HeapStats statsAfterAllocation; | 575 HeapStats statsAfterAllocation; |
| 591 getHeapStats(&statsAfterAllocation); | 576 getHeapStats(&statsAfterAllocation); |
| 592 EXPECT_TRUE(statsAfterAllocation.totalObjectSpace() >= sizeof(HeapAllocatedA
rray)); | 577 EXPECT_TRUE(statsAfterAllocation.totalObjectSpace() >= sizeof(HeapAllocatedA
rray)); |
| 593 | 578 |
| 594 // Sanity check of the contents in the heap. | 579 // Sanity check of the contents in the heap. |
| 595 EXPECT_EQ(0, array->at(0)); | 580 EXPECT_EQ(0, array->at(0)); |
| 596 EXPECT_EQ(42, array->at(42)); | 581 EXPECT_EQ(42, array->at(42)); |
| 597 EXPECT_EQ(0, array->at(128)); | 582 EXPECT_EQ(0, array->at(128)); |
| 598 EXPECT_EQ(999 % 128, array->at(999)); | 583 EXPECT_EQ(999 % 128, array->at(999)); |
| 599 | |
| 600 Heap::shutdown(); | |
| 601 } | 584 } |
| 602 | 585 |
| 603 TEST(HeapTest, SimplePersistent) | 586 TEST(HeapTest, SimplePersistent) |
| 604 { | 587 { |
| 605 // FIXME: init and shutdown should be called via Blink | 588 Persistent<TraceCounter> traceCounter = TraceCounter::create(); |
| 606 // initialization in the test runner. | 589 EXPECT_EQ(0, traceCounter->traceCount()); |
| 607 Heap::init(); | |
| 608 | 590 |
| 609 { | 591 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 610 Persistent<TraceCounter> traceCounter = TraceCounter::create(); | 592 EXPECT_EQ(1, traceCounter->traceCount()); |
| 611 EXPECT_EQ(0, traceCounter->traceCount()); | |
| 612 | 593 |
| 613 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 594 Persistent<ClassWithMember> classWithMember = ClassWithMember::create(); |
| 614 EXPECT_EQ(1, traceCounter->traceCount()); | 595 EXPECT_EQ(0, classWithMember->traceCount()); |
| 615 | 596 |
| 616 Persistent<ClassWithMember> classWithMember = ClassWithMember::create(); | 597 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 617 EXPECT_EQ(0, classWithMember->traceCount()); | 598 EXPECT_EQ(1, classWithMember->traceCount()); |
| 618 | 599 EXPECT_EQ(2, traceCounter->traceCount()); |
| 619 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | |
| 620 EXPECT_EQ(1, classWithMember->traceCount()); | |
| 621 EXPECT_EQ(2, traceCounter->traceCount()); | |
| 622 } | |
| 623 | |
| 624 Heap::shutdown(); | |
| 625 } | 600 } |
| 626 | 601 |
| 627 TEST(HeapTest, SimpleFinalization) | 602 TEST(HeapTest, SimpleFinalization) |
| 628 { | 603 { |
| 629 // FIXME: init and shutdown should be called via Blink | |
| 630 // initialization in the test runner. | |
| 631 Heap::init(); | |
| 632 | |
| 633 { | 604 { |
| 634 Persistent<SimpleFinalizedObject> finalized = SimpleFinalizedObject::cre
ate(); | 605 Persistent<SimpleFinalizedObject> finalized = SimpleFinalizedObject::cre
ate(); |
| 635 EXPECT_EQ(0, SimpleFinalizedObject::s_destructorCalls); | 606 EXPECT_EQ(0, SimpleFinalizedObject::s_destructorCalls); |
| 636 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 607 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 637 EXPECT_EQ(0, SimpleFinalizedObject::s_destructorCalls); | 608 EXPECT_EQ(0, SimpleFinalizedObject::s_destructorCalls); |
| 638 } | 609 } |
| 639 | 610 |
| 640 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 611 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 641 EXPECT_EQ(1, SimpleFinalizedObject::s_destructorCalls); | 612 EXPECT_EQ(1, SimpleFinalizedObject::s_destructorCalls); |
| 642 | |
| 643 Heap::shutdown(); | |
| 644 } | 613 } |
| 645 | 614 |
| 646 TEST(HeapTest, TypedHeapSanity) | 615 TEST(HeapTest, TypedHeapSanity) |
| 647 { | 616 { |
| 648 // FIXME: init and shutdown should be called via Blink | 617 // We use TraceCounter for allocating an object on the general heap. |
| 649 // initialization in the test runner. | 618 Persistent<TraceCounter> generalHeapObject = TraceCounter::create(); |
| 650 Heap::init(); | 619 Persistent<TestTypedHeapClass> typedHeapObject = TestTypedHeapClass::create(
); |
| 651 | 620 EXPECT_NE(pageHeaderAddress(reinterpret_cast<Address>(generalHeapObject.get(
))), |
| 652 { | 621 pageHeaderAddress(reinterpret_cast<Address>(typedHeapObject.get()))); |
| 653 // We use TraceCounter for allocating an object on the general heap. | |
| 654 Persistent<TraceCounter> generalHeapObject = TraceCounter::create(); | |
| 655 Persistent<TestTypedHeapClass> typedHeapObject = TestTypedHeapClass::cre
ate(); | |
| 656 EXPECT_NE(pageHeaderAddress(reinterpret_cast<Address>(generalHeapObject.
get())), | |
| 657 pageHeaderAddress(reinterpret_cast<Address>(typedHeapObject.get())))
; | |
| 658 } | |
| 659 | |
| 660 Heap::shutdown(); | |
| 661 } | 622 } |
| 662 | 623 |
| 663 TEST(HeapTest, NoAllocation) | 624 TEST(HeapTest, NoAllocation) |
| 664 { | 625 { |
| 665 // FIXME: init and shutdown should be called via Blink | |
| 666 // initialization in the test runner. | |
| 667 Heap::init(); | |
| 668 | |
| 669 EXPECT_TRUE(ThreadState::current()->isAllocationAllowed()); | 626 EXPECT_TRUE(ThreadState::current()->isAllocationAllowed()); |
| 670 { | 627 { |
| 671 // Disallow allocation | 628 // Disallow allocation |
| 672 NoAllocationScope<AnyThread> noAllocationScope; | 629 NoAllocationScope<AnyThread> noAllocationScope; |
| 673 EXPECT_FALSE(ThreadState::current()->isAllocationAllowed()); | 630 EXPECT_FALSE(ThreadState::current()->isAllocationAllowed()); |
| 674 } | 631 } |
| 675 EXPECT_TRUE(ThreadState::current()->isAllocationAllowed()); | 632 EXPECT_TRUE(ThreadState::current()->isAllocationAllowed()); |
| 676 | |
| 677 Heap::shutdown(); | |
| 678 } | 633 } |
| 679 | 634 |
| 680 TEST(HeapTest, Members) | 635 TEST(HeapTest, Members) |
| 681 { | 636 { |
| 682 // FIXME: init and shutdown should be called via Blink | |
| 683 // initialization in the test runner. | |
| 684 Heap::init(); | |
| 685 | |
| 686 Bar::s_live = 0; | 637 Bar::s_live = 0; |
| 687 { | 638 { |
| 688 Persistent<Baz> h1; | 639 Persistent<Baz> h1; |
| 689 Persistent<Baz> h2; | 640 Persistent<Baz> h2; |
| 690 { | 641 { |
| 691 h1 = Baz::create(Bar::create()); | 642 h1 = Baz::create(Bar::create()); |
| 692 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 643 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 693 EXPECT_EQ(1u, Bar::s_live); | 644 EXPECT_EQ(1u, Bar::s_live); |
| 694 h2 = Baz::create(Bar::create()); | 645 h2 = Baz::create(Bar::create()); |
| 695 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 646 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 696 EXPECT_EQ(2u, Bar::s_live); | 647 EXPECT_EQ(2u, Bar::s_live); |
| 697 } | 648 } |
| 698 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 649 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 699 EXPECT_EQ(2u, Bar::s_live); | 650 EXPECT_EQ(2u, Bar::s_live); |
| 700 h1->clear(); | 651 h1->clear(); |
| 701 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 652 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 702 EXPECT_EQ(1u, Bar::s_live); | 653 EXPECT_EQ(1u, Bar::s_live); |
| 703 } | 654 } |
| 704 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 655 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 705 EXPECT_EQ(0u, Bar::s_live); | 656 EXPECT_EQ(0u, Bar::s_live); |
| 706 | |
| 707 Heap::shutdown(); | |
| 708 } | 657 } |
| 709 | 658 |
| 710 TEST(HeapTest, DeepTest) | 659 TEST(HeapTest, DeepTest) |
| 711 { | 660 { |
| 712 // FIXME: init and shutdown should be called via Blink | |
| 713 // initialization in the test runner. | |
| 714 Heap::init(); | |
| 715 | |
| 716 const unsigned depth = 100000; | 661 const unsigned depth = 100000; |
| 717 Bar::s_live = 0; | 662 Bar::s_live = 0; |
| 718 { | 663 { |
| 719 Bar* bar = Bar::create(); | 664 Bar* bar = Bar::create(); |
| 720 EXPECT_TRUE(ThreadState::current()->contains(bar)); | 665 EXPECT_TRUE(ThreadState::current()->contains(bar)); |
| 721 Foo* foo = Foo::create(bar); | 666 Foo* foo = Foo::create(bar); |
| 722 EXPECT_TRUE(ThreadState::current()->contains(foo)); | 667 EXPECT_TRUE(ThreadState::current()->contains(foo)); |
| 723 EXPECT_EQ(2u, Bar::s_live); | 668 EXPECT_EQ(2u, Bar::s_live); |
| 724 for (unsigned i = 0; i < depth; i++) { | 669 for (unsigned i = 0; i < depth; i++) { |
| 725 Foo* foo2 = Foo::create(foo); | 670 Foo* foo2 = Foo::create(foo); |
| 726 foo = foo2; | 671 foo = foo2; |
| 727 EXPECT_TRUE(ThreadState::current()->contains(foo)); | 672 EXPECT_TRUE(ThreadState::current()->contains(foo)); |
| 728 } | 673 } |
| 729 EXPECT_EQ(depth + 2, Bar::s_live); | 674 EXPECT_EQ(depth + 2, Bar::s_live); |
| 730 Heap::collectGarbage(ThreadState::HeapPointersOnStack); | 675 Heap::collectGarbage(ThreadState::HeapPointersOnStack); |
| 731 EXPECT_EQ(depth + 2, Bar::s_live); | 676 EXPECT_EQ(depth + 2, Bar::s_live); |
| 732 } | 677 } |
| 733 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 678 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 734 EXPECT_EQ(0u, Bar::s_live); | 679 EXPECT_EQ(0u, Bar::s_live); |
| 735 | |
| 736 Heap::shutdown(); | |
| 737 } | 680 } |
| 738 | 681 |
| 739 TEST(HeapTest, WideTest) | 682 TEST(HeapTest, WideTest) |
| 740 { | 683 { |
| 741 // FIXME: init and shutdown should be called via Blink | |
| 742 // initialization in the test runner. | |
| 743 Heap::init(); | |
| 744 | |
| 745 Bar::s_live = 0; | 684 Bar::s_live = 0; |
| 746 { | 685 { |
| 747 Bars* bars = Bars::create(); | 686 Bars* bars = Bars::create(); |
| 748 unsigned width = Bars::width; | 687 unsigned width = Bars::width; |
| 749 EXPECT_EQ(width + 1, Bar::s_live); | 688 EXPECT_EQ(width + 1, Bar::s_live); |
| 750 Heap::collectGarbage(ThreadState::HeapPointersOnStack); | 689 Heap::collectGarbage(ThreadState::HeapPointersOnStack); |
| 751 EXPECT_EQ(width + 1, Bar::s_live); | 690 EXPECT_EQ(width + 1, Bar::s_live); |
| 752 // Use bars here to make sure that it will be on the stack | 691 // Use bars here to make sure that it will be on the stack |
| 753 // for the conservative stack scan to find. | 692 // for the conservative stack scan to find. |
| 754 EXPECT_EQ(width, bars->getWidth()); | 693 EXPECT_EQ(width, bars->getWidth()); |
| 755 } | 694 } |
| 756 EXPECT_EQ(Bars::width + 1, Bar::s_live); | 695 EXPECT_EQ(Bars::width + 1, Bar::s_live); |
| 757 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 696 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 758 EXPECT_EQ(0u, Bar::s_live); | 697 EXPECT_EQ(0u, Bar::s_live); |
| 759 | |
| 760 Heap::shutdown(); | |
| 761 } | 698 } |
| 762 | 699 |
| 763 TEST(HeapTest, HashMapOfMembers) | 700 TEST(HeapTest, HashMapOfMembers) |
| 764 { | 701 { |
| 765 // FIXME: init and shutdown should be called via Blink | |
| 766 // initialization in the test runner. | |
| 767 Heap::init(); | |
| 768 | |
| 769 HeapStats initialHeapSize; | 702 HeapStats initialHeapSize; |
| 770 IntWrapper::s_destructorCalls = 0; | 703 IntWrapper::s_destructorCalls = 0; |
| 771 | 704 |
| 772 clearOutOldGarbage(&initialHeapSize); | 705 clearOutOldGarbage(&initialHeapSize); |
| 773 { | 706 { |
| 774 typedef HashMap< | 707 typedef HashMap< |
| 775 Member<IntWrapper>, | 708 Member<IntWrapper>, |
| 776 Member<IntWrapper>, | 709 Member<IntWrapper>, |
| 777 DefaultHash<Member<IntWrapper> >::Hash, | 710 DefaultHash<Member<IntWrapper> >::Hash, |
| 778 HashTraits<Member<IntWrapper> >, | 711 HashTraits<Member<IntWrapper> >, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 841 getHeapStats(&afterGC3); | 774 getHeapStats(&afterGC3); |
| 842 EXPECT_TRUE(afterGC3.totalObjectSpace() < afterAdding1000.totalObjectSpa
ce()); | 775 EXPECT_TRUE(afterGC3.totalObjectSpace() < afterAdding1000.totalObjectSpa
ce()); |
| 843 } | 776 } |
| 844 | 777 |
| 845 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 778 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 846 // The objects 'one', anotherOne, and the 999 other pairs. | 779 // The objects 'one', anotherOne, and the 999 other pairs. |
| 847 EXPECT_EQ(IntWrapper::s_destructorCalls, 2000); | 780 EXPECT_EQ(IntWrapper::s_destructorCalls, 2000); |
| 848 HeapStats afterGC4; | 781 HeapStats afterGC4; |
| 849 getHeapStats(&afterGC4); | 782 getHeapStats(&afterGC4); |
| 850 EXPECT_EQ(afterGC4.totalObjectSpace(), initialHeapSize.totalObjectSpace()); | 783 EXPECT_EQ(afterGC4.totalObjectSpace(), initialHeapSize.totalObjectSpace()); |
| 851 | |
| 852 Heap::shutdown(); | |
| 853 } | 784 } |
| 854 | 785 |
| 855 TEST(HeapTest, NestedAllocation) | 786 TEST(HeapTest, NestedAllocation) |
| 856 { | 787 { |
| 857 // FIXME: init and shutdown should be called via Blink | |
| 858 // initialization in the test runner. | |
| 859 Heap::init(); | |
| 860 | |
| 861 HeapStats initialHeapSize; | 788 HeapStats initialHeapSize; |
| 862 clearOutOldGarbage(&initialHeapSize); | 789 clearOutOldGarbage(&initialHeapSize); |
| 863 { | 790 { |
| 864 Persistent<ConstructorAllocation> constructorAllocation = ConstructorAll
ocation::create(); | 791 Persistent<ConstructorAllocation> constructorAllocation = ConstructorAll
ocation::create(); |
| 865 } | 792 } |
| 866 HeapStats afterFree; | 793 HeapStats afterFree; |
| 867 clearOutOldGarbage(&afterFree); | 794 clearOutOldGarbage(&afterFree); |
| 868 EXPECT_TRUE(initialHeapSize == afterFree); | 795 EXPECT_TRUE(initialHeapSize == afterFree); |
| 869 | |
| 870 Heap::shutdown(); | |
| 871 } | 796 } |
| 872 | 797 |
| 873 TEST(HeapTest, LargeObjects) | 798 TEST(HeapTest, LargeObjects) |
| 874 { | 799 { |
| 875 // FIXME: init and shutdown should be called via Blink | |
| 876 // initialization in the test runner. | |
| 877 Heap::init(); | |
| 878 | |
| 879 HeapStats initialHeapSize; | 800 HeapStats initialHeapSize; |
| 880 clearOutOldGarbage(&initialHeapSize); | 801 clearOutOldGarbage(&initialHeapSize); |
| 881 IntWrapper::s_destructorCalls = 0; | 802 IntWrapper::s_destructorCalls = 0; |
| 882 LargeObject::s_destructorCalls = 0; | 803 LargeObject::s_destructorCalls = 0; |
| 883 { | 804 { |
| 884 int slack = 8; // LargeObject points to an IntWrapper that is also alloc
ated. | 805 int slack = 8; // LargeObject points to an IntWrapper that is also alloc
ated. |
| 885 Persistent<LargeObject> object = LargeObject::create(); | 806 Persistent<LargeObject> object = LargeObject::create(); |
| 886 HeapStats afterAllocation; | 807 HeapStats afterAllocation; |
| 887 clearOutOldGarbage(&afterAllocation); | 808 clearOutOldGarbage(&afterAllocation); |
| 888 { | 809 { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 914 EXPECT_TRUE(oneLargeObject == afterAllocation); | 835 EXPECT_TRUE(oneLargeObject == afterAllocation); |
| 915 EXPECT_EQ(10, IntWrapper::s_destructorCalls); | 836 EXPECT_EQ(10, IntWrapper::s_destructorCalls); |
| 916 EXPECT_EQ(10, LargeObject::s_destructorCalls); | 837 EXPECT_EQ(10, LargeObject::s_destructorCalls); |
| 917 } | 838 } |
| 918 HeapStats backToInitial; | 839 HeapStats backToInitial; |
| 919 clearOutOldGarbage(&backToInitial); | 840 clearOutOldGarbage(&backToInitial); |
| 920 EXPECT_TRUE(initialHeapSize == backToInitial); | 841 EXPECT_TRUE(initialHeapSize == backToInitial); |
| 921 EXPECT_EQ(11, IntWrapper::s_destructorCalls); | 842 EXPECT_EQ(11, IntWrapper::s_destructorCalls); |
| 922 EXPECT_EQ(11, LargeObject::s_destructorCalls); | 843 EXPECT_EQ(11, LargeObject::s_destructorCalls); |
| 923 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 844 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 924 | |
| 925 Heap::shutdown(); | |
| 926 } | 845 } |
| 927 | 846 |
| 928 TEST(HeapTest, RefCountedGarbageCollected) | 847 TEST(HeapTest, RefCountedGarbageCollected) |
| 929 { | 848 { |
| 930 // FIXME: init and shutdown should be called via Blink | |
| 931 // initialization in the test runner. | |
| 932 Heap::init(); | |
| 933 | |
| 934 RefCountedAndGarbageCollected::s_destructorCalls = 0; | 849 RefCountedAndGarbageCollected::s_destructorCalls = 0; |
| 935 { | 850 { |
| 936 RefPtr<RefCountedAndGarbageCollected> refPtr3; | 851 RefPtr<RefCountedAndGarbageCollected> refPtr3; |
| 937 { | 852 { |
| 938 Persistent<RefCountedAndGarbageCollected> persistent; | 853 Persistent<RefCountedAndGarbageCollected> persistent; |
| 939 { | 854 { |
| 940 RefPtr<RefCountedAndGarbageCollected> refPtr1 = RefCountedAndGar
bageCollected::create(); | 855 RefPtr<RefCountedAndGarbageCollected> refPtr1 = RefCountedAndGar
bageCollected::create(); |
| 941 RefPtr<RefCountedAndGarbageCollected> refPtr2 = RefCountedAndGar
bageCollected::create(); | 856 RefPtr<RefCountedAndGarbageCollected> refPtr2 = RefCountedAndGar
bageCollected::create(); |
| 942 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 857 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 943 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); | 858 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); |
| 944 persistent = refPtr1.get(); | 859 persistent = refPtr1.get(); |
| 945 } | 860 } |
| 946 // Reference count is zero for both objects but one of | 861 // Reference count is zero for both objects but one of |
| 947 // them is kept alive by a persistent handle. | 862 // them is kept alive by a persistent handle. |
| 948 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 863 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 949 EXPECT_EQ(1, RefCountedAndGarbageCollected::s_destructorCalls); | 864 EXPECT_EQ(1, RefCountedAndGarbageCollected::s_destructorCalls); |
| 950 refPtr3 = persistent; | 865 refPtr3 = persistent; |
| 951 } | 866 } |
| 952 // The persistent handle is gone but the ref count has been | 867 // The persistent handle is gone but the ref count has been |
| 953 // increased to 1. | 868 // increased to 1. |
| 954 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 869 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 955 EXPECT_EQ(1, RefCountedAndGarbageCollected::s_destructorCalls); | 870 EXPECT_EQ(1, RefCountedAndGarbageCollected::s_destructorCalls); |
| 956 } | 871 } |
| 957 // Both persistent handle is gone and ref count is zero so the | 872 // Both persistent handle is gone and ref count is zero so the |
| 958 // object can be collected. | 873 // object can be collected. |
| 959 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 874 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 960 EXPECT_EQ(2, RefCountedAndGarbageCollected::s_destructorCalls); | 875 EXPECT_EQ(2, RefCountedAndGarbageCollected::s_destructorCalls); |
| 961 | |
| 962 Heap::shutdown(); | |
| 963 } | 876 } |
| 964 | 877 |
| 965 TEST(HeapTest, WeakMembers) | 878 TEST(HeapTest, WeakMembers) |
| 966 { | 879 { |
| 967 // FIXME: init and shutdown should be called via Blink | |
| 968 // initialization in the test runner. | |
| 969 Heap::init(); | |
| 970 | |
| 971 Bar::s_live = 0; | 880 Bar::s_live = 0; |
| 972 { | 881 { |
| 973 Persistent<Bar> h1 = Bar::create(); | 882 Persistent<Bar> h1 = Bar::create(); |
| 974 Persistent<Weak> h4; | 883 Persistent<Weak> h4; |
| 975 Persistent<WithWeakMember> h5; | 884 Persistent<WithWeakMember> h5; |
| 976 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 885 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 977 ASSERT_EQ(1u, Bar::s_live); // h1 is live. | 886 ASSERT_EQ(1u, Bar::s_live); // h1 is live. |
| 978 { | 887 { |
| 979 Bar* h2 = Bar::create(); | 888 Bar* h2 = Bar::create(); |
| 980 Bar* h3 = Bar::create(); | 889 Bar* h3 = Bar::create(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 996 EXPECT_FALSE(h5->weakIsThere()); // h3 is gone from weak pointer. | 905 EXPECT_FALSE(h5->weakIsThere()); // h3 is gone from weak pointer. |
| 997 h1.release(); // Zero out h1. | 906 h1.release(); // Zero out h1. |
| 998 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 907 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 999 EXPECT_EQ(3u, Bar::s_live); // Only h4, h5 and h2 are left. | 908 EXPECT_EQ(3u, Bar::s_live); // Only h4, h5 and h2 are left. |
| 1000 EXPECT_TRUE(h4->strongIsThere()); // h2 is still pointed to from h4. | 909 EXPECT_TRUE(h4->strongIsThere()); // h2 is still pointed to from h4. |
| 1001 EXPECT_TRUE(h5->strongIsThere()); // h2 is still pointed to from h5. | 910 EXPECT_TRUE(h5->strongIsThere()); // h2 is still pointed to from h5. |
| 1002 } | 911 } |
| 1003 // h4 and h5 have gone out of scope now and they were keeping h2 alive. | 912 // h4 and h5 have gone out of scope now and they were keeping h2 alive. |
| 1004 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 913 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 1005 EXPECT_EQ(0u, Bar::s_live); // All gone. | 914 EXPECT_EQ(0u, Bar::s_live); // All gone. |
| 1006 | |
| 1007 Heap::shutdown(); | |
| 1008 } | 915 } |
| 1009 | 916 |
| 1010 TEST(HeapTest, Comparisons) | 917 TEST(HeapTest, Comparisons) |
| 1011 { | 918 { |
| 1012 Heap::init(); | 919 Persistent<Bar> barPersistent = Bar::create(); |
| 1013 | 920 Persistent<Foo> fooPersistent = Foo::create(barPersistent); |
| 1014 { | 921 EXPECT_TRUE(barPersistent != fooPersistent); |
| 1015 Persistent<Bar> barPersistent = Bar::create(); | 922 barPersistent = fooPersistent; |
| 1016 Persistent<Foo> fooPersistent = Foo::create(barPersistent); | 923 EXPECT_TRUE(barPersistent == fooPersistent); |
| 1017 EXPECT_TRUE(barPersistent != fooPersistent); | |
| 1018 barPersistent = fooPersistent; | |
| 1019 EXPECT_TRUE(barPersistent == fooPersistent); | |
| 1020 } | |
| 1021 | |
| 1022 Heap::shutdown(); | |
| 1023 } | 924 } |
| 1024 | 925 |
| 1025 DEFINE_GC_INFO(Bar); | 926 DEFINE_GC_INFO(Bar); |
| 1026 DEFINE_GC_INFO(Baz); | 927 DEFINE_GC_INFO(Baz); |
| 1027 DEFINE_GC_INFO(ClassWithMember); | 928 DEFINE_GC_INFO(ClassWithMember); |
| 1028 DEFINE_GC_INFO(ConstructorAllocation); | 929 DEFINE_GC_INFO(ConstructorAllocation); |
| 1029 DEFINE_GC_INFO(HeapAllocatedArray); | 930 DEFINE_GC_INFO(HeapAllocatedArray); |
| 1030 DEFINE_GC_INFO(IntWrapper); | 931 DEFINE_GC_INFO(IntWrapper); |
| 1031 DEFINE_GC_INFO(LargeObject); | 932 DEFINE_GC_INFO(LargeObject); |
| 1032 DEFINE_GC_INFO(RefCountedAndGarbageCollected); | 933 DEFINE_GC_INFO(RefCountedAndGarbageCollected); |
| 1033 DEFINE_GC_INFO(SimpleFinalizedObject); | 934 DEFINE_GC_INFO(SimpleFinalizedObject); |
| 1034 DEFINE_GC_INFO(TestTypedHeapClass); | 935 DEFINE_GC_INFO(TestTypedHeapClass); |
| 1035 DEFINE_GC_INFO(TraceCounter); | 936 DEFINE_GC_INFO(TraceCounter); |
| 1036 | 937 |
| 1037 } // namespace | 938 } // namespace |
| OLD | NEW |