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 804 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 m_intWrapper = IntWrapper::create(23); | 815 m_intWrapper = IntWrapper::create(23); |
816 } | 816 } |
817 Member<IntWrapper> m_intWrapper; | 817 Member<IntWrapper> m_intWrapper; |
818 char m_data[s_length]; | 818 char m_data[s_length]; |
819 }; | 819 }; |
820 | 820 |
821 int LargeObject::s_destructorCalls = 0; | 821 int LargeObject::s_destructorCalls = 0; |
822 | 822 |
823 class RefCountedAndGarbageCollected : public RefCountedGarbageCollected<RefCount
edAndGarbageCollected> { | 823 class RefCountedAndGarbageCollected : public RefCountedGarbageCollected<RefCount
edAndGarbageCollected> { |
824 public: | 824 public: |
825 static PassRefPtr<RefCountedAndGarbageCollected> create() | 825 static RefCountedAndGarbageCollected* create() |
826 { | 826 { |
827 return adoptRef(new RefCountedAndGarbageCollected()); | 827 return new RefCountedAndGarbageCollected(); |
828 } | 828 } |
829 | 829 |
830 ~RefCountedAndGarbageCollected() | 830 ~RefCountedAndGarbageCollected() |
831 { | 831 { |
832 ++s_destructorCalls; | 832 ++s_destructorCalls; |
833 } | 833 } |
834 | 834 |
835 // These are here with their default implementations so you can break in | 835 // These are here with their default implementations so you can break in |
836 // them in the debugger. | 836 // them in the debugger. |
837 void ref() { RefCountedGarbageCollected<RefCountedAndGarbageCollected>::ref(
); } | 837 void ref() { RefCountedGarbageCollected<RefCountedAndGarbageCollected>::ref(
); } |
838 void deref() { RefCountedGarbageCollected<RefCountedAndGarbageCollected>::de
ref(); } | 838 void deref() { RefCountedGarbageCollected<RefCountedAndGarbageCollected>::de
ref(); } |
839 | 839 |
840 void trace(Visitor*) { } | 840 void trace(Visitor*) { } |
841 | 841 |
842 static int s_destructorCalls; | 842 static int s_destructorCalls; |
843 | 843 |
844 private: | 844 private: |
845 RefCountedAndGarbageCollected() | 845 RefCountedAndGarbageCollected() |
846 { | 846 { |
847 } | 847 } |
848 }; | 848 }; |
849 | 849 |
850 int RefCountedAndGarbageCollected::s_destructorCalls = 0; | 850 int RefCountedAndGarbageCollected::s_destructorCalls = 0; |
851 | 851 |
852 class RefCountedAndGarbageCollected2 : public HeapTestOtherSuperClass, public Re
fCountedGarbageCollected<RefCountedAndGarbageCollected2> { | 852 class RefCountedAndGarbageCollected2 : public HeapTestOtherSuperClass, public Re
fCountedGarbageCollected<RefCountedAndGarbageCollected2> { |
853 public: | 853 public: |
854 static RefCountedAndGarbageCollected2* create() | 854 static RefCountedAndGarbageCollected2* create() |
855 { | 855 { |
856 return adoptRefCountedGarbageCollected(new RefCountedAndGarbageCollected
2()); | 856 return new RefCountedAndGarbageCollected2(); |
857 } | 857 } |
858 | 858 |
859 ~RefCountedAndGarbageCollected2() | 859 ~RefCountedAndGarbageCollected2() |
860 { | 860 { |
861 ++s_destructorCalls; | 861 ++s_destructorCalls; |
862 } | 862 } |
863 | 863 |
864 void trace(Visitor*) { } | 864 void trace(Visitor*) { } |
865 | 865 |
866 static int s_destructorCalls; | 866 static int s_destructorCalls; |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1260 private: | 1260 private: |
1261 OwnPtrWillBeMember<SubData> m_data; | 1261 OwnPtrWillBeMember<SubData> m_data; |
1262 }; | 1262 }; |
1263 | 1263 |
1264 int SubClass::s_aliveCount = 0; | 1264 int SubClass::s_aliveCount = 0; |
1265 | 1265 |
1266 class TransitionRefCounted : public RefCountedWillBeRefCountedGarbageCollected<T
ransitionRefCounted> { | 1266 class TransitionRefCounted : public RefCountedWillBeRefCountedGarbageCollected<T
ransitionRefCounted> { |
1267 public: | 1267 public: |
1268 static PassRefPtrWillBeRawPtr<TransitionRefCounted> create() | 1268 static PassRefPtrWillBeRawPtr<TransitionRefCounted> create() |
1269 { | 1269 { |
1270 return adoptRefWillBeRefCountedGarbageCollected(new TransitionRefCounted
()); | 1270 return adoptRefWillBeNoop(new TransitionRefCounted()); |
1271 } | 1271 } |
1272 | 1272 |
1273 ~TransitionRefCounted() | 1273 ~TransitionRefCounted() |
1274 { | 1274 { |
1275 --s_aliveCount; | 1275 --s_aliveCount; |
1276 } | 1276 } |
1277 | 1277 |
1278 void trace(Visitor* visitor) { } | 1278 void trace(Visitor* visitor) { } |
1279 | 1279 |
1280 static int s_aliveCount; | 1280 static int s_aliveCount; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1434 | 1434 |
1435 void trace(Visitor*) { } | 1435 void trace(Visitor*) { } |
1436 | 1436 |
1437 private: | 1437 private: |
1438 Persistent<IntWrapper>* m_wrapper; | 1438 Persistent<IntWrapper>* m_wrapper; |
1439 }; | 1439 }; |
1440 | 1440 |
1441 TEST(HeapTest, Transition) | 1441 TEST(HeapTest, Transition) |
1442 { | 1442 { |
1443 { | 1443 { |
1444 RefPtr<TransitionRefCounted> refCounted = TransitionRefCounted::create()
; | 1444 RefPtrWillBePersistent<TransitionRefCounted> refCounted = TransitionRefC
ounted::create(); |
1445 EXPECT_EQ(1, TransitionRefCounted::s_aliveCount); | 1445 EXPECT_EQ(1, TransitionRefCounted::s_aliveCount); |
1446 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 1446 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
1447 EXPECT_EQ(1, TransitionRefCounted::s_aliveCount); | 1447 EXPECT_EQ(1, TransitionRefCounted::s_aliveCount); |
1448 } | 1448 } |
1449 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 1449 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
1450 EXPECT_EQ(0, TransitionRefCounted::s_aliveCount); | 1450 EXPECT_EQ(0, TransitionRefCounted::s_aliveCount); |
1451 | 1451 |
1452 RefPtrWillBePersistent<PointsBack> pointsBack1 = PointsBack::create(); | 1452 RefPtrWillBePersistent<PointsBack> pointsBack1 = PointsBack::create(); |
1453 RefPtrWillBePersistent<PointsBack> pointsBack2 = PointsBack::create(); | 1453 RefPtrWillBePersistent<PointsBack> pointsBack2 = PointsBack::create(); |
1454 RefPtrWillBePersistent<SuperClass> superClass = SuperClass::create(pointsBac
k1); | 1454 RefPtrWillBePersistent<SuperClass> superClass = SuperClass::create(pointsBac
k1); |
(...skipping 1280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2735 struct ThingWithDestructorTraits : public HashTraits<ThingWithDestructor> { | 2735 struct ThingWithDestructorTraits : public HashTraits<ThingWithDestructor> { |
2736 static const bool needsDestruction = true; | 2736 static const bool needsDestruction = true; |
2737 }; | 2737 }; |
2738 | 2738 |
2739 static void heapMapDestructorHelper(bool clearMaps) | 2739 static void heapMapDestructorHelper(bool clearMaps) |
2740 { | 2740 { |
2741 HeapStats initialHeapStats; | 2741 HeapStats initialHeapStats; |
2742 clearOutOldGarbage(&initialHeapStats); | 2742 clearOutOldGarbage(&initialHeapStats); |
2743 ThingWithDestructor::s_liveThingsWithDestructor = 0; | 2743 ThingWithDestructor::s_liveThingsWithDestructor = 0; |
2744 | 2744 |
2745 typedef HeapHashMap<WeakMember<IntWrapper>, RefPtr<RefCountedAndGarbageColle
cted> > RefMap; | 2745 typedef HeapHashMap<WeakMember<IntWrapper>, Member<RefCountedAndGarbageColle
cted> > RefMap; |
2746 | 2746 |
2747 typedef HeapHashMap< | 2747 typedef HeapHashMap< |
2748 WeakMember<IntWrapper>, | 2748 WeakMember<IntWrapper>, |
2749 ThingWithDestructor, | 2749 ThingWithDestructor, |
2750 DefaultHash<WeakMember<IntWrapper> >::Hash, | 2750 DefaultHash<WeakMember<IntWrapper> >::Hash, |
2751 HashTraits<WeakMember<IntWrapper> >, | 2751 HashTraits<WeakMember<IntWrapper> >, |
2752 ThingWithDestructorTraits> Map; | 2752 ThingWithDestructorTraits> Map; |
2753 | 2753 |
2754 Persistent<Map> map(new Map()); | 2754 Persistent<Map> map(new Map()); |
2755 Persistent<RefMap> refMap(new RefMap()); | 2755 Persistent<RefMap> refMap(new RefMap()); |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3162 } | 3162 } |
3163 | 3163 |
3164 TEST(HeapTest, RefCountedGarbageCollected) | 3164 TEST(HeapTest, RefCountedGarbageCollected) |
3165 { | 3165 { |
3166 RefCountedAndGarbageCollected::s_destructorCalls = 0; | 3166 RefCountedAndGarbageCollected::s_destructorCalls = 0; |
3167 { | 3167 { |
3168 RefPtr<RefCountedAndGarbageCollected> refPtr3; | 3168 RefPtr<RefCountedAndGarbageCollected> refPtr3; |
3169 { | 3169 { |
3170 Persistent<RefCountedAndGarbageCollected> persistent; | 3170 Persistent<RefCountedAndGarbageCollected> persistent; |
3171 { | 3171 { |
3172 RefPtr<RefCountedAndGarbageCollected> refPtr1 = RefCountedAndGar
bageCollected::create(); | 3172 Persistent<RefCountedAndGarbageCollected> refPtr1 = RefCountedAn
dGarbageCollected::create(); |
3173 RefPtr<RefCountedAndGarbageCollected> refPtr2 = RefCountedAndGar
bageCollected::create(); | 3173 Persistent<RefCountedAndGarbageCollected> refPtr2 = RefCountedAn
dGarbageCollected::create(); |
3174 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 3174 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
3175 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); | 3175 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); |
3176 persistent = refPtr1.get(); | 3176 persistent = refPtr1.get(); |
3177 } | 3177 } |
3178 // Reference count is zero for both objects but one of | 3178 // Reference count is zero for both objects but one of |
3179 // them is kept alive by a persistent handle. | 3179 // them is kept alive by a persistent handle. |
3180 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 3180 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
3181 EXPECT_EQ(1, RefCountedAndGarbageCollected::s_destructorCalls); | 3181 EXPECT_EQ(1, RefCountedAndGarbageCollected::s_destructorCalls); |
3182 refPtr3 = persistent.get(); | 3182 refPtr3 = persistent.get(); |
3183 } | 3183 } |
3184 // The persistent handle is gone but the ref count has been | 3184 // The persistent handle is gone but the ref count has been |
3185 // increased to 1. | 3185 // increased to 1. |
3186 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 3186 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
3187 EXPECT_EQ(1, RefCountedAndGarbageCollected::s_destructorCalls); | 3187 EXPECT_EQ(1, RefCountedAndGarbageCollected::s_destructorCalls); |
3188 } | 3188 } |
3189 // Both persistent handle is gone and ref count is zero so the | 3189 // Both persistent handle is gone and ref count is zero so the |
3190 // object can be collected. | 3190 // object can be collected. |
3191 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 3191 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
3192 EXPECT_EQ(2, RefCountedAndGarbageCollected::s_destructorCalls); | 3192 EXPECT_EQ(2, RefCountedAndGarbageCollected::s_destructorCalls); |
3193 } | 3193 } |
3194 | 3194 |
3195 TEST(HeapTest, RefCountedGarbageCollectedWithStackPointers) | 3195 TEST(HeapTest, RefCountedGarbageCollectedWithStackPointers) |
3196 { | 3196 { |
3197 RefCountedAndGarbageCollected::s_destructorCalls = 0; | 3197 RefCountedAndGarbageCollected::s_destructorCalls = 0; |
3198 RefCountedAndGarbageCollected2::s_destructorCalls = 0; | 3198 RefCountedAndGarbageCollected2::s_destructorCalls = 0; |
3199 { | 3199 { |
3200 RefCountedAndGarbageCollected* pointer1 = 0; | 3200 RefCountedAndGarbageCollected* pointer1 = 0; |
3201 RefCountedAndGarbageCollected2* pointer2 = 0; | 3201 RefCountedAndGarbageCollected2* pointer2 = 0; |
3202 { | 3202 { |
3203 RefPtr<RefCountedAndGarbageCollected> object1 = RefCountedAndGarbage
Collected::create(); | 3203 Persistent<RefCountedAndGarbageCollected> object1 = RefCountedAndGar
bageCollected::create(); |
3204 RefPtr<RefCountedAndGarbageCollected2> object2 = RefCountedAndGarbag
eCollected2::create(); | 3204 Persistent<RefCountedAndGarbageCollected2> object2 = RefCountedAndGa
rbageCollected2::create(); |
3205 pointer1 = object1.get(); | 3205 pointer1 = object1.get(); |
3206 pointer2 = object2.get(); | 3206 pointer2 = object2.get(); |
3207 void* objects[2] = { object1.get(), object2.get() }; | 3207 void* objects[2] = { object1.get(), object2.get() }; |
3208 RefCountedGarbageCollectedVisitor visitor(2, objects); | 3208 RefCountedGarbageCollectedVisitor visitor(2, objects); |
3209 ThreadState::current()->visitPersistents(&visitor); | 3209 ThreadState::current()->visitPersistents(&visitor); |
3210 EXPECT_TRUE(visitor.validate()); | 3210 EXPECT_TRUE(visitor.validate()); |
3211 | 3211 |
3212 Heap::collectGarbage(ThreadState::HeapPointersOnStack); | 3212 Heap::collectGarbage(ThreadState::HeapPointersOnStack); |
3213 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); | 3213 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); |
3214 EXPECT_EQ(0, RefCountedAndGarbageCollected2::s_destructorCalls); | 3214 EXPECT_EQ(0, RefCountedAndGarbageCollected2::s_destructorCalls); |
3215 } | 3215 } |
3216 Heap::collectGarbage(ThreadState::HeapPointersOnStack); | 3216 Heap::collectGarbage(ThreadState::HeapPointersOnStack); |
3217 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); | 3217 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); |
3218 EXPECT_EQ(0, RefCountedAndGarbageCollected2::s_destructorCalls); | 3218 EXPECT_EQ(0, RefCountedAndGarbageCollected2::s_destructorCalls); |
3219 | 3219 |
3220 // At this point, the reference counts of object1 and object2 are 0. | 3220 // At this point, the reference counts of object1 and object2 are 0. |
3221 // Only pointer1 and pointer2 keep references to object1 and object2. | 3221 // Only pointer1 and pointer2 keep references to object1 and object2. |
3222 void* objects[] = { 0 }; | 3222 void* objects[] = { 0 }; |
3223 RefCountedGarbageCollectedVisitor visitor(0, objects); | 3223 RefCountedGarbageCollectedVisitor visitor(0, objects); |
3224 ThreadState::current()->visitPersistents(&visitor); | 3224 ThreadState::current()->visitPersistents(&visitor); |
3225 EXPECT_TRUE(visitor.validate()); | 3225 EXPECT_TRUE(visitor.validate()); |
3226 | 3226 |
3227 { | 3227 { |
3228 RefPtr<RefCountedAndGarbageCollected> object1(pointer1); | 3228 Persistent<RefCountedAndGarbageCollected> object1(pointer1); |
3229 RefPtr<RefCountedAndGarbageCollected2> object2(pointer2); | 3229 Persistent<RefCountedAndGarbageCollected2> object2(pointer2); |
3230 void* objects[2] = { object1.get(), object2.get() }; | 3230 void* objects[2] = { object1.get(), object2.get() }; |
3231 RefCountedGarbageCollectedVisitor visitor(2, objects); | 3231 RefCountedGarbageCollectedVisitor visitor(2, objects); |
3232 ThreadState::current()->visitPersistents(&visitor); | 3232 ThreadState::current()->visitPersistents(&visitor); |
3233 EXPECT_TRUE(visitor.validate()); | 3233 EXPECT_TRUE(visitor.validate()); |
3234 | 3234 |
3235 Heap::collectGarbage(ThreadState::HeapPointersOnStack); | 3235 Heap::collectGarbage(ThreadState::HeapPointersOnStack); |
3236 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); | 3236 EXPECT_EQ(0, RefCountedAndGarbageCollected::s_destructorCalls); |
3237 EXPECT_EQ(0, RefCountedAndGarbageCollected2::s_destructorCalls); | 3237 EXPECT_EQ(0, RefCountedAndGarbageCollected2::s_destructorCalls); |
3238 } | 3238 } |
3239 | 3239 |
(...skipping 2157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5397 TEST(HeapTest, NonNodeAllocatingNodeInDestructor) | 5397 TEST(HeapTest, NonNodeAllocatingNodeInDestructor) |
5398 { | 5398 { |
5399 new NonNodeAllocatingNodeInDestructor(); | 5399 new NonNodeAllocatingNodeInDestructor(); |
5400 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 5400 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
5401 EXPECT_EQ(10, (*NonNodeAllocatingNodeInDestructor::s_node)->value()); | 5401 EXPECT_EQ(10, (*NonNodeAllocatingNodeInDestructor::s_node)->value()); |
5402 delete NonNodeAllocatingNodeInDestructor::s_node; | 5402 delete NonNodeAllocatingNodeInDestructor::s_node; |
5403 NonNodeAllocatingNodeInDestructor::s_node = 0; | 5403 NonNodeAllocatingNodeInDestructor::s_node = 0; |
5404 } | 5404 } |
5405 | 5405 |
5406 } // namespace blink | 5406 } // namespace blink |
OLD | NEW |