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 914 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 o->m_data->willFinalize(); | 925 o->m_data->willFinalize(); |
926 o->m_data = nullptr; | 926 o->m_data = nullptr; |
927 o->m_didCallWillFinalize = true; | 927 o->m_didCallWillFinalize = true; |
928 } | 928 } |
929 } | 929 } |
930 | 930 |
931 WeakMember<T> m_data; | 931 WeakMember<T> m_data; |
932 bool m_didCallWillFinalize; | 932 bool m_didCallWillFinalize; |
933 }; | 933 }; |
934 | 934 |
| 935 class FinalizationObserverWithHashMap { |
| 936 public: |
| 937 typedef HeapHashMap<WeakMember<Observable>, OwnPtr<FinalizationObserverWithH
ashMap> > ObserverMap; |
| 938 |
| 939 explicit FinalizationObserverWithHashMap(Observable& target) : m_target(targ
et) { } |
| 940 ~FinalizationObserverWithHashMap() |
| 941 { |
| 942 m_target.willFinalize(); |
| 943 s_didCallWillFinalize = true; |
| 944 } |
| 945 |
| 946 static ObserverMap& observe(Observable& target) |
| 947 { |
| 948 ObserverMap& map = observers(); |
| 949 ObserverMap::AddResult result = map.add(&target, nullptr); |
| 950 if (result.isNewEntry) |
| 951 result.storedValue->value = adoptPtr(new FinalizationObserverWithHas
hMap(target)); |
| 952 else |
| 953 ASSERT(result.storedValue->value); |
| 954 return map; |
| 955 } |
| 956 |
| 957 static bool s_didCallWillFinalize; |
| 958 |
| 959 private: |
| 960 static ObserverMap& observers() |
| 961 { |
| 962 DEFINE_STATIC_LOCAL(Persistent<ObserverMap>, observerMap, ()); |
| 963 if (!observerMap) |
| 964 observerMap = new ObserverMap(); |
| 965 return *observerMap; |
| 966 } |
| 967 |
| 968 Observable& m_target; |
| 969 }; |
| 970 |
| 971 bool FinalizationObserverWithHashMap::s_didCallWillFinalize = false; |
| 972 |
935 class SuperClass; | 973 class SuperClass; |
936 | 974 |
937 class PointsBack : public RefCountedWillBeGarbageCollectedFinalized<PointsBack>
{ | 975 class PointsBack : public RefCountedWillBeGarbageCollectedFinalized<PointsBack>
{ |
938 public: | 976 public: |
939 static PassRefPtrWillBeRawPtr<PointsBack> create() | 977 static PassRefPtrWillBeRawPtr<PointsBack> create() |
940 { | 978 { |
941 return adoptRefWillBeNoop(new PointsBack()); | 979 return adoptRefWillBeNoop(new PointsBack()); |
942 } | 980 } |
943 | 981 |
944 ~PointsBack() | 982 ~PointsBack() |
(...skipping 1679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2624 { | 2662 { |
2625 Observable* foo = Observable::create(Bar::create()); | 2663 Observable* foo = Observable::create(Bar::create()); |
2626 // |o| observes |foo|. | 2664 // |o| observes |foo|. |
2627 o = FinalizationObserver<Observable>::create(foo); | 2665 o = FinalizationObserver<Observable>::create(foo); |
2628 } | 2666 } |
2629 // FinalizationObserver doesn't have a strong reference to |foo|. So |foo| | 2667 // FinalizationObserver doesn't have a strong reference to |foo|. So |foo| |
2630 // and its member will be collected. | 2668 // and its member will be collected. |
2631 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 2669 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
2632 EXPECT_EQ(0u, Bar::s_live); | 2670 EXPECT_EQ(0u, Bar::s_live); |
2633 EXPECT_TRUE(o->didCallWillFinalize()); | 2671 EXPECT_TRUE(o->didCallWillFinalize()); |
| 2672 |
| 2673 FinalizationObserverWithHashMap::s_didCallWillFinalize = false; |
| 2674 Observable* foo = Observable::create(Bar::create()); |
| 2675 FinalizationObserverWithHashMap::ObserverMap& map = FinalizationObserverWith
HashMap::observe(*foo); |
| 2676 EXPECT_EQ(1u, map.size()); |
| 2677 foo = 0; |
| 2678 // FinalizationObserverWithHashMap doesn't have a strong reference to |
| 2679 // |foo|. So |foo| and its member will be collected. |
| 2680 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
| 2681 EXPECT_EQ(0u, Bar::s_live); |
| 2682 EXPECT_EQ(0u, map.size()); |
| 2683 EXPECT_TRUE(FinalizationObserverWithHashMap::s_didCallWillFinalize); |
2634 } | 2684 } |
2635 | 2685 |
2636 TEST(HeapTest, Comparisons) | 2686 TEST(HeapTest, Comparisons) |
2637 { | 2687 { |
2638 Persistent<Bar> barPersistent = Bar::create(); | 2688 Persistent<Bar> barPersistent = Bar::create(); |
2639 Persistent<Foo> fooPersistent = Foo::create(barPersistent); | 2689 Persistent<Foo> fooPersistent = Foo::create(barPersistent); |
2640 EXPECT_TRUE(barPersistent != fooPersistent); | 2690 EXPECT_TRUE(barPersistent != fooPersistent); |
2641 barPersistent = fooPersistent; | 2691 barPersistent = fooPersistent; |
2642 EXPECT_TRUE(barPersistent == fooPersistent); | 2692 EXPECT_TRUE(barPersistent == fooPersistent); |
2643 } | 2693 } |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3006 // swept away and zapped later in the same sweeping phase. | 3056 // swept away and zapped later in the same sweeping phase. |
3007 EXPECT_EQ(42, wrapper->value()); | 3057 EXPECT_EQ(42, wrapper->value()); |
3008 | 3058 |
3009 wrapper.clear(); | 3059 wrapper.clear(); |
3010 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); | 3060 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack); |
3011 EXPECT_EQ(10, IntWrapper::s_destructorCalls); | 3061 EXPECT_EQ(10, IntWrapper::s_destructorCalls); |
3012 EXPECT_EQ(512, OneKiloByteObject::s_destructorCalls); | 3062 EXPECT_EQ(512, OneKiloByteObject::s_destructorCalls); |
3013 } | 3063 } |
3014 | 3064 |
3015 } // WebCore namespace | 3065 } // WebCore namespace |
OLD | NEW |