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

Side by Side Diff: Source/heap/HeapTest.cpp

Issue 130493003: Alternative approach to supporting ref counting and garbage collection (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix copyright. Created 6 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « Source/heap/Heap.h ('k') | Source/wtf/Ptr.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 540 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 : Bar() 551 : Bar()
552 , m_strongBar(strongBar) 552 , m_strongBar(strongBar)
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
562 class SuperClass;
563
564 class PointsBack : public GarbageCollectedFinalized<PointsBack>::FromRefCounted {
565 DECLARE_GC_INFO;
566 public:
567 static Ptr<PointsBack>::FromPassRefPtr create()
568 {
569 return adoptRefWillBeNoop(new PointsBack());
570 }
571
572 ~PointsBack()
573 {
574 --s_aliveCount;
575 }
576
577 void setBackPointer(SuperClass* backPointer)
578 {
579 m_backPointer = backPointer;
580 }
581
582 SuperClass* backPointer() const { return m_backPointer; }
583
584 void trace(Visitor* visitor)
585 {
586 #if ENABLE_OILPAN
587 visitor->trace(m_backPointer);
588 #endif
589 }
590
591 static int s_aliveCount;
592 private:
593 PointsBack() : m_backPointer(nullptr)
594 {
595 ++s_aliveCount;
596 }
597
598 WeakMember<SuperClass>::FromPtr m_backPointer;
599 };
600
601 int PointsBack::s_aliveCount = 0;
602
603 class SuperClass : public GarbageCollectedFinalized<SuperClass>::FromRefCounted {
604 DECLARE_GC_INFO;
605 public:
606 static Ptr<SuperClass>::FromPassRefPtr create(Ptr<PointsBack>::FromPassRefPt r pointsBack)
607 {
608 return adoptRefWillBeNoop(new SuperClass(pointsBack));
609 }
610
611 virtual ~SuperClass()
612 {
613 #if !ENABLE_OILPAN
614 m_pointsBack->setBackPointer(0);
615 #endif
616 --s_aliveCount;
617 }
618
619 void doStuff(Ptr<SuperClass>::FromPassRefPtr targetPass, PointsBack* pointsB ack, int superClassCount)
620 {
621 Ptr<SuperClass>::FromRefPtr target = targetPass;
622 Heap::collectGarbage(ThreadState::HeapPointersOnStack);
623 EXPECT_EQ(pointsBack, target->pointsBack());
624 EXPECT_EQ(superClassCount, SuperClass::s_aliveCount);
625 }
626
627 virtual void trace(Visitor* visitor)
628 {
629 #if ENABLE_OILPAN
630 visitor->trace(m_pointsBack);
631 #endif
632 }
633
634 PointsBack* pointsBack() const { return m_pointsBack.get(); }
635
636 static int s_aliveCount;
637 protected:
638 explicit SuperClass(Ptr<PointsBack>::FromPassRefPtr pointsBack)
639 : m_pointsBack(pointsBack)
640 {
641 m_pointsBack->setBackPointer(this);
642 ++s_aliveCount;
643 }
644
645 private:
646 Member<PointsBack>::FromRefPtr m_pointsBack;
647 };
648
649 int SuperClass::s_aliveCount = 0;
650 class SubData : public GarbageCollectedFinalized<SubData>::FromNoBase {
651 DECLARE_GC_INFO
652 public:
653 SubData() { ++s_aliveCount; }
654 ~SubData() { --s_aliveCount; }
655
656 void trace(Visitor*) { }
657
658 static int s_aliveCount;
659 };
660
661 int SubData::s_aliveCount = 0;
662
663 class SubClass : public SuperClass {
664 public:
665 static Ptr<SubClass>::FromPassRefPtr create(Ptr<PointsBack>::FromPassRefPtr pointsBack)
666 {
667 return adoptRefWillBeNoop(new SubClass(pointsBack));
668 }
669
670 virtual ~SubClass()
671 {
672 --s_aliveCount;
673 }
674
675 virtual void trace(Visitor* visitor)
676 {
677 #if ENABLE_OILPAN
678 SuperClass::trace(visitor);
679 visitor->trace(m_data);
680 #endif
681 }
682
683 static int s_aliveCount;
684 private:
685 explicit SubClass(Ptr<PointsBack>::FromPassRefPtr pointsBack)
686 : SuperClass(pointsBack)
687 , m_data(adoptPtrWillBeNoop(new SubData()))
688 {
689 ++s_aliveCount;
690 }
691
692 private:
693 Member<SubData>::FromOwnPtr m_data;
694 };
695
696 int SubClass::s_aliveCount = 0;
697
698 TEST(HeapTest, Transition)
699 {
700 Heap::init();
701
702 {
703 Persistent<PointsBack>::FromRefPtr pointsBack1 = PointsBack::create();
704 Persistent<PointsBack>::FromRefPtr pointsBack2 = PointsBack::create();
705 Persistent<SuperClass>::FromRefPtr superClass = SuperClass::create(point sBack1);
706 Persistent<SubClass>::FromRefPtr subClass = SubClass::create(pointsBack2 );
707 EXPECT_EQ(2, PointsBack::s_aliveCount);
708 EXPECT_EQ(2, SuperClass::s_aliveCount);
709 EXPECT_EQ(1, SubClass::s_aliveCount);
710 EXPECT_EQ(1, SubData::s_aliveCount);
711
712 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack);
713 EXPECT_EQ(2, PointsBack::s_aliveCount);
714 EXPECT_EQ(2, SuperClass::s_aliveCount);
715 EXPECT_EQ(1, SubClass::s_aliveCount);
716 EXPECT_EQ(1, SubData::s_aliveCount);
717
718 superClass->doStuff(superClass.release(), pointsBack1.get(), 2);
719 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack);
720 EXPECT_EQ(2, PointsBack::s_aliveCount);
721 EXPECT_EQ(1, SuperClass::s_aliveCount);
722 EXPECT_EQ(1, SubClass::s_aliveCount);
723 EXPECT_EQ(1, SubData::s_aliveCount);
724 EXPECT_EQ(0, pointsBack1->backPointer());
725
726 pointsBack1.release();
727 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack);
728 EXPECT_EQ(1, PointsBack::s_aliveCount);
729 EXPECT_EQ(1, SuperClass::s_aliveCount);
730 EXPECT_EQ(1, SubClass::s_aliveCount);
731 EXPECT_EQ(1, SubData::s_aliveCount);
732
733 subClass->doStuff(subClass.release(), pointsBack2.get(), 1);
734 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack);
735 EXPECT_EQ(1, PointsBack::s_aliveCount);
736 EXPECT_EQ(0, SuperClass::s_aliveCount);
737 EXPECT_EQ(0, SubClass::s_aliveCount);
738 EXPECT_EQ(0, SubData::s_aliveCount);
739 EXPECT_EQ(0, pointsBack2->backPointer());
740
741 pointsBack2.release();
742 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack);
743 EXPECT_EQ(0, PointsBack::s_aliveCount);
744 EXPECT_EQ(0, SuperClass::s_aliveCount);
745 EXPECT_EQ(0, SubClass::s_aliveCount);
746 EXPECT_EQ(0, SubData::s_aliveCount);
747
748 EXPECT_TRUE(superClass == subClass);
749 }
750
751 Heap::shutdown();
752 }
753
561 TEST(HeapTest, Threading) 754 TEST(HeapTest, Threading)
562 { 755 {
563 ThreadedHeapTester::test(); 756 ThreadedHeapTester::test();
564 } 757 }
565 758
566 TEST(HeapTest, SimpleAllocation) 759 TEST(HeapTest, SimpleAllocation)
567 { 760 {
568 // Get initial heap stats. 761 // Get initial heap stats.
569 HeapStats initialHeapStats; 762 HeapStats initialHeapStats;
570 getHeapStats(&initialHeapStats); 763 getHeapStats(&initialHeapStats);
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 EXPECT_TRUE(barPersistent == fooPersistent); 1116 EXPECT_TRUE(barPersistent == fooPersistent);
924 } 1117 }
925 1118
926 DEFINE_GC_INFO(Bar); 1119 DEFINE_GC_INFO(Bar);
927 DEFINE_GC_INFO(Baz); 1120 DEFINE_GC_INFO(Baz);
928 DEFINE_GC_INFO(ClassWithMember); 1121 DEFINE_GC_INFO(ClassWithMember);
929 DEFINE_GC_INFO(ConstructorAllocation); 1122 DEFINE_GC_INFO(ConstructorAllocation);
930 DEFINE_GC_INFO(HeapAllocatedArray); 1123 DEFINE_GC_INFO(HeapAllocatedArray);
931 DEFINE_GC_INFO(IntWrapper); 1124 DEFINE_GC_INFO(IntWrapper);
932 DEFINE_GC_INFO(LargeObject); 1125 DEFINE_GC_INFO(LargeObject);
1126 DEFINE_GC_INFO(PointsBack);
933 DEFINE_GC_INFO(RefCountedAndGarbageCollected); 1127 DEFINE_GC_INFO(RefCountedAndGarbageCollected);
934 DEFINE_GC_INFO(SimpleFinalizedObject); 1128 DEFINE_GC_INFO(SimpleFinalizedObject);
1129 DEFINE_GC_INFO(SuperClass);
1130 DEFINE_GC_INFO(SubData);
935 DEFINE_GC_INFO(TestTypedHeapClass); 1131 DEFINE_GC_INFO(TestTypedHeapClass);
936 DEFINE_GC_INFO(TraceCounter); 1132 DEFINE_GC_INFO(TraceCounter);
937 1133
938 } // namespace 1134 } // namespace
OLDNEW
« no previous file with comments | « Source/heap/Heap.h ('k') | Source/wtf/Ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698