| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GarbageCollected_h | 5 #ifndef GarbageCollected_h |
| 6 #define GarbageCollected_h | 6 #define GarbageCollected_h |
| 7 | 7 |
| 8 #include "platform/heap/ThreadState.h" | 8 #include "platform/heap/ThreadState.h" |
| 9 #include "wtf/Allocator.h" | 9 #include "wtf/Allocator.h" |
| 10 #include "wtf/Assertions.h" | 10 #include "wtf/Assertions.h" |
| 11 #include "wtf/ListHashSet.h" | |
| 12 #include "wtf/TypeTraits.h" | 11 #include "wtf/TypeTraits.h" |
| 13 | 12 |
| 14 namespace blink { | 13 namespace blink { |
| 15 | 14 |
| 16 template<typename T> class GarbageCollected; | 15 template<typename T> class GarbageCollected; |
| 17 template<typename T, typename U, typename V, typename W, typename X> class HeapH
ashMap; | |
| 18 template<typename T, typename U, typename V> class HeapHashSet; | |
| 19 template<typename T, typename U, typename V> class HeapLinkedHashSet; | |
| 20 template<typename T, size_t inlineCapacity, typename U> class HeapListHashSet; | |
| 21 template<typename T, size_t inlineCapacity> class HeapVector; | |
| 22 template<typename T, size_t inlineCapacity> class HeapDeque; | |
| 23 template<typename T, typename U, typename V> class HeapHashCountedSet; | |
| 24 template<typename T> class HeapTerminatedArray; | |
| 25 template<typename T, typename Traits> class HeapVectorBacking; | |
| 26 template<typename Table> class HeapHashTableBacking; | |
| 27 template<typename ValueArg, size_t inlineCapacity> class HeapListHashSetAllocato
r; | |
| 28 class InlinedGlobalMarkingVisitor; | 16 class InlinedGlobalMarkingVisitor; |
| 29 class WrapperVisitor; | 17 class WrapperVisitor; |
| 30 template<typename T> class Persistent; | |
| 31 | 18 |
| 32 // GC_PLUGIN_IGNORE is used to make the plugin ignore a particular class or | 19 // GC_PLUGIN_IGNORE is used to make the plugin ignore a particular class or |
| 33 // field when checking for proper usage. When using GC_PLUGIN_IGNORE | 20 // field when checking for proper usage. When using GC_PLUGIN_IGNORE |
| 34 // a bug-number should be provided as an argument where the bug describes | 21 // a bug-number should be provided as an argument where the bug describes |
| 35 // what needs to happen to remove the GC_PLUGIN_IGNORE again. | 22 // what needs to happen to remove the GC_PLUGIN_IGNORE again. |
| 36 #if COMPILER(CLANG) | 23 #if COMPILER(CLANG) |
| 37 #define GC_PLUGIN_IGNORE(bug) \ | 24 #define GC_PLUGIN_IGNORE(bug) \ |
| 38 __attribute__((annotate("blink_gc_plugin_ignore"))) | 25 __attribute__((annotate("blink_gc_plugin_ignore"))) |
| 39 #else | 26 #else |
| 40 #define GC_PLUGIN_IGNORE(bug) | 27 #define GC_PLUGIN_IGNORE(bug) |
| 41 #endif | 28 #endif |
| 42 | 29 |
| 43 // Template to determine if a class is a GarbageCollectedMixin by checking if it | 30 // Template to determine if a class is a GarbageCollectedMixin by checking if it |
| 44 // has IsGarbageCollectedMixinMarker | 31 // has IsGarbageCollectedMixinMarker |
| 45 template<typename T> | 32 template<typename T> |
| 46 struct IsGarbageCollectedMixin { | 33 struct IsGarbageCollectedMixin { |
| 47 private: | 34 private: |
| 48 typedef char YesType; | 35 typedef char YesType; |
| 49 struct NoType { | 36 struct NoType { |
| 50 char padding[8]; | 37 char padding[8]; |
| 51 }; | 38 }; |
| 52 | 39 |
| 53 template <typename U> static YesType checkMarker(typename U::IsGarbageCollec
tedMixinMarker*); | 40 template <typename U> static YesType checkMarker(typename U::IsGarbageCollec
tedMixinMarker*); |
| 54 template <typename U> static NoType checkMarker(...); | 41 template <typename U> static NoType checkMarker(...); |
| 55 | 42 |
| 56 public: | 43 public: |
| 57 static const bool value = sizeof(checkMarker<T>(nullptr)) == sizeof(YesType)
; | 44 static const bool value = sizeof(checkMarker<T>(nullptr)) == sizeof(YesType)
; |
| 58 }; | 45 }; |
| 59 | 46 |
| 60 template <typename T> | |
| 61 struct IsGarbageCollectedType { | |
| 62 using TrueType = char; | |
| 63 struct FalseType { | |
| 64 char dummy[2]; | |
| 65 }; | |
| 66 | |
| 67 using NonConstType = typename std::remove_const<T>::type; | |
| 68 using GarbageCollectedSubclass = WTF::IsSubclassOfTemplate<NonConstType, Gar
bageCollected>; | |
| 69 using GarbageCollectedMixinSubclass = IsGarbageCollectedMixin<NonConstType>; | |
| 70 using HeapHashSetSubclass = WTF::IsSubclassOfTemplate<NonConstType, HeapHash
Set>; | |
| 71 using HeapLinkedHashSetSubclass = WTF::IsSubclassOfTemplate<NonConstType, He
apLinkedHashSet>; | |
| 72 using HeapListHashSetSubclass = WTF::IsSubclassOfTemplateTypenameSizeTypenam
e<NonConstType, HeapListHashSet>; | |
| 73 using HeapHashMapSubclass = WTF::IsSubclassOfTemplate<NonConstType, HeapHash
Map>; | |
| 74 using HeapVectorSubclass = WTF::IsSubclassOfTemplateTypenameSize<NonConstTyp
e, HeapVector>; | |
| 75 using HeapDequeSubclass = WTF::IsSubclassOfTemplateTypenameSize<NonConstType
, HeapDeque>; | |
| 76 using HeapHashCountedSetSubclass = WTF::IsSubclassOfTemplate<NonConstType, H
eapHashCountedSet>; | |
| 77 using HeapTerminatedArraySubclass = WTF::IsSubclassOfTemplate<NonConstType,
HeapTerminatedArray>; | |
| 78 using HeapVectorBackingSubclass = WTF::IsSubclassOfTemplate<NonConstType, He
apVectorBacking>; | |
| 79 using HeapHashTableBackingSubclass = WTF::IsSubclassOfTemplate<NonConstType,
HeapHashTableBacking>; | |
| 80 | |
| 81 template<typename U, size_t inlineCapacity> static TrueType listHashSetNodeI
sHeapAllocated(WTF::ListHashSetNode<U, HeapListHashSetAllocator<U, inlineCapacit
y>>*); | |
| 82 static FalseType listHashSetNodeIsHeapAllocated(...); | |
| 83 static const bool isHeapAllocatedListHashSetNode = sizeof(TrueType) == sizeo
f(listHashSetNodeIsHeapAllocated(reinterpret_cast<NonConstType*>(0))); | |
| 84 | |
| 85 static_assert(sizeof(T), "T must be fully defined"); | |
| 86 | |
| 87 static const bool value = | |
| 88 GarbageCollectedSubclass::value | |
| 89 || GarbageCollectedMixinSubclass::value | |
| 90 || HeapHashSetSubclass::value | |
| 91 || HeapLinkedHashSetSubclass::value | |
| 92 || HeapListHashSetSubclass::value | |
| 93 || HeapHashMapSubclass::value | |
| 94 || HeapVectorSubclass::value | |
| 95 || HeapDequeSubclass::value | |
| 96 || HeapHashCountedSetSubclass::value | |
| 97 || HeapTerminatedArraySubclass::value | |
| 98 || HeapVectorBackingSubclass::value | |
| 99 || HeapHashTableBackingSubclass::value | |
| 100 || isHeapAllocatedListHashSetNode; | |
| 101 }; | |
| 102 | |
| 103 template <> | |
| 104 struct IsGarbageCollectedType<void> { | |
| 105 static const bool value = false; | |
| 106 }; | |
| 107 | |
| 108 // The GarbageCollectedMixin interface and helper macro | 47 // The GarbageCollectedMixin interface and helper macro |
| 109 // USING_GARBAGE_COLLECTED_MIXIN can be used to automatically define | 48 // USING_GARBAGE_COLLECTED_MIXIN can be used to automatically define |
| 110 // TraceTrait/ObjectAliveTrait on non-leftmost deriving classes | 49 // TraceTrait/ObjectAliveTrait on non-leftmost deriving classes |
| 111 // which need to be garbage collected. | 50 // which need to be garbage collected. |
| 112 // | 51 // |
| 113 // Consider the following case: | 52 // Consider the following case: |
| 114 // class B {}; | 53 // class B {}; |
| 115 // class A : public GarbageCollected, public B {}; | 54 // class A : public GarbageCollected, public B {}; |
| 116 // | 55 // |
| 117 // We can't correctly handle "Member<B> p = &a" as we can't compute addr of | 56 // We can't correctly handle "Member<B> p = &a" as we can't compute addr of |
| 118 // object header statically. This can be solved by using GarbageCollectedMixin: | 57 // object header statically. This can be solved by using GarbageCollectedMixin: |
| 119 // class B : public GarbageCollectedMixin {}; | 58 // class B : public GarbageCollectedMixin {}; |
| 120 // class A : public GarbageCollected, public B { | 59 // class A : public GarbageCollected, public B { |
| 121 // USING_GARBAGE_COLLECTED_MIXIN(A) | 60 // USING_GARBAGE_COLLECTED_MIXIN(A); |
| 122 // }; | 61 // }; |
| 123 // | 62 // |
| 124 // With the helper, as long as we are using Member<B>, TypeTrait<B> will | 63 // With the helper, as long as we are using Member<B>, TypeTrait<B> will |
| 125 // dispatch adjustAndMark dynamically to find collect addr of the object header. | 64 // dispatch adjustAndMark dynamically to find collect addr of the object header. |
| 126 // Note that this is only enabled for Member<B>. For Member<A> which we can | 65 // Note that this is only enabled for Member<B>. For Member<A> which we can |
| 127 // compute the object header addr statically, this dynamic dispatch is not used. | 66 // compute the object header addr statically, this dynamic dispatch is not used. |
| 67 // |
| 128 class PLATFORM_EXPORT GarbageCollectedMixin { | 68 class PLATFORM_EXPORT GarbageCollectedMixin { |
| 129 IS_GARBAGE_COLLECTED_TYPE(); | |
| 130 public: | 69 public: |
| 131 typedef int IsGarbageCollectedMixinMarker; | 70 typedef int IsGarbageCollectedMixinMarker; |
| 132 virtual void adjustAndMark(Visitor*) const = 0; | 71 virtual void adjustAndMark(Visitor*) const = 0; |
| 133 virtual void trace(Visitor*) { } | 72 virtual void trace(Visitor*) { } |
| 134 virtual void adjustAndMark(InlinedGlobalMarkingVisitor) const = 0; | 73 virtual void adjustAndMark(InlinedGlobalMarkingVisitor) const = 0; |
| 135 virtual void trace(InlinedGlobalMarkingVisitor); | 74 virtual void trace(InlinedGlobalMarkingVisitor); |
| 136 virtual void adjustAndMarkWrapper(const WrapperVisitor*) const = 0; | 75 virtual void adjustAndMarkWrapper(const WrapperVisitor*) const = 0; |
| 137 virtual bool isHeapObjectAlive() const = 0; | 76 virtual bool isHeapObjectAlive() const = 0; |
| 138 }; | 77 }; |
| 139 | 78 |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 260 |
| 322 template<typename U, size_t sz = sizeof(U)> static TrueType isSizeofKnown(U*
); | 261 template<typename U, size_t sz = sizeof(U)> static TrueType isSizeofKnown(U*
); |
| 323 static FalseType isSizeofKnown(...); | 262 static FalseType isSizeofKnown(...); |
| 324 static T& t; | 263 static T& t; |
| 325 public: | 264 public: |
| 326 static const bool value = sizeof(TrueType) == sizeof(isSizeofKnown(&t)); | 265 static const bool value = sizeof(TrueType) == sizeof(isSizeofKnown(&t)); |
| 327 }; | 266 }; |
| 328 | 267 |
| 329 } // namespace blink | 268 } // namespace blink |
| 330 | 269 |
| 331 namespace WTF { | |
| 332 | |
| 333 } // namespace WTF | |
| 334 | |
| 335 #endif | 270 #endif |
| OLD | NEW |