2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "platform/heap/Heap.h"
35 #include "platform/heap/ThreadState.h"
36 #include "platform/heap/Visitor.h"
37 #include "wtf/Functional.h"
38 #include "wtf/HashFunctions.h"
39 #include "wtf/Locker.h"
40 #include "wtf/RawPtr.h"
41 #include "wtf/RefCounted.h"
42 #include "wtf/TypeTraits.h"
46 template<typename T> class HeapTerminatedArray;
48 // Template to determine if a class is a GarbageCollectedMixin by checking if it
49 // has adjustAndMark and isAlive. We can't check directly if the class is a
50 // GarbageCollectedMixin because casting to it is potentially ambiguous.
52 struct IsGarbageCollectedMixin {
53 typedef char TrueType;
59 template<typename U> static TrueType hasAdjustAndMark(char[&U::adjustAndMark != 0]);
60 template<typename U> static TrueType hasIsHeapObjectAlive(char[&U::isHeapObjectAlive != 0]);
62 template<size_t> struct F;
63 template<typename U> static TrueType hasAdjustAndMark(F<sizeof(&U::adjustAndMark)>*);
64 template<typename U> static TrueType hasIsHeapObjectAlive(F<sizeof(&U::isHeapObjectAlive)>*);
66 template<typename U> static FalseType hasIsHeapObjectAlive(...);
67 template<typename U> static FalseType hasAdjustAndMark(...);
69 static bool const value = (sizeof(TrueType) == sizeof(hasAdjustAndMark<T>(0))) && (sizeof(TrueType) == sizeof(hasIsHeapObjectAlive<T>(0)));
73 struct IsGarbageCollectedType {
74 typedef char TrueType;
79 typedef typename WTF::RemoveConst<T>::Type NonConstType;
80 typedef WTF::IsSubclassOfTemplate<NonConstType, GarbageCollected> GarbageCollectedSubclass;
81 typedef IsGarbageCollectedMixin<NonConstType> GarbageCollectedMixinSubclass;
82 typedef WTF::IsSubclassOfTemplate<NonConstType, HeapHashSet> HeapHashSetSubclass;
83 typedef WTF::IsSubclassOfTemplate<NonConstType, HeapLinkedHashSet> HeapLinkedHashSetSubclass;
84 typedef WTF::IsSubclassOfTemplateTypenameSizeTypename<NonConstType, HeapListHashSet> HeapListHashSetSubclass;
85 typedef WTF::IsSubclassOfTemplate<NonConstType, HeapHashMap> HeapHashMapSubclass;
86 typedef WTF::IsSubclassOfTemplateTypenameSize<NonConstType, HeapVector> HeapVectorSubclass;
87 typedef WTF::IsSubclassOfTemplateTypenameSize<NonConstType, HeapDeque> HeapDequeSubclass;
88 typedef WTF::IsSubclassOfTemplate<NonConstType, HeapHashCountedSet> HeapHashCountedSetSubclass;
89 typedef WTF::IsSubclassOfTemplate<NonConstType, HeapTerminatedArray> HeapTerminatedArraySubclass;
91 template<typename U, size_t inlineCapacity> static TrueType listHashSetNodeIsHeapAllocated(WTF::ListHashSetNode<U, HeapListHashSetAllocator<U, inlineCapacity> >*);
92 static FalseType listHashSetNodeIsHeapAllocated(...);
93 static const bool isHeapAllocatedListHashSetNode = sizeof(TrueType) == sizeof(listHashSetNodeIsHeapAllocated(reinterpret_cast<NonConstType*>(0)));
95 static const bool value =
96 GarbageCollectedSubclass::value
97 || GarbageCollectedMixinSubclass::value
98 || HeapHashSetSubclass::value
99 || HeapLinkedHashSetSubclass::value
100 || HeapListHashSetSubclass::value
101 || HeapHashMapSubclass::value
102 || HeapVectorSubclass::value
103 || HeapDequeSubclass::value
104 || HeapHashCountedSetSubclass::value
105 || HeapTerminatedArraySubclass::value
106 || isHeapAllocatedListHashSetNode;
109 #define COMPILE_ASSERT_IS_GARBAGE_COLLECTED(T, ErrorMessage) \
110 COMPILE_ASSERT(IsGarbageCollectedType<T>::value, ErrorMessage)
112 template<typename T> class Member;
114 class PersistentNode {
116 explicit PersistentNode(TraceCallback trace)
121 bool isAlive() { return m_trace; }
123 virtual ~PersistentNode()
129 // Ideally the trace method should be virtual and automatically dispatch
130 // to the most specific implementation. However having a virtual method
131 // on PersistentNode leads to too eager template instantiation with MSVC
132 // which leads to include cycles.
133 // Instead we call the constructor with a TraceCallback which knows the
134 // type of the most specific child and calls trace directly. See
135 // TraceMethodDelegate in Visitor.h for how this is done.
136 void trace(Visitor* visitor)
138 m_trace(visitor, this);
142 TraceCallback m_trace;
145 PersistentNode* m_next;
146 PersistentNode* m_prev;
148 template<typename RootsAccessor, typename Owner> friend class PersistentBase;
149 friend class PersistentAnchor;
150 friend class ThreadState;
153 // RootsAccessor for Persistent that provides access to thread-local list
154 // of persistent handles. Can only be used to create handles that
155 // are constructed and destructed on the same thread.
156 template<ThreadAffinity Affinity>
157 class ThreadLocalPersistents {
159 static PersistentNode* roots() { return state()->roots(); }
161 // No locking required. Just check that we are at the right thread.
164 Lock() { state()->checkThread(); }
168 static ThreadState* state() { return ThreadStateFor<Affinity>::state(); }
171 // RootsAccessor for Persistent that provides synchronized access to global
172 // list of persistent handles. Can be used for persistent handles that are
173 // passed between threads.
174 class GlobalPersistents {
176 static PersistentNode* roots() { return ThreadState::globalRoots(); }
180 Lock() : m_locker(ThreadState::globalRootsMutex()) { }
182 MutexLocker m_locker;
186 // Base class for persistent handles. RootsAccessor specifies which list to
187 // link resulting handle into. Owner specifies the class containing trace
189 template<typename RootsAccessor, typename Owner>
190 class PersistentBase : public PersistentNode {
194 typename RootsAccessor::Lock lock;
195 ASSERT(m_roots == RootsAccessor::roots()); // Check that the thread is using the same roots list.
197 ASSERT(m_next->isAlive());
198 ASSERT(m_prev->isAlive());
199 m_next->m_prev = m_prev;
200 m_prev->m_next = m_next;
204 inline PersistentBase()
205 : PersistentNode(TraceMethodDelegate<Owner, &Owner::trace>::trampoline)
207 , m_roots(RootsAccessor::roots())
210 typename RootsAccessor::Lock lock;
211 m_prev = RootsAccessor::roots();
212 m_next = m_prev->m_next;
213 m_prev->m_next = this;
214 m_next->m_prev = this;
217 inline explicit PersistentBase(const PersistentBase& otherref)
218 : PersistentNode(otherref.m_trace)
220 , m_roots(RootsAccessor::roots())
223 // We don't support allocation of thread local Persistents while doing
224 // thread shutdown/cleanup.
225 ASSERT(!ThreadState::current()->isTerminating());
226 typename RootsAccessor::Lock lock;
227 ASSERT(otherref.m_roots == m_roots); // Handles must belong to the same list.
228 PersistentBase* other = const_cast<PersistentBase*>(&otherref);
230 m_next = other->m_next;
231 other->m_next = this;
232 m_next->m_prev = this;
235 inline PersistentBase& operator=(const PersistentBase& otherref) { return *this; }
239 PersistentNode* m_roots;
243 // A dummy Persistent handle that ensures the list of persistents is never null.
244 // This removes a test from a hot path.
245 class PersistentAnchor : public PersistentNode {
247 void trace(Visitor* visitor)
249 for (PersistentNode* current = m_next; current != this; current = current->m_next)
250 current->trace(visitor);
253 int numberOfPersistents()
255 int numberOfPersistents = 0;
256 for (PersistentNode* current = m_next; current != this; current = current->m_next)
257 ++numberOfPersistents;
258 return numberOfPersistents;
261 virtual ~PersistentAnchor()
263 // FIXME: oilpan: Ideally we should have no left-over persistents at this point. However currently there is a
264 // large number of objects leaked when we tear down the main thread. Since some of these might contain a
265 // persistent or e.g. be RefCountedGarbageCollected we cannot guarantee there are no remaining Persistents at
270 PersistentAnchor() : PersistentNode(TraceMethodDelegate<PersistentAnchor, &PersistentAnchor::trace>::trampoline)
276 friend class ThreadState;
280 // For global persistent handles we cannot check that the
281 // pointer is in the heap because that would involve
282 // inspecting the heap of running threads.
283 #define ASSERT_IS_VALID_PERSISTENT_POINTER(pointer) \
284 bool isGlobalPersistent = WTF::IsSubclass<RootsAccessor, GlobalPersistents>::value; \
285 ASSERT(!pointer || isGlobalPersistent || ThreadStateFor<ThreadingTrait<T>::Affinity>::state()->contains(pointer))
287 #define ASSERT_IS_VALID_PERSISTENT_POINTER(pointer)
291 class CrossThreadPersistent;
293 // Persistent handles are used to store pointers into the
294 // managed heap. As long as the Persistent handle is alive
295 // the GC will keep the object pointed to alive. Persistent
296 // handles can be stored in objects and they are not scoped.
297 // Persistent handles must not be used to contain pointers
298 // between objects that are in the managed heap. They are only
299 // meant to point to managed heap objects from variables/members
300 // outside the managed heap.
302 // A Persistent is always a GC root from the point of view of
303 // the garbage collector.
305 // We have to construct and destruct Persistent with default RootsAccessor in
307 template<typename T, typename RootsAccessor /* = ThreadLocalPersistents<ThreadingTrait<T>::Affinity > */ >
308 class Persistent : public PersistentBase<RootsAccessor, Persistent<T, RootsAccessor> > {
310 Persistent() : m_raw(0) { }
312 Persistent(std::nullptr_t) : m_raw(0) { }
314 Persistent(T* raw) : m_raw(raw)
316 ASSERT_IS_VALID_PERSISTENT_POINTER(m_raw);
320 explicit Persistent(T& raw) : m_raw(&raw)
322 ASSERT_IS_VALID_PERSISTENT_POINTER(m_raw);
326 Persistent(const Persistent& other) : m_raw(other) { recordBacktrace(); }
329 Persistent(const Persistent<U, RootsAccessor>& other) : m_raw(other) { recordBacktrace(); }
332 Persistent(const Member<U>& other) : m_raw(other) { recordBacktrace(); }
335 Persistent(const RawPtr<U>& other) : m_raw(other.get()) { recordBacktrace(); }
338 Persistent& operator=(U* other)
345 Persistent& operator=(std::nullptr_t)
351 void clear() { m_raw = 0; }
353 virtual ~Persistent()
361 return static_cast<U*>(m_raw);
364 void trace(Visitor* visitor)
366 COMPILE_ASSERT_IS_GARBAGE_COLLECTED(T, NonGarbageCollectedObjectInPersistent);
367 #if ENABLE(GC_PROFILE_MARKING)
368 visitor->setHostInfo(this, m_tracingName.isEmpty() ? "Persistent" : m_tracingName);
370 visitor->mark(m_raw);
375 RawPtr<T> result = m_raw;
380 T& operator*() const { return *m_raw; }
382 bool operator!() const { return !m_raw; }
384 operator T*() const { return m_raw; }
385 operator RawPtr<T>() const { return m_raw; }
387 T* operator->() const { return *this; }
389 Persistent& operator=(const Persistent& other)
397 Persistent& operator=(const Persistent<U, RootsAccessor>& other)
405 Persistent& operator=(const Member<U>& other)
413 Persistent& operator=(const RawPtr<U>& other)
420 T* get() const { return m_raw; }
423 #if ENABLE(GC_PROFILE_MARKING)
424 void recordBacktrace()
427 m_tracingName = Heap::createBacktraceString();
430 String m_tracingName;
432 inline void recordBacktrace() const { }
436 friend class CrossThreadPersistent<T>;
439 // Unlike Persistent, we can destruct a CrossThreadPersistent in a thread
440 // different from the construction thread.
442 class CrossThreadPersistent : public Persistent<T, GlobalPersistents> {
444 CrossThreadPersistent(T* raw) : Persistent<T, GlobalPersistents>(raw) { }
446 using Persistent<T, GlobalPersistents>::operator=;
449 // FIXME: derive affinity based on the collection.
450 template<typename Collection, ThreadAffinity Affinity = AnyThread>
451 class PersistentHeapCollectionBase
453 , public PersistentBase<ThreadLocalPersistents<Affinity>, PersistentHeapCollectionBase<Collection, Affinity> > {
454 // We overload the various new and delete operators with using the WTF DefaultAllocator to ensure persistent
455 // heap collections are always allocated off-heap. This allows persistent collections to be used in
456 // DEFINE_STATIC_LOCAL et. al.
457 WTF_USE_ALLOCATOR(PersistentHeapCollectionBase, WTF::DefaultAllocator);
459 PersistentHeapCollectionBase() { }
461 template<typename OtherCollection>
462 PersistentHeapCollectionBase(const OtherCollection& other) : Collection(other) { }
464 void trace(Visitor* visitor)
466 #if ENABLE(GC_PROFILE_MARKING)
467 visitor->setHostInfo(this, "PersistentHeapCollectionBase");
469 visitor->trace(*static_cast<Collection*>(this));
476 typename HashArg = typename DefaultHash<KeyArg>::Hash,
477 typename KeyTraitsArg = HashTraits<KeyArg>,
478 typename MappedTraitsArg = HashTraits<MappedArg> >
479 class PersistentHeapHashMap : public PersistentHeapCollectionBase<HeapHashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg> > { };
483 typename HashArg = typename DefaultHash<ValueArg>::Hash,
484 typename TraitsArg = HashTraits<ValueArg> >
485 class PersistentHeapHashSet : public PersistentHeapCollectionBase<HeapHashSet<ValueArg, HashArg, TraitsArg> > { };
489 typename HashArg = typename DefaultHash<ValueArg>::Hash,
490 typename TraitsArg = HashTraits<ValueArg> >
491 class PersistentHeapLinkedHashSet : public PersistentHeapCollectionBase<HeapLinkedHashSet<ValueArg, HashArg, TraitsArg> > { };
495 size_t inlineCapacity = 0,
496 typename HashArg = typename DefaultHash<ValueArg>::Hash>
497 class PersistentHeapListHashSet : public PersistentHeapCollectionBase<HeapListHashSet<ValueArg, inlineCapacity, HashArg> > { };
499 template<typename T, typename U, typename V>
500 class PersistentHeapHashCountedSet : public PersistentHeapCollectionBase<HeapHashCountedSet<T, U, V> > { };
502 template<typename T, size_t inlineCapacity = 0>
503 class PersistentHeapVector : public PersistentHeapCollectionBase<HeapVector<T, inlineCapacity> > {
505 PersistentHeapVector() { }
507 template<size_t otherCapacity>
508 PersistentHeapVector(const HeapVector<T, otherCapacity>& other)
509 : PersistentHeapCollectionBase<HeapVector<T, inlineCapacity> >(other)
514 template<typename T, size_t inlineCapacity = 0>
515 class PersistentHeapDeque : public PersistentHeapCollectionBase<HeapDeque<T, inlineCapacity> > {
517 PersistentHeapDeque() { }
519 template<size_t otherCapacity>
520 PersistentHeapDeque(const HeapDeque<T, otherCapacity>& other)
521 : PersistentHeapCollectionBase<HeapDeque<T, inlineCapacity> >(other)
526 // Members are used in classes to contain strong pointers to other oilpan heap
527 // allocated objects.
528 // All Member fields of a class must be traced in the class' trace method.
529 // During the mark phase of the GC all live objects are marked as live and
530 // all Member fields of a live object will be traced marked as live as well.
538 Member(std::nullptr_t) : m_raw(0)
542 Member(T* raw) : m_raw(raw)
546 explicit Member(T& raw) : m_raw(&raw)
551 Member(const RawPtr<U>& other) : m_raw(other.get())
555 Member(WTF::HashTableDeletedValueType) : m_raw(reinterpret_cast<T*>(-1))
559 bool isHashTableDeletedValue() const { return m_raw == reinterpret_cast<T*>(-1); }
562 Member(const Persistent<U>& other) : m_raw(other) { }
564 Member(const Member& other) : m_raw(other) { }
567 Member(const Member<U>& other) : m_raw(other) { }
579 return static_cast<U*>(m_raw);
582 bool operator!() const { return !m_raw; }
584 operator T*() const { return m_raw; }
586 T* operator->() const { return m_raw; }
587 T& operator*() const { return *m_raw; }
589 operator RawPtr<U>() const { return m_raw; }
592 Member& operator=(const Persistent<U>& other)
599 Member& operator=(const Member<U>& other)
606 Member& operator=(U* other)
613 Member& operator=(RawPtr<U> other)
619 Member& operator=(std::nullptr_t)
625 void swap(Member<T>& other) { std::swap(m_raw, other.m_raw); }
627 T* get() const { return m_raw; }
629 void clear() { m_raw = 0; }
633 void verifyTypeIsGarbageCollected() const
635 COMPILE_ASSERT_IS_GARBAGE_COLLECTED(T, NonGarbageCollectedObjectInMember);
640 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStrongly z, typename U, typename V> friend struct CollectionBackingTraceTrait;
641 friend class Visitor;
645 class TraceTrait<Member<T> > {
647 static void trace(Visitor* visitor, void* self)
649 TraceTrait<T>::mark(visitor, *static_cast<Member<T>*>(self));
653 // TraceTrait to allow compilation of trace method bodies when oilpan is disabled.
654 // This should never be called, but is needed to compile.
656 class TraceTrait<RefPtr<T> > {
658 static void trace(Visitor*, void*)
660 ASSERT_NOT_REACHED();
665 class TraceTrait<OwnPtr<T> > {
667 static void trace(Visitor* visitor, OwnPtr<T>* ptr)
669 ASSERT_NOT_REACHED();
673 template<typename T, bool needsTracing>
674 struct TraceIfEnabled;
677 struct TraceIfEnabled<T, false> {
678 static void trace(Visitor*, T*) { }
682 struct TraceIfEnabled<T, true> {
683 static void trace(Visitor* visitor, T* t)
689 template <typename T> struct RemoveHeapPointerWrapperTypes {
690 typedef typename WTF::RemoveTemplate<typename WTF::RemoveTemplate<typename WTF::RemoveTemplate<T, Member>::Type, WeakMember>::Type, RawPtr>::Type Type;
693 // FIXME: Oilpan: TraceIfNeeded should be implemented ala:
694 // NeedsTracing<T>::value || IsWeakMember<T>::value. It should not need to test
695 // raw pointer types. To remove these tests, we may need support for
696 // instantiating a template with a RawPtrOrMember'ish template.
698 struct TraceIfNeeded : public TraceIfEnabled<T, WTF::NeedsTracing<T>::value || blink::IsGarbageCollectedType<typename RemoveHeapPointerWrapperTypes<typename WTF::RemovePointer<T>::Type>::Type>::value> { };
700 // This trace trait for std::pair will null weak members if their referent is
701 // collected. If you have a collection that contain weakness it does not remove
702 // entries from the collection that contain nulled weak members.
703 template<typename T, typename U>
704 class TraceTrait<std::pair<T, U> > {
706 static const bool firstNeedsTracing = WTF::NeedsTracing<T>::value || WTF::IsWeak<T>::value;
707 static const bool secondNeedsTracing = WTF::NeedsTracing<U>::value || WTF::IsWeak<U>::value;
708 static void trace(Visitor* visitor, std::pair<T, U>* pair)
710 TraceIfEnabled<T, firstNeedsTracing>::trace(visitor, &pair->first);
711 TraceIfEnabled<U, secondNeedsTracing>::trace(visitor, &pair->second);
715 // WeakMember is similar to Member in that it is used to point to other oilpan
716 // heap allocated objects.
717 // However instead of creating a strong pointer to the object, the WeakMember creates
718 // a weak pointer, which does not keep the pointee alive. Hence if all pointers to
719 // to a heap allocated object are weak the object will be garbage collected. At the
720 // time of GC the weak pointers will automatically be set to null.
722 class WeakMember : public Member<T> {
724 WeakMember() : Member<T>() { }
726 WeakMember(std::nullptr_t) : Member<T>(nullptr) { }
728 WeakMember(T* raw) : Member<T>(raw) { }
730 WeakMember(WTF::HashTableDeletedValueType x) : Member<T>(x) { }
733 WeakMember(const Persistent<U>& other) : Member<T>(other) { }
736 WeakMember(const Member<U>& other) : Member<T>(other) { }
739 WeakMember& operator=(const Persistent<U>& other)
746 WeakMember& operator=(const Member<U>& other)
753 WeakMember& operator=(U* other)
760 WeakMember& operator=(const RawPtr<U>& other)
766 WeakMember& operator=(std::nullptr_t)
773 T** cell() const { return const_cast<T**>(&this->m_raw); }
775 friend class Visitor;
778 // Comparison operators between (Weak)Members and Persistents
779 template<typename T, typename U> inline bool operator==(const Member<T>& a, const Member<U>& b) { return a.get() == b.get(); }
780 template<typename T, typename U> inline bool operator!=(const Member<T>& a, const Member<U>& b) { return a.get() != b.get(); }
781 template<typename T, typename U> inline bool operator==(const Member<T>& a, const Persistent<U>& b) { return a.get() == b.get(); }
782 template<typename T, typename U> inline bool operator!=(const Member<T>& a, const Persistent<U>& b) { return a.get() != b.get(); }
783 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Member<U>& b) { return a.get() == b.get(); }
784 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Member<U>& b) { return a.get() != b.get(); }
785 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Persistent<U>& b) { return a.get() == b.get(); }
786 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Persistent<U>& b) { return a.get() != b.get(); }
788 // CPP-defined type names for the transition period where we want to
789 // support both reference counting and garbage collection based on a
790 // compile-time flag.
792 // C++11 template aliases were initially used (with clang only, not
793 // with GCC nor MSVC.) However, supporting both CPP defines and
794 // template aliases is problematic from outside a WebCore namespace
795 // when Oilpan is disabled: e.g.,
796 // blink::RefCountedWillBeGarbageCollected as a template alias would
797 // uniquely resolve from within any namespace, but if it is backed by
798 // a CPP #define, it would expand to blink::RefCounted, and not the
799 // required WTF::RefCounted.
801 // Having the CPP expansion instead be fully namespace qualified, and the
802 // transition type be unqualified, would dually not work for template
803 // aliases. So, slightly unfortunately, fall back/down to the lowest
804 // commmon denominator of using CPP macros only.
806 #define PassRefPtrWillBeRawPtr WTF::RawPtr
807 #define RefCountedWillBeGarbageCollected blink::GarbageCollected
808 #define RefCountedWillBeGarbageCollectedFinalized blink::GarbageCollectedFinalized
809 #define RefCountedWillBeRefCountedGarbageCollected blink::RefCountedGarbageCollected
810 #define RefCountedGarbageCollectedWillBeGarbageCollectedFinalized blink::GarbageCollectedFinalized
811 #define ThreadSafeRefCountedWillBeGarbageCollected blink::GarbageCollected
812 #define ThreadSafeRefCountedWillBeGarbageCollectedFinalized blink::GarbageCollectedFinalized
813 #define PersistentWillBeMember blink::Member
814 #define CrossThreadPersistentWillBeMember blink::Member
815 #define RefPtrWillBePersistent blink::Persistent
816 #define RefPtrWillBeRawPtr WTF::RawPtr
817 #define RefPtrWillBeMember blink::Member
818 #define RefPtrWillBeWeakMember blink::WeakMember
819 #define RefPtrWillBeCrossThreadPersistent blink::CrossThreadPersistent
820 #define RawPtrWillBeMember blink::Member
821 #define RawPtrWillBePersistent blink::Persistent
822 #define RawPtrWillBeWeakMember blink::WeakMember
823 #define OwnPtrWillBeMember blink::Member
824 #define OwnPtrWillBePersistent blink::Persistent
825 #define OwnPtrWillBeRawPtr WTF::RawPtr
826 #define PassOwnPtrWillBeRawPtr WTF::RawPtr
827 #define WeakPtrWillBeMember blink::Member
828 #define WeakPtrWillBeRawPtr WTF::RawPtr
829 #define WeakPtrWillBeMember blink::Member
830 #define WeakPtrWillBeWeakMember blink::WeakMember
831 #define NoBaseWillBeGarbageCollected blink::GarbageCollected
832 #define NoBaseWillBeGarbageCollectedFinalized blink::GarbageCollectedFinalized
833 #define NoBaseWillBeRefCountedGarbageCollected blink::RefCountedGarbageCollected
834 #define WillBeHeapHashMap blink::HeapHashMap
835 #define WillBePersistentHeapHashMap blink::PersistentHeapHashMap
836 #define WillBeHeapHashSet blink::HeapHashSet
837 #define WillBePersistentHeapHashSet blink::PersistentHeapHashSet
838 #define WillBeHeapLinkedHashSet blink::HeapLinkedHashSet
839 #define WillBePersistentHeapLinkedHashSet blink::PersistentHeapLinkedHashSet
840 #define WillBeHeapListHashSet blink::HeapListHashSet
841 #define WillBePersistentHeapListHashSet blink::PersistentHeapListHashSet
842 #define WillBeHeapVector blink::HeapVector
843 #define WillBePersistentHeapVector blink::PersistentHeapVector
844 #define WillBeHeapDeque blink::HeapDeque
845 #define WillBePersistentHeapDeque blink::PersistentHeapDeque
846 #define WillBeHeapHashCountedSet blink::HeapHashCountedSet
847 #define WillBePersistentHeapHashCountedSet blink::PersistentHeapHashCountedSet
848 #define WillBeGarbageCollectedMixin blink::GarbageCollectedMixin
849 #define WillBeHeapSupplement blink::HeapSupplement
850 #define WillBeHeapSupplementable blink::HeapSupplementable
851 #define WillBeHeapTerminatedArray blink::HeapTerminatedArray
852 #define WillBeHeapTerminatedArrayBuilder blink::HeapTerminatedArrayBuilder
853 #define WillBeHeapLinkedStack blink::HeapLinkedStack
854 #define PersistentHeapHashMapWillBeHeapHashMap blink::HeapHashMap
855 #define PersistentHeapHashSetWillBeHeapHashSet blink::HeapHashSet
856 #define PersistentHeapDequeWillBeHeapDeque blink::HeapDeque
857 #define PersistentHeapVectorWillBeHeapVector blink::HeapVector
859 template<typename T> T* adoptRefWillBeNoop(T* ptr)
861 static const bool notRefCounted = !WTF::IsSubclassOfTemplate<typename WTF::RemoveConst<T>::Type, RefCounted>::value;
862 COMPILE_ASSERT(notRefCounted, youMustAdopt);
866 template<typename T> T* adoptPtrWillBeNoop(T* ptr)
868 static const bool notRefCounted = !WTF::IsSubclassOfTemplate<typename WTF::RemoveConst<T>::Type, RefCounted>::value;
869 COMPILE_ASSERT(notRefCounted, youMustAdopt);
873 #define WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED // do nothing when oilpan is enabled.
874 #define DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(type) // do nothing
875 #define DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(type) // do nothing
876 #define DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(type) // do nothing
878 #define DEFINE_STATIC_REF_WILL_BE_PERSISTENT(type, name, arguments) \
879 static type* name = (new Persistent<type>(arguments))->get();
881 #else // !ENABLE(OILPAN)
890 #define PassRefPtrWillBeRawPtr WTF::PassRefPtr
891 #define RefCountedWillBeGarbageCollected WTF::RefCounted
892 #define RefCountedWillBeGarbageCollectedFinalized WTF::RefCounted
893 #define RefCountedWillBeRefCountedGarbageCollected WTF::RefCounted
894 #define RefCountedGarbageCollectedWillBeGarbageCollectedFinalized blink::RefCountedGarbageCollected
895 #define ThreadSafeRefCountedWillBeGarbageCollected WTF::ThreadSafeRefCounted
896 #define ThreadSafeRefCountedWillBeGarbageCollectedFinalized WTF::ThreadSafeRefCounted
897 #define PersistentWillBeMember blink::Persistent
898 #define CrossThreadPersistentWillBeMember blink::CrossThreadPersistent
899 #define RefPtrWillBePersistent WTF::RefPtr
900 #define RefPtrWillBeRawPtr WTF::RefPtr
901 #define RefPtrWillBeMember WTF::RefPtr
902 #define RefPtrWillBeWeakMember WTF::RefPtr
903 #define RefPtrWillBeCrossThreadPersistent WTF::RefPtr
904 #define RawPtrWillBeMember WTF::RawPtr
905 #define RawPtrWillBePersistent WTF::RawPtr
906 #define RawPtrWillBeWeakMember WTF::RawPtr
907 #define OwnPtrWillBeMember WTF::OwnPtr
908 #define OwnPtrWillBePersistent WTF::OwnPtr
909 #define OwnPtrWillBeRawPtr WTF::OwnPtr
910 #define PassOwnPtrWillBeRawPtr WTF::PassOwnPtr
911 #define WeakPtrWillBeMember WTF::WeakPtr
912 #define WeakPtrWillBeRawPtr WTF::WeakPtr
913 #define WeakPtrWillBeMember WTF::WeakPtr
914 #define WeakPtrWillBeWeakMember WTF::WeakPtr
915 #define NoBaseWillBeGarbageCollected blink::DummyBase
916 #define NoBaseWillBeGarbageCollectedFinalized blink::DummyBase
917 #define NoBaseWillBeRefCountedGarbageCollected blink::DummyBase
918 #define WillBeHeapHashMap WTF::HashMap
919 #define WillBePersistentHeapHashMap WTF::HashMap
920 #define WillBeHeapHashSet WTF::HashSet
921 #define WillBePersistentHeapHashSet WTF::HashSet
922 #define WillBeHeapLinkedHashSet WTF::LinkedHashSet
923 #define WillBePersistentLinkedHeapHashSet WTF::LinkedHashSet
924 #define WillBeHeapListHashSet WTF::ListHashSet
925 #define WillBePersistentListHeapHashSet WTF::ListHashSet
926 #define WillBeHeapVector WTF::Vector
927 #define WillBePersistentHeapVector WTF::Vector
928 #define WillBeHeapDeque WTF::Deque
929 #define WillBePersistentHeapDeque WTF::Deque
930 #define WillBeHeapHashCountedSet WTF::HashCountedSet
931 #define WillBePersistentHeapHashCountedSet WTF::HashCountedSet
932 #define WillBeGarbageCollectedMixin blink::DummyBase<void>
933 #define WillBeHeapSupplement blink::Supplement
934 #define WillBeHeapSupplementable blink::Supplementable
935 #define WillBeHeapTerminatedArray WTF::TerminatedArray
936 #define WillBeHeapTerminatedArrayBuilder WTF::TerminatedArrayBuilder
937 #define WillBeHeapLinkedStack WTF::LinkedStack
938 #define PersistentHeapHashMapWillBeHeapHashMap blink::PersistentHeapHashMap
939 #define PersistentHeapHashSetWillBeHeapHashSet blink::PersistentHeapHashSet
940 #define PersistentHeapDequeWillBeHeapDeque blink::PersistentHeapDeque
941 #define PersistentHeapVectorWillBeHeapVector blink::PersistentHeapVector
943 template<typename T> PassRefPtrWillBeRawPtr<T> adoptRefWillBeNoop(T* ptr) { return adoptRef(ptr); }
944 template<typename T> PassOwnPtrWillBeRawPtr<T> adoptPtrWillBeNoop(T* ptr) { return adoptPtr(ptr); }
946 #define WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED WTF_MAKE_FAST_ALLOCATED
947 #define DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(type) \
951 #define DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(type) \
956 #define DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(type) \
959 #define DEFINE_STATIC_REF_WILL_BE_PERSISTENT(type, name, arguments) \
960 DEFINE_STATIC_REF(type, name, arguments)
962 #endif // ENABLE(OILPAN)
968 template <typename T> struct VectorTraits<blink::Member<T> > : VectorTraitsBase<blink::Member<T> > {
969 static const bool needsDestruction = false;
970 static const bool canInitializeWithMemset = true;
971 static const bool canMoveWithMemcpy = true;
974 template <typename T> struct VectorTraits<blink::WeakMember<T> > : VectorTraitsBase<blink::WeakMember<T> > {
975 static const bool needsDestruction = false;
976 static const bool canInitializeWithMemset = true;
977 static const bool canMoveWithMemcpy = true;
980 template <typename T> struct VectorTraits<blink::HeapVector<T, 0> > : VectorTraitsBase<blink::HeapVector<T, 0> > {
981 static const bool needsDestruction = false;
982 static const bool canInitializeWithMemset = true;
983 static const bool canMoveWithMemcpy = true;
986 template <typename T> struct VectorTraits<blink::HeapDeque<T, 0> > : VectorTraitsBase<blink::HeapDeque<T, 0> > {
987 static const bool needsDestruction = false;
988 static const bool canInitializeWithMemset = true;
989 static const bool canMoveWithMemcpy = true;
992 template <typename T, size_t inlineCapacity> struct VectorTraits<blink::HeapVector<T, inlineCapacity> > : VectorTraitsBase<blink::HeapVector<T, inlineCapacity> > {
993 static const bool needsDestruction = VectorTraits<T>::needsDestruction;
994 static const bool canInitializeWithMemset = VectorTraits<T>::canInitializeWithMemset;
995 static const bool canMoveWithMemcpy = VectorTraits<T>::canMoveWithMemcpy;
998 template <typename T, size_t inlineCapacity> struct VectorTraits<blink::HeapDeque<T, inlineCapacity> > : VectorTraitsBase<blink::HeapDeque<T, inlineCapacity> > {
999 static const bool needsDestruction = VectorTraits<T>::needsDestruction;
1000 static const bool canInitializeWithMemset = VectorTraits<T>::canInitializeWithMemset;
1001 static const bool canMoveWithMemcpy = VectorTraits<T>::canMoveWithMemcpy;
1004 template<typename T> struct HashTraits<blink::Member<T> > : SimpleClassHashTraits<blink::Member<T> > {
1005 static const bool needsDestruction = false;
1006 // FIXME: The distinction between PeekInType and PassInType is there for
1007 // the sake of the reference counting handles. When they are gone the two
1008 // types can be merged into PassInType.
1009 // FIXME: Implement proper const'ness for iterator types. Requires support
1010 // in the marking Visitor.
1011 typedef RawPtr<T> PeekInType;
1012 typedef RawPtr<T> PassInType;
1013 typedef blink::Member<T>* IteratorGetType;
1014 typedef const blink::Member<T>* IteratorConstGetType;
1015 typedef blink::Member<T>& IteratorReferenceType;
1016 typedef T* const IteratorConstReferenceType;
1017 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; }
1018 static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return x->get(); }
1019 // FIXME: Similarly, there is no need for a distinction between PeekOutType
1020 // and PassOutType without reference counting.
1021 typedef T* PeekOutType;
1022 typedef T* PassOutType;
1024 template<typename U>
1025 static void store(const U& value, blink::Member<T>& storage) { storage = value; }
1027 static PeekOutType peek(const blink::Member<T>& value) { return value; }
1028 static PassOutType passOut(const blink::Member<T>& value) { return value; }
1031 template<typename T> struct HashTraits<blink::WeakMember<T> > : SimpleClassHashTraits<blink::WeakMember<T> > {
1032 static const bool needsDestruction = false;
1033 // FIXME: The distinction between PeekInType and PassInType is there for
1034 // the sake of the reference counting handles. When they are gone the two
1035 // types can be merged into PassInType.
1036 // FIXME: Implement proper const'ness for iterator types. Requires support
1037 // in the marking Visitor.
1038 typedef RawPtr<T> PeekInType;
1039 typedef RawPtr<T> PassInType;
1040 typedef blink::WeakMember<T>* IteratorGetType;
1041 typedef const blink::WeakMember<T>* IteratorConstGetType;
1042 typedef blink::WeakMember<T>& IteratorReferenceType;
1043 typedef T* const IteratorConstReferenceType;
1044 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; }
1045 static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return x->get(); }
1046 // FIXME: Similarly, there is no need for a distinction between PeekOutType
1047 // and PassOutType without reference counting.
1048 typedef T* PeekOutType;
1049 typedef T* PassOutType;
1051 template<typename U>
1052 static void store(const U& value, blink::WeakMember<T>& storage) { storage = value; }
1054 static PeekOutType peek(const blink::WeakMember<T>& value) { return value; }
1055 static PassOutType passOut(const blink::WeakMember<T>& value) { return value; }
1056 static bool traceInCollection(blink::Visitor* visitor, blink::WeakMember<T>& weakMember, ShouldWeakPointersBeMarkedStrongly strongify)
1058 if (strongify == WeakPointersActStrong) {
1059 visitor->trace(reinterpret_cast<blink::Member<T>&>(weakMember)); // Strongified visit.
1062 return !visitor->isAlive(weakMember);
1066 template<typename T> struct PtrHash<blink::Member<T> > : PtrHash<T*> {
1067 template<typename U>
1068 static unsigned hash(const U& key) { return PtrHash<T*>::hash(key); }
1069 static bool equal(T* a, const blink::Member<T>& b) { return a == b; }
1070 static bool equal(const blink::Member<T>& a, T* b) { return a == b; }
1071 template<typename U, typename V>
1072 static bool equal(const U& a, const V& b) { return a == b; }
1075 template<typename T> struct PtrHash<blink::WeakMember<T> > : PtrHash<blink::Member<T> > {
1078 template<typename P> struct PtrHash<blink::Persistent<P> > : PtrHash<P*> {
1079 using PtrHash<P*>::hash;
1080 static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); }
1081 using PtrHash<P*>::equal;
1082 static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
1083 static bool equal(P* a, const RefPtr<P>& b) { return a == b; }
1084 static bool equal(const RefPtr<P>& a, P* b) { return a == b; }
1087 // PtrHash is the default hash for hash tables with members.
1088 template<typename T> struct DefaultHash<blink::Member<T> > {
1089 typedef PtrHash<blink::Member<T> > Hash;
1092 template<typename T> struct DefaultHash<blink::WeakMember<T> > {
1093 typedef PtrHash<blink::WeakMember<T> > Hash;
1096 template<typename T> struct DefaultHash<blink::Persistent<T> > {
1097 typedef PtrHash<blink::Persistent<T> > Hash;
1100 template<typename T>
1101 struct NeedsTracing<blink::Member<T> > {
1102 static const bool value = true;
1105 template<typename T>
1106 struct IsWeak<blink::WeakMember<T> > {
1107 static const bool value = true;
1110 template<typename T> inline T* getPtr(const blink::Member<T>& p)
1115 template<typename T> inline T* getPtr(const blink::Persistent<T>& p)
1120 template<typename T, size_t inlineCapacity>
1121 struct NeedsTracing<ListHashSetNode<T, blink::HeapListHashSetAllocator<T, inlineCapacity> > *> {
1122 // All heap allocated node pointers need visiting to keep the nodes alive,
1123 // regardless of whether they contain pointers to other heap allocated
1125 static const bool value = true;
1128 // For wtf/Functional.h
1129 template<typename T, bool isGarbageCollected> struct PointerParamStorageTraits;
1131 template<typename T>
1132 struct PointerParamStorageTraits<T*, false> {
1133 typedef T* StorageType;
1135 static StorageType wrap(T* value) { return value; }
1136 static T* unwrap(const StorageType& value) { return value; }
1139 template<typename T>
1140 struct PointerParamStorageTraits<T*, true> {
1141 typedef blink::CrossThreadPersistent<T> StorageType;
1143 static StorageType wrap(T* value) { return value; }
1144 static T* unwrap(const StorageType& value) { return value.get(); }
1147 template<typename T>
1148 struct ParamStorageTraits<T*> : public PointerParamStorageTraits<T*, blink::IsGarbageCollectedType<T>::value> {
1151 template<typename T>
1152 struct ParamStorageTraits<RawPtr<T> > : public PointerParamStorageTraits<T*, blink::IsGarbageCollectedType<T>::value> {