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 988 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
999 , m_strongBar(strongBar) | 999 , m_strongBar(strongBar) |
1000 , m_weakBar(weakBar) | 1000 , m_weakBar(weakBar) |
1001 { | 1001 { |
1002 } | 1002 } |
1003 | 1003 |
1004 Member<Bar> m_strongBar; | 1004 Member<Bar> m_strongBar; |
1005 WeakMember<Bar> m_weakBar; | 1005 WeakMember<Bar> m_weakBar; |
1006 }; | 1006 }; |
1007 | 1007 |
1008 class Observable : public GarbageCollectedFinalized<Observable> { | 1008 class Observable : public GarbageCollectedFinalized<Observable> { |
| 1009 USING_PRE_FINALIZER(Observable, willFinalize); |
1009 public: | 1010 public: |
1010 static Observable* create(Bar* bar) { return new Observable(bar); } | 1011 static Observable* create(Bar* bar) { return new Observable(bar); } |
1011 ~Observable() { m_wasDestructed = true; } | 1012 ~Observable() { m_wasDestructed = true; } |
1012 void trace(Visitor* visitor) { visitor->trace(m_bar); } | 1013 void trace(Visitor* visitor) { visitor->trace(m_bar); } |
1013 | 1014 |
1014 // willFinalize is called by FinalizationObserver. willFinalize can touch | 1015 // willFinalize is called by FinalizationObserver. willFinalize can touch |
1015 // other on-heap objects. | 1016 // other on-heap objects. |
1016 void willFinalize() | 1017 void willFinalize() |
1017 { | 1018 { |
1018 EXPECT_FALSE(m_wasDestructed); | 1019 EXPECT_FALSE(m_wasDestructed); |
1019 EXPECT_FALSE(m_bar->hasBeenFinalized()); | 1020 EXPECT_FALSE(m_bar->hasBeenFinalized()); |
| 1021 s_willFinalizeWasCalled = true; |
1020 } | 1022 } |
| 1023 static bool s_willFinalizeWasCalled; |
1021 | 1024 |
1022 private: | 1025 private: |
1023 explicit Observable(Bar* bar) | 1026 explicit Observable(Bar* bar) |
1024 : m_bar(bar) | 1027 : m_bar(bar) |
1025 , m_wasDestructed(false) | 1028 , m_wasDestructed(false) |
1026 { | 1029 { |
1027 } | 1030 } |
1028 | 1031 |
1029 Member<Bar> m_bar; | 1032 Member<Bar> m_bar; |
1030 bool m_wasDestructed; | 1033 bool m_wasDestructed; |
1031 }; | 1034 }; |
1032 | 1035 |
| 1036 bool Observable::s_willFinalizeWasCalled = false; |
| 1037 |
| 1038 class ObservableWithPreFinalizer : public GarbageCollected<ObservableWithPreFina
lizer> { |
| 1039 USING_PRE_FINALIZER(ObservableWithPreFinalizer, dispose); |
| 1040 public: |
| 1041 static ObservableWithPreFinalizer* create() { return new ObservableWithPreFi
nalizer(); } |
| 1042 ~ObservableWithPreFinalizer() { m_wasDestructed = true; } |
| 1043 void trace(Visitor*) { } |
| 1044 void dispose() |
| 1045 { |
| 1046 ThreadState::current()->unregisterPreFinalizer(*this); |
| 1047 EXPECT_FALSE(m_wasDestructed); |
| 1048 s_disposeWasCalled = true; |
| 1049 } |
| 1050 static bool s_disposeWasCalled; |
| 1051 |
| 1052 private: |
| 1053 explicit ObservableWithPreFinalizer() |
| 1054 : m_wasDestructed(false) |
| 1055 { |
| 1056 ThreadState::current()->registerPreFinalizer(*this); |
| 1057 } |
| 1058 |
| 1059 bool m_wasDestructed; |
| 1060 }; |
| 1061 |
| 1062 bool ObservableWithPreFinalizer::s_disposeWasCalled = false; |
| 1063 |
1033 template <typename T> class FinalizationObserver : public GarbageCollected<Final
izationObserver<T> > { | 1064 template <typename T> class FinalizationObserver : public GarbageCollected<Final
izationObserver<T> > { |
1034 public: | 1065 public: |
1035 static FinalizationObserver* create(T* data) { return new FinalizationObserv
er(data); } | 1066 static FinalizationObserver* create(T* data) { return new FinalizationObserv
er(data); } |
1036 bool didCallWillFinalize() const { return m_didCallWillFinalize; } | 1067 bool didCallWillFinalize() const { return m_didCallWillFinalize; } |
1037 | 1068 |
1038 void trace(Visitor* visitor) | 1069 void trace(Visitor* visitor) |
1039 { | 1070 { |
1040 visitor->registerWeakMembers(this, zapWeakMembers); | 1071 visitor->registerWeakMembers(this, zapWeakMembers); |
1041 } | 1072 } |
1042 | 1073 |
(...skipping 2232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3275 EXPECT_EQ(1u, map.size()); | 3306 EXPECT_EQ(1u, map.size()); |
3276 foo = 0; | 3307 foo = 0; |
3277 // FinalizationObserverWithHashMap doesn't have a strong reference to | 3308 // FinalizationObserverWithHashMap doesn't have a strong reference to |
3278 // |foo|. So |foo| and its member will be collected. | 3309 // |foo|. So |foo| and its member will be collected. |
3279 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 3310 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
3280 EXPECT_EQ(0u, Bar::s_live); | 3311 EXPECT_EQ(0u, Bar::s_live); |
3281 EXPECT_EQ(0u, map.size()); | 3312 EXPECT_EQ(0u, map.size()); |
3282 EXPECT_TRUE(FinalizationObserverWithHashMap::s_didCallWillFinalize); | 3313 EXPECT_TRUE(FinalizationObserverWithHashMap::s_didCallWillFinalize); |
3283 } | 3314 } |
3284 | 3315 |
| 3316 TEST(HeapTest, PreFinalizer) |
| 3317 { |
| 3318 Observable::s_willFinalizeWasCalled = false; |
| 3319 { |
| 3320 Observable* foo = Observable::create(Bar::create()); |
| 3321 ThreadState::current()->registerPreFinalizer(*foo); |
| 3322 } |
| 3323 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 3324 EXPECT_TRUE(Observable::s_willFinalizeWasCalled); |
| 3325 } |
| 3326 |
| 3327 TEST(HeapTest, PreFinalizerIsNotCalledIfUnregistered) |
| 3328 { |
| 3329 Observable::s_willFinalizeWasCalled = false; |
| 3330 { |
| 3331 Observable* foo = Observable::create(Bar::create()); |
| 3332 ThreadState::current()->registerPreFinalizer(*foo); |
| 3333 ThreadState::current()->unregisterPreFinalizer(*foo); |
| 3334 } |
| 3335 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 3336 EXPECT_FALSE(Observable::s_willFinalizeWasCalled); |
| 3337 } |
| 3338 |
| 3339 TEST(HeapTest, PreFinalizerUnregistersItself) |
| 3340 { |
| 3341 ObservableWithPreFinalizer::s_disposeWasCalled = false; |
| 3342 ObservableWithPreFinalizer::create(); |
| 3343 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 3344 EXPECT_TRUE(ObservableWithPreFinalizer::s_disposeWasCalled); |
| 3345 // Don't crash, and assertions don't fail. |
| 3346 } |
| 3347 |
3285 TEST(HeapTest, Comparisons) | 3348 TEST(HeapTest, Comparisons) |
3286 { | 3349 { |
3287 Persistent<Bar> barPersistent = Bar::create(); | 3350 Persistent<Bar> barPersistent = Bar::create(); |
3288 Persistent<Foo> fooPersistent = Foo::create(barPersistent); | 3351 Persistent<Foo> fooPersistent = Foo::create(barPersistent); |
3289 EXPECT_TRUE(barPersistent != fooPersistent); | 3352 EXPECT_TRUE(barPersistent != fooPersistent); |
3290 barPersistent = fooPersistent; | 3353 barPersistent = fooPersistent; |
3291 EXPECT_TRUE(barPersistent == fooPersistent); | 3354 EXPECT_TRUE(barPersistent == fooPersistent); |
3292 } | 3355 } |
3293 | 3356 |
3294 TEST(HeapTest, CheckAndMarkPointer) | 3357 TEST(HeapTest, CheckAndMarkPointer) |
(...skipping 2037 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5332 TEST(HeapTest, NonNodeAllocatingNodeInDestructor) | 5395 TEST(HeapTest, NonNodeAllocatingNodeInDestructor) |
5333 { | 5396 { |
5334 new NonNodeAllocatingNodeInDestructor(); | 5397 new NonNodeAllocatingNodeInDestructor(); |
5335 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 5398 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
5336 EXPECT_EQ(10, (*NonNodeAllocatingNodeInDestructor::s_node)->value()); | 5399 EXPECT_EQ(10, (*NonNodeAllocatingNodeInDestructor::s_node)->value()); |
5337 delete NonNodeAllocatingNodeInDestructor::s_node; | 5400 delete NonNodeAllocatingNodeInDestructor::s_node; |
5338 NonNodeAllocatingNodeInDestructor::s_node = 0; | 5401 NonNodeAllocatingNodeInDestructor::s_node = 0; |
5339 } | 5402 } |
5340 | 5403 |
5341 } // namespace blink | 5404 } // namespace blink |
OLD | NEW |