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/HashFunctions.h"
38 #include "wtf/Locker.h"
39 #include "wtf/RawPtr.h"
40 #include "wtf/RefCounted.h"
41 #include "wtf/TypeTraits.h"
45 template<typename T> class HeapTerminatedArray;
47 // Template to determine if a class is a GarbageCollectedMixin by checking if it
48 // has adjustAndMark and isAlive. We can't check directly if the class is a
49 // GarbageCollectedMixin because casting to it is potentially ambiguous.
51 struct IsGarbageCollectedMixin {
52 typedef char TrueType;
58 template<typename U> static TrueType hasAdjustAndMark(char[&U::adjustAndMark != 0]);
59 template<typename U> static TrueType hasIsAlive(char[&U::isAlive != 0]);
61 template<size_t> struct F;
62 template<typename U> static TrueType hasAdjustAndMark(F<sizeof(&U::adjustAndMark)>*);
63 template<typename U> static TrueType hasIsAlive(F<sizeof(&U::isAlive)>*);
65 template<typename U> static FalseType hasIsAlive(...);
66 template<typename U> static FalseType hasAdjustAndMark(...);
68 static bool const value = (sizeof(TrueType) == sizeof(hasAdjustAndMark<T>(0))) && (sizeof(TrueType) == sizeof(hasIsAlive<T>(0)));
71 #define COMPILE_ASSERT_IS_GARBAGE_COLLECTED(T, ErrorMessage) \
73 typedef typename WTF::RemoveConst<T>::Type NonConstType; \
74 typedef WTF::IsSubclassOfTemplate<NonConstType, GarbageCollected> GarbageCollectedSubclass; \
75 typedef IsGarbageCollectedMixin<NonConstType> GarbageCollectedMixinSubclass; \
76 typedef WTF::IsSubclassOfTemplate3<NonConstType, HeapHashSet> HeapHashSetSubclass; \
77 typedef WTF::IsSubclassOfTemplate3<NonConstType, HeapLinkedHashSet> HeapLinkedHashSetSubclass; \
78 typedef WTF::IsSubclassOfTemplateTypenameSizeTypename<NonConstType, HeapListHashSet> HeapListHashSetSubclass; \
79 typedef WTF::IsSubclassOfTemplate5<NonConstType, HeapHashMap> HeapHashMapSubclass; \
80 typedef WTF::IsSubclassOfTemplateTypenameSize<NonConstType, HeapVector> HeapVectorSubclass; \
81 typedef WTF::IsSubclassOfTemplateTypenameSize<NonConstType, HeapDeque> HeapDequeSubclass; \
82 typedef WTF::IsSubclassOfTemplate3<NonConstType, HeapHashCountedSet> HeapHashCountedSetSubclass; \
83 typedef WTF::IsSubclassOfTemplate<NonConstType, HeapTerminatedArray> HeapTerminatedArraySubclass; \
84 COMPILE_ASSERT(GarbageCollectedSubclass::value || \
85 GarbageCollectedMixinSubclass::value || \
86 HeapHashSetSubclass::value || \
87 HeapLinkedHashSetSubclass::value || \
88 HeapListHashSetSubclass::value || \
89 HeapHashMapSubclass::value || \
90 HeapVectorSubclass::value || \
91 HeapDequeSubclass::value || \
92 HeapHashCountedSetSubclass::value || \
93 HeapTerminatedArraySubclass::value, \
97 template<typename T> class Member;
99 class PersistentNode {
101 explicit PersistentNode(TraceCallback trace)
106 bool isAlive() { return m_trace; }
108 virtual ~PersistentNode()
114 // Ideally the trace method should be virtual and automatically dispatch
115 // to the most specific implementation. However having a virtual method
116 // on PersistentNode leads to too eager template instantiation with MSVC
117 // which leads to include cycles.
118 // Instead we call the constructor with a TraceCallback which knows the
119 // type of the most specific child and calls trace directly. See
120 // TraceMethodDelegate in Visitor.h for how this is done.
121 void trace(Visitor* visitor)
123 m_trace(visitor, this);
127 TraceCallback m_trace;
130 PersistentNode* m_next;
131 PersistentNode* m_prev;
133 template<typename RootsAccessor, typename Owner> friend class PersistentBase;
134 friend class PersistentAnchor;
135 friend class ThreadState;
138 // RootsAccessor for Persistent that provides access to thread-local list
139 // of persistent handles. Can only be used to create handles that
140 // are constructed and destructed on the same thread.
141 template<ThreadAffinity Affinity>
142 class ThreadLocalPersistents {
144 static PersistentNode* roots() { return state()->roots(); }
146 // No locking required. Just check that we are at the right thread.
149 Lock() { state()->checkThread(); }
153 static ThreadState* state() { return ThreadStateFor<Affinity>::state(); }
156 // RootsAccessor for Persistent that provides synchronized access to global
157 // list of persistent handles. Can be used for persistent handles that are
158 // passed between threads.
159 class GlobalPersistents {
161 static PersistentNode* roots() { return ThreadState::globalRoots(); }
165 Lock() : m_locker(ThreadState::globalRootsMutex()) { }
167 MutexLocker m_locker;
171 // Base class for persistent handles. RootsAccessor specifies which list to
172 // link resulting handle into. Owner specifies the class containing trace
174 template<typename RootsAccessor, typename Owner>
175 class PersistentBase : public PersistentNode {
179 typename RootsAccessor::Lock lock;
180 ASSERT(m_roots == RootsAccessor::roots()); // Check that the thread is using the same roots list.
182 ASSERT(m_next->isAlive());
183 ASSERT(m_prev->isAlive());
184 m_next->m_prev = m_prev;
185 m_prev->m_next = m_next;
189 inline PersistentBase()
190 : PersistentNode(TraceMethodDelegate<Owner, &Owner::trace>::trampoline)
192 , m_roots(RootsAccessor::roots())
195 typename RootsAccessor::Lock lock;
196 m_prev = RootsAccessor::roots();
197 m_next = m_prev->m_next;
198 m_prev->m_next = this;
199 m_next->m_prev = this;
202 inline explicit PersistentBase(const PersistentBase& otherref)
203 : PersistentNode(otherref.m_trace)
205 , m_roots(RootsAccessor::roots())
208 typename RootsAccessor::Lock lock;
209 ASSERT(otherref.m_roots == m_roots); // Handles must belong to the same list.
210 PersistentBase* other = const_cast<PersistentBase*>(&otherref);
212 m_next = other->m_next;
213 other->m_next = this;
214 m_next->m_prev = this;
217 inline PersistentBase& operator=(const PersistentBase& otherref) { return *this; }
221 PersistentNode* m_roots;
225 // A dummy Persistent handle that ensures the list of persistents is never null.
226 // This removes a test from a hot path.
227 class PersistentAnchor : public PersistentNode {
229 void trace(Visitor* visitor)
231 for (PersistentNode* current = m_next; current != this; current = current->m_next)
232 current->trace(visitor);
235 virtual ~PersistentAnchor()
237 // FIXME: oilpan: Ideally we should have no left-over persistents at this point. However currently there is a
238 // large number of objects leaked when we tear down the main thread. Since some of these might contain a
239 // persistent or e.g. be RefCountedGarbageCollected we cannot guarantee there are no remaining Persistents at
244 PersistentAnchor() : PersistentNode(TraceMethodDelegate<PersistentAnchor, &PersistentAnchor::trace>::trampoline)
250 friend class ThreadState;
254 // For global persistent handles we cannot check that the
255 // pointer is in the heap because that would involve
256 // inspecting the heap of running threads.
257 #define ASSERT_IS_VALID_PERSISTENT_POINTER(pointer) \
258 bool isGlobalPersistent = WTF::IsSubclass<RootsAccessor, GlobalPersistents>::value; \
259 ASSERT(!pointer || isGlobalPersistent || ThreadStateFor<ThreadingTrait<T>::Affinity>::state()->contains(pointer))
261 #define ASSERT_IS_VALID_PERSISTENT_POINTER(pointer)
265 class CrossThreadPersistent;
267 // Persistent handles are used to store pointers into the
268 // managed heap. As long as the Persistent handle is alive
269 // the GC will keep the object pointed to alive. Persistent
270 // handles can be stored in objects and they are not scoped.
271 // Persistent handles must not be used to contain pointers
272 // between objects that are in the managed heap. They are only
273 // meant to point to managed heap objects from variables/members
274 // outside the managed heap.
276 // A Persistent is always a GC root from the point of view of
277 // the garbage collector.
279 // We have to construct and destruct Persistent with default RootsAccessor in
281 template<typename T, typename RootsAccessor /* = ThreadLocalPersistents<ThreadingTrait<T>::Affinity > */ >
282 class Persistent : public PersistentBase<RootsAccessor, Persistent<T, RootsAccessor> > {
283 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(Persistent);
284 WTF_DISALLOW_ZERO_ASSIGNMENT(Persistent);
286 Persistent() : m_raw(0) { }
288 Persistent(std::nullptr_t) : m_raw(0) { }
290 Persistent(T* raw) : m_raw(raw)
292 ASSERT_IS_VALID_PERSISTENT_POINTER(m_raw);
295 explicit Persistent(T& raw) : m_raw(&raw)
297 ASSERT_IS_VALID_PERSISTENT_POINTER(m_raw);
300 Persistent(const Persistent& other) : m_raw(other) { }
303 Persistent(const Persistent<U, RootsAccessor>& other) : m_raw(other) { }
306 Persistent(const Member<U>& other) : m_raw(other) { }
309 Persistent(const RawPtr<U>& other) : m_raw(other.get()) { }
312 Persistent& operator=(U* other)
318 Persistent& operator=(std::nullptr_t)
324 void clear() { m_raw = 0; }
326 virtual ~Persistent()
334 return static_cast<U*>(m_raw);
337 void trace(Visitor* visitor)
339 COMPILE_ASSERT_IS_GARBAGE_COLLECTED(T, NonGarbageCollectedObjectInPersistent);
340 #if ENABLE(GC_TRACING)
341 visitor->setHostInfo(this, "Persistent");
343 visitor->mark(m_raw);
353 T& operator*() const { return *m_raw; }
355 bool operator!() const { return !m_raw; }
357 operator T*() const { return m_raw; }
358 operator RawPtr<T>() const { return m_raw; }
360 T* operator->() const { return *this; }
362 Persistent& operator=(const Persistent& other)
369 Persistent& operator=(const Persistent<U, RootsAccessor>& other)
376 Persistent& operator=(const Member<U>& other)
383 Persistent& operator=(const RawPtr<U>& other)
389 T* get() const { return m_raw; }
394 friend class CrossThreadPersistent<T>;
397 // Unlike Persistent, we can destruct a CrossThreadPersistent in a thread
398 // different from the construction thread.
400 class CrossThreadPersistent : public Persistent<T, GlobalPersistents> {
401 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(CrossThreadPersistent);
402 WTF_DISALLOW_ZERO_ASSIGNMENT(CrossThreadPersistent);
404 CrossThreadPersistent(T* raw) : Persistent<T, GlobalPersistents>(raw) { }
406 using Persistent<T, GlobalPersistents>::operator=;
409 // FIXME: derive affinity based on the collection.
410 template<typename Collection, ThreadAffinity Affinity = AnyThread>
411 class PersistentHeapCollectionBase
413 , public PersistentBase<ThreadLocalPersistents<Affinity>, PersistentHeapCollectionBase<Collection, Affinity> > {
414 // We overload the various new and delete operators with using the WTF DefaultAllocator to ensure persistent
415 // heap collections are always allocated off-heap. This allows persistent collections to be used in
416 // DEFINE_STATIC_LOCAL et. al.
417 WTF_USE_ALLOCATOR(PersistentHeapCollectionBase, WTF::DefaultAllocator);
419 PersistentHeapCollectionBase() { }
421 template<typename OtherCollection>
422 PersistentHeapCollectionBase(const OtherCollection& other) : Collection(other) { }
424 void trace(Visitor* visitor) { visitor->trace(*static_cast<Collection*>(this)); }
430 typename HashArg = typename DefaultHash<KeyArg>::Hash,
431 typename KeyTraitsArg = HashTraits<KeyArg>,
432 typename MappedTraitsArg = HashTraits<MappedArg> >
433 class PersistentHeapHashMap : public PersistentHeapCollectionBase<HeapHashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg> > { };
437 typename HashArg = typename DefaultHash<ValueArg>::Hash,
438 typename TraitsArg = HashTraits<ValueArg> >
439 class PersistentHeapHashSet : public PersistentHeapCollectionBase<HeapHashSet<ValueArg, HashArg, TraitsArg> > { };
443 typename HashArg = typename DefaultHash<ValueArg>::Hash,
444 typename TraitsArg = HashTraits<ValueArg> >
445 class PersistentHeapLinkedHashSet : public PersistentHeapCollectionBase<HeapLinkedHashSet<ValueArg, HashArg, TraitsArg> > { };
449 size_t inlineCapacity = 0,
450 typename HashArg = typename DefaultHash<ValueArg>::Hash>
451 class PersistentHeapListHashSet : public PersistentHeapCollectionBase<HeapListHashSet<ValueArg, inlineCapacity, HashArg> > { };
453 template<typename T, typename U, typename V>
454 class PersistentHeapHashCountedSet : public PersistentHeapCollectionBase<HeapHashCountedSet<T, U, V> > { };
456 template<typename T, size_t inlineCapacity = 0>
457 class PersistentHeapVector : public PersistentHeapCollectionBase<HeapVector<T, inlineCapacity> > {
459 PersistentHeapVector() { }
461 template<size_t otherCapacity>
462 PersistentHeapVector(const HeapVector<T, otherCapacity>& other)
463 : PersistentHeapCollectionBase<HeapVector<T, inlineCapacity> >(other)
468 template<typename T, size_t inlineCapacity = 0>
469 class PersistentHeapDeque : public PersistentHeapCollectionBase<HeapDeque<T, inlineCapacity> > {
471 PersistentHeapDeque() { }
473 template<size_t otherCapacity>
474 PersistentHeapDeque(const HeapDeque<T, otherCapacity>& other)
475 : PersistentHeapCollectionBase<HeapDeque<T, inlineCapacity> >(other)
480 // Members are used in classes to contain strong pointers to other oilpan heap
481 // allocated objects.
482 // All Member fields of a class must be traced in the class' trace method.
483 // During the mark phase of the GC all live objects are marked as live and
484 // all Member fields of a live object will be traced marked as live as well.
487 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(Member);
488 WTF_DISALLOW_ZERO_ASSIGNMENT(Member);
494 Member(std::nullptr_t) : m_raw(0)
498 Member(T* raw) : m_raw(raw)
502 explicit Member(T& raw) : m_raw(&raw)
507 Member(const RawPtr<U>& other) : m_raw(other.get())
511 Member(WTF::HashTableDeletedValueType) : m_raw(reinterpret_cast<T*>(-1))
515 bool isHashTableDeletedValue() const { return m_raw == reinterpret_cast<T*>(-1); }
518 Member(const Persistent<U>& other) : m_raw(other) { }
520 Member(const Member& other) : m_raw(other) { }
523 Member(const Member<U>& other) : m_raw(other) { }
525 // FIXME: Oilpan: Get rid of these ASAP; this is only here to make
526 // Node hierarchy transition easier.
528 Member(const PassRefPtr<U>& other) : m_raw(other.get()) { }
531 Member(const RefPtr<U>& other) : m_raw(other.get()) { }
543 return static_cast<U*>(m_raw);
546 bool operator!() const { return !m_raw; }
548 operator T*() const { return m_raw; }
550 T* operator->() const { return m_raw; }
551 T& operator*() const { return *m_raw; }
552 operator RawPtr<T>() const { return m_raw; }
555 Member& operator=(const Persistent<U>& other)
562 Member& operator=(const Member<U>& other)
569 Member& operator=(U* other)
576 Member& operator=(RawPtr<U> other)
582 Member& operator=(std::nullptr_t)
588 void swap(Member<T>& other) { std::swap(m_raw, other.m_raw); }
590 T* get() const { return m_raw; }
592 void clear() { m_raw = 0; }
595 // FIXME: Oilpan: Remove this ASAP. Only here to make Node transition easier.
597 operator PassRefPtr<U>() { return PassRefPtr<U>(m_raw); }
600 void verifyTypeIsGarbageCollected() const
602 COMPILE_ASSERT_IS_GARBAGE_COLLECTED(T, NonGarbageCollectedObjectInMember);
607 template<bool x, bool y, ShouldWeakPointersBeMarkedStrongly z, typename U, typename V> friend struct CollectionBackingTraceTrait;
608 friend class Visitor;
612 class TraceTrait<Member<T> > {
614 static void trace(Visitor* visitor, void* self)
616 TraceTrait<T>::mark(visitor, *static_cast<Member<T>*>(self));
620 // TraceTrait to allow compilation of trace method bodies when oilpan is disabled.
621 // This should never be called, but is needed to compile.
623 class TraceTrait<RefPtr<T> > {
625 static void trace(Visitor*, void*)
627 ASSERT_NOT_REACHED();
632 class TraceTrait<OwnPtr<T> > {
634 static void trace(Visitor* visitor, OwnPtr<T>* ptr)
636 TraceTrait<T>::trace(visitor, ptr->get());
640 template<bool needsTracing, typename T>
641 struct StdPairHelper;
644 struct StdPairHelper<false, T> {
645 static void trace(Visitor*, T*) { }
649 struct StdPairHelper<true, T> {
650 static void trace(Visitor* visitor, T* t)
656 // This trace trait for std::pair will null weak members if their referent is
657 // collected. If you have a collection that contain weakness it does not remove
658 // entries from the collection that contain nulled weak members.
659 template<typename T, typename U>
660 class TraceTrait<std::pair<T, U> > {
662 static const bool firstNeedsTracing = WTF::NeedsTracing<T>::value || WTF::IsWeak<T>::value;
663 static const bool secondNeedsTracing = WTF::NeedsTracing<U>::value || WTF::IsWeak<U>::value;
664 static void trace(Visitor* visitor, std::pair<T, U>* pair)
666 StdPairHelper<firstNeedsTracing, T>::trace(visitor, &pair->first);
667 StdPairHelper<secondNeedsTracing, U>::trace(visitor, &pair->second);
671 // WeakMember is similar to Member in that it is used to point to other oilpan
672 // heap allocated objects.
673 // However instead of creating a strong pointer to the object, the WeakMember creates
674 // a weak pointer, which does not keep the pointee alive. Hence if all pointers to
675 // to a heap allocated object are weak the object will be garbage collected. At the
676 // time of GC the weak pointers will automatically be set to null.
678 class WeakMember : public Member<T> {
679 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(WeakMember);
680 WTF_DISALLOW_ZERO_ASSIGNMENT(WeakMember);
682 WeakMember() : Member<T>() { }
684 WeakMember(std::nullptr_t) : Member<T>(nullptr) { }
686 WeakMember(T* raw) : Member<T>(raw) { }
688 WeakMember(WTF::HashTableDeletedValueType x) : Member<T>(x) { }
691 WeakMember(const Persistent<U>& other) : Member<T>(other) { }
694 WeakMember(const Member<U>& other) : Member<T>(other) { }
697 WeakMember& operator=(const Persistent<U>& other)
704 WeakMember& operator=(const Member<U>& other)
711 WeakMember& operator=(U* other)
718 WeakMember& operator=(const RawPtr<U>& other)
724 WeakMember& operator=(std::nullptr_t)
731 T** cell() const { return const_cast<T**>(&this->m_raw); }
733 friend class Visitor;
736 // Comparison operators between (Weak)Members and Persistents
737 template<typename T, typename U> inline bool operator==(const Member<T>& a, const Member<U>& b) { return a.get() == b.get(); }
738 template<typename T, typename U> inline bool operator!=(const Member<T>& a, const Member<U>& b) { return a.get() != b.get(); }
739 template<typename T, typename U> inline bool operator==(const Member<T>& a, const Persistent<U>& b) { return a.get() == b.get(); }
740 template<typename T, typename U> inline bool operator!=(const Member<T>& a, const Persistent<U>& b) { return a.get() != b.get(); }
741 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Member<U>& b) { return a.get() == b.get(); }
742 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Member<U>& b) { return a.get() != b.get(); }
743 template<typename T, typename U> inline bool operator==(const Persistent<T>& a, const Persistent<U>& b) { return a.get() == b.get(); }
744 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a, const Persistent<U>& b) { return a.get() != b.get(); }
746 // FIXME: Oilpan: Get rid of these ASAP; only here to make Node transition easier.
747 template<typename T, typename U> inline bool operator==(const Member<T>& a, const RefPtr<U>& b) { return a.get() == b.get(); }
748 template<typename T, typename U> inline bool operator!=(const Member<T>& a, const RefPtr<U>& b) { return a.get() != b.get(); }
749 template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, const Member<U>& b) { return a.get() == b.get(); }
750 template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const Member<U>& b) { return a.get() != b.get(); }
751 template<typename T, typename U> inline bool operator==(const Member<T>& a, const PassRefPtr<U>& b) { return a.get() == b.get(); }
752 template<typename T, typename U> inline bool operator!=(const Member<T>& a, const PassRefPtr<U>& b) { return a.get() != b.get(); }
753 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const Member<U>& b) { return a.get() == b.get(); }
754 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const Member<U>& b) { return a.get() != b.get(); }
756 // CPP-defined type names for the transition period where we want to
757 // support both reference counting and garbage collection based on a
758 // compile-time flag.
760 // C++11 template aliases were initially used (with clang only, not
761 // with GCC nor MSVC.) However, supporting both CPP defines and
762 // template aliases is problematic from outside a WebCore namespace
763 // when Oilpan is disabled: e.g.,
764 // WebCore::RefCountedWillBeGarbageCollected as a template alias would
765 // uniquely resolve from within any namespace, but if it is backed by
766 // a CPP #define, it would expand to WebCore::RefCounted, and not the
767 // required WTF::RefCounted.
769 // Having the CPP expansion instead be fully namespace qualified, and the
770 // transition type be unqualified, would dually not work for template
771 // aliases. So, slightly unfortunately, fall back/down to the lowest
772 // commmon denominator of using CPP macros only.
774 #define PassRefPtrWillBeRawPtr WTF::RawPtr
775 #define RefCountedWillBeGarbageCollected WebCore::GarbageCollected
776 #define RefCountedWillBeGarbageCollectedFinalized WebCore::GarbageCollectedFinalized
777 #define RefCountedWillBeRefCountedGarbageCollected WebCore::RefCountedGarbageCollected
778 #define ThreadSafeRefCountedWillBeGarbageCollected WebCore::GarbageCollected
779 #define ThreadSafeRefCountedWillBeGarbageCollectedFinalized WebCore::GarbageCollectedFinalized
780 #define ThreadSafeRefCountedWillBeThreadSafeRefCountedGarbageCollected WebCore::ThreadSafeRefCountedGarbageCollected
781 #define TreeSharedWillBeRefCountedGarbageCollected WebCore::RefCountedGarbageCollected
782 #define PersistentWillBeMember WebCore::Member
783 #define RefPtrWillBePersistent WebCore::Persistent
784 #define RefPtrWillBeRawPtr WTF::RawPtr
785 #define RefPtrWillBeMember WebCore::Member
786 #define RefPtrWillBeCrossThreadPersistent WebCore::CrossThreadPersistent
787 #define RawPtrWillBeMember WebCore::Member
788 #define RawPtrWillBeWeakMember WebCore::WeakMember
789 #define OwnPtrWillBeMember WebCore::Member
790 #define OwnPtrWillBePersistent WebCore::Persistent
791 #define OwnPtrWillBeRawPtr WTF::RawPtr
792 #define PassOwnPtrWillBeRawPtr WTF::RawPtr
793 #define WeakPtrWillBeRawPtr WTF::RawPtr
794 #define WeakPtrWillBeWeakMember WebCore::WeakMember
795 #define NoBaseWillBeGarbageCollected WebCore::GarbageCollected
796 #define NoBaseWillBeGarbageCollectedFinalized WebCore::GarbageCollectedFinalized
797 #define WillBeHeapHashMap WebCore::HeapHashMap
798 #define WillBePersistentHeapHashMap WebCore::PersistentHeapHashMap
799 #define WillBeHeapHashSet WebCore::HeapHashSet
800 #define WillBePersistentHeapHashSet WebCore::PersistentHeapHashSet
801 #define WillBeHeapLinkedHashSet WebCore::HeapLinkedHashSet
802 #define WillBePersistentHeapLinkedHashSet WebCore::PersistentHeapLinkedHashSet
803 #define WillBeHeapListHashSet WebCore::HeapListHashSet
804 #define WillBePersistentHeapListHashSet WebCore::PersistentHeapListHashSet
805 #define WillBeHeapVector WebCore::HeapVector
806 #define WillBePersistentHeapVector WebCore::PersistentHeapVector
807 #define WillBeHeapDeque WebCore::HeapDeque
808 #define WillBePersistentHeapDeque WebCore::PersistentHeapDeque
809 #define WillBeHeapHashCountedSet WebCore::HeapHashCountedSet
810 #define WillBePersistentHeapHashCountedSet WebCore::PersistentHeapHashCountedSet
811 #define WillBeGarbageCollectedMixin WebCore::GarbageCollectedMixin
812 #define WillBeHeapSupplement WebCore::HeapSupplement
813 #define WillBeHeapSupplementable WebCore::HeapSupplementable
814 #define WillBeHeapTerminatedArray WebCore::HeapTerminatedArray
815 #define WillBeHeapTerminatedArrayBuilder WebCore::HeapTerminatedArrayBuilder
816 #define WillBeHeapLinkedStack WebCore::HeapLinkedStack
818 template<typename T> PassRefPtrWillBeRawPtr<T> adoptRefWillBeNoop(T* ptr)
820 static const bool notRefCountedGarbageCollected = !WTF::IsSubclassOfTemplate<T, RefCountedGarbageCollected>::value;
821 static const bool notRefCounted = !WTF::IsSubclassOfTemplate<T, RefCounted>::value;
822 COMPILE_ASSERT(notRefCountedGarbageCollected, useAdoptRefCountedWillBeRefCountedGarbageCollected);
823 COMPILE_ASSERT(notRefCounted, youMustAdopt);
824 return PassRefPtrWillBeRawPtr<T>(ptr);
827 template<typename T> PassRefPtrWillBeRawPtr<T> adoptRefWillBeRefCountedGarbageCollected(T* ptr)
829 static const bool isRefCountedGarbageCollected = WTF::IsSubclassOfTemplate<T, RefCountedGarbageCollected>::value;
830 COMPILE_ASSERT(isRefCountedGarbageCollected, useAdoptRefWillBeNoop);
831 return PassRefPtrWillBeRawPtr<T>(adoptRefCountedGarbageCollected(ptr));
834 template<typename T> PassRefPtrWillBeRawPtr<T> adoptRefWillBeThreadSafeRefCountedGarbageCollected(T* ptr)
836 static const bool isThreadSafeRefCountedGarbageCollected = WTF::IsSubclassOfTemplate<T, ThreadSafeRefCountedGarbageCollected>::value;
837 COMPILE_ASSERT(isThreadSafeRefCountedGarbageCollected, useAdoptRefWillBeNoop);
838 return PassRefPtrWillBeRawPtr<T>(adoptRefCountedGarbageCollected(ptr));
841 template<typename T> PassOwnPtrWillBeRawPtr<T> adoptPtrWillBeNoop(T* ptr)
843 static const bool notRefCountedGarbageCollected = !WTF::IsSubclassOfTemplate<T, RefCountedGarbageCollected>::value;
844 static const bool notRefCounted = !WTF::IsSubclassOfTemplate<T, RefCounted>::value;
845 COMPILE_ASSERT(notRefCountedGarbageCollected, useAdoptRefCountedWillBeRefCountedGarbageCollected);
846 COMPILE_ASSERT(notRefCounted, youMustAdopt);
847 return PassOwnPtrWillBeRawPtr<T>(ptr);
850 #define WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED // do nothing when oilpan is enabled.
851 #define DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(type) // do nothing
852 #define DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(type) // do nothing
853 #define DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(type) // do nothing
855 #define DEFINE_STATIC_REF_WILL_BE_PERSISTENT(type, name, arguments) \
856 DEFINE_STATIC_LOCAL(Persistent<type>, name, arguments)
858 #else // !ENABLE(OILPAN)
867 #define PassRefPtrWillBeRawPtr WTF::PassRefPtr
868 #define RefCountedWillBeGarbageCollected WTF::RefCounted
869 #define RefCountedWillBeGarbageCollectedFinalized WTF::RefCounted
870 #define RefCountedWillBeRefCountedGarbageCollected WTF::RefCounted
871 #define ThreadSafeRefCountedWillBeGarbageCollected WTF::ThreadSafeRefCounted
872 #define ThreadSafeRefCountedWillBeGarbageCollectedFinalized WTF::ThreadSafeRefCounted
873 #define ThreadSafeRefCountedWillBeThreadSafeRefCountedGarbageCollected WTF::ThreadSafeRefCounted
874 #define TreeSharedWillBeRefCountedGarbageCollected WebCore::TreeShared
875 #define PersistentWillBeMember WebCore::Persistent
876 #define RefPtrWillBePersistent WTF::RefPtr
877 #define RefPtrWillBeRawPtr WTF::RefPtr
878 #define RefPtrWillBeMember WTF::RefPtr
879 #define RefPtrWillBeCrossThreadPersistent WTF::RefPtr
880 #define RawPtrWillBeMember WTF::RawPtr
881 #define RawPtrWillBeWeakMember WTF::RawPtr
882 #define OwnPtrWillBeMember WTF::OwnPtr
883 #define OwnPtrWillBePersistent WTF::OwnPtr
884 #define OwnPtrWillBeRawPtr WTF::OwnPtr
885 #define PassOwnPtrWillBeRawPtr WTF::PassOwnPtr
886 #define WeakPtrWillBeRawPtr WTF::WeakPtr
887 #define WeakPtrWillBeWeakMember WTF::WeakPtr
888 #define NoBaseWillBeGarbageCollected WebCore::DummyBase
889 #define NoBaseWillBeGarbageCollectedFinalized WebCore::DummyBase
890 #define WillBeHeapHashMap WTF::HashMap
891 #define WillBePersistentHeapHashMap WTF::HashMap
892 #define WillBeHeapHashSet WTF::HashSet
893 #define WillBePersistentHeapHashSet WTF::HashSet
894 #define WillBeHeapLinkedHashSet WTF::LinkedHashSet
895 #define WillBePersistentLinkedHeapHashSet WTF::LinkedHashSet
896 #define WillBeHeapListHashSet WTF::ListHashSet
897 #define WillBePersistentListHeapHashSet WTF::ListHashSet
898 #define WillBeHeapVector WTF::Vector
899 #define WillBePersistentHeapVector WTF::Vector
900 #define WillBeHeapDeque WTF::Deque
901 #define WillBePersistentHeapDeque WTF::Deque
902 #define WillBeHeapHeapCountedSet WTF::HeapCountedSet
903 #define WillBePersistentHeapHeapCountedSet WTF::HeapCountedSet
904 #define WillBeGarbageCollectedMixin WebCore::DummyBase<void>
905 #define WillBeHeapSupplement WebCore::Supplement
906 #define WillBeHeapSupplementable WebCore::Supplementable
907 #define WillBeHeapTerminatedArray WTF::TerminatedArray
908 #define WillBeHeapTerminatedArrayBuilder WTF::TerminatedArrayBuilder
909 #define WillBeHeapLinkedStack WTF::LinkedStack
911 template<typename T> PassRefPtrWillBeRawPtr<T> adoptRefWillBeNoop(T* ptr) { return adoptRef(ptr); }
912 template<typename T> PassRefPtrWillBeRawPtr<T> adoptRefWillBeRefCountedGarbageCollected(T* ptr) { return adoptRef(ptr); }
913 template<typename T> PassRefPtrWillBeRawPtr<T> adoptRefWillBeThreadSafeRefCountedGarbageCollected(T* ptr) { return adoptRef(ptr); }
914 template<typename T> PassOwnPtrWillBeRawPtr<T> adoptPtrWillBeNoop(T* ptr) { return adoptPtr(ptr); }
916 #define WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED WTF_MAKE_FAST_ALLOCATED
917 #define DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(type) \
921 #define DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(type) \
926 #define DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(type) \
929 #define DEFINE_STATIC_REF_WILL_BE_PERSISTENT(type, name, arguments) \
930 DEFINE_STATIC_REF(type, name, arguments)
932 #endif // ENABLE(OILPAN)
934 } // namespace WebCore
938 template <typename T> struct VectorTraits<WebCore::Member<T> > : VectorTraitsBase<WebCore::Member<T> > {
939 static const bool needsDestruction = false;
940 static const bool canInitializeWithMemset = true;
941 static const bool canMoveWithMemcpy = true;
944 template <typename T> struct VectorTraits<WebCore::WeakMember<T> > : VectorTraitsBase<WebCore::WeakMember<T> > {
945 static const bool needsDestruction = false;
946 static const bool canInitializeWithMemset = true;
947 static const bool canMoveWithMemcpy = true;
950 template <typename T> struct VectorTraits<WebCore::HeapVector<T, 0> > : VectorTraitsBase<WebCore::HeapVector<T, 0> > {
951 static const bool needsDestruction = false;
952 static const bool canInitializeWithMemset = true;
953 static const bool canMoveWithMemcpy = true;
956 template <typename T> struct VectorTraits<WebCore::HeapDeque<T, 0> > : VectorTraitsBase<WebCore::HeapDeque<T, 0> > {
957 static const bool needsDestruction = false;
958 static const bool canInitializeWithMemset = true;
959 static const bool canMoveWithMemcpy = true;
962 template <typename T, size_t inlineCapacity> struct VectorTraits<WebCore::HeapVector<T, inlineCapacity> > : VectorTraitsBase<WebCore::HeapVector<T, inlineCapacity> > {
963 static const bool needsDestruction = VectorTraits<T>::needsDestruction;
964 static const bool canInitializeWithMemset = VectorTraits<T>::canInitializeWithMemset;
965 static const bool canMoveWithMemcpy = VectorTraits<T>::canMoveWithMemcpy;
968 template <typename T, size_t inlineCapacity> struct VectorTraits<WebCore::HeapDeque<T, inlineCapacity> > : VectorTraitsBase<WebCore::HeapDeque<T, inlineCapacity> > {
969 static const bool needsDestruction = VectorTraits<T>::needsDestruction;
970 static const bool canInitializeWithMemset = VectorTraits<T>::canInitializeWithMemset;
971 static const bool canMoveWithMemcpy = VectorTraits<T>::canMoveWithMemcpy;
974 template<typename T> struct HashTraits<WebCore::Member<T> > : SimpleClassHashTraits<WebCore::Member<T> > {
975 static const bool needsDestruction = false;
976 // FIXME: The distinction between PeekInType and PassInType is there for
977 // the sake of the reference counting handles. When they are gone the two
978 // types can be merged into PassInType.
979 // FIXME: Implement proper const'ness for iterator types. Requires support
980 // in the marking Visitor.
981 typedef RawPtr<T> PeekInType;
982 typedef RawPtr<T> PassInType;
983 typedef WebCore::Member<T>* IteratorGetType;
984 typedef const WebCore::Member<T>* IteratorConstGetType;
985 typedef WebCore::Member<T>& IteratorReferenceType;
986 typedef T* const IteratorConstReferenceType;
987 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; }
988 static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return x->get(); }
989 // FIXME: Similarly, there is no need for a distinction between PeekOutType
990 // and PassOutType without reference counting.
991 typedef T* PeekOutType;
992 typedef T* PassOutType;
995 static void store(const U& value, WebCore::Member<T>& storage) { storage = value; }
997 static PeekOutType peek(const WebCore::Member<T>& value) { return value; }
998 static PassOutType passOut(const WebCore::Member<T>& value) { return value; }
1001 template<typename T> struct HashTraits<WebCore::WeakMember<T> > : SimpleClassHashTraits<WebCore::WeakMember<T> > {
1002 static const bool needsDestruction = false;
1003 // FIXME: The distinction between PeekInType and PassInType is there for
1004 // the sake of the reference counting handles. When they are gone the two
1005 // types can be merged into PassInType.
1006 // FIXME: Implement proper const'ness for iterator types. Requires support
1007 // in the marking Visitor.
1008 typedef RawPtr<T> PeekInType;
1009 typedef RawPtr<T> PassInType;
1010 typedef WebCore::WeakMember<T>* IteratorGetType;
1011 typedef const WebCore::WeakMember<T>* IteratorConstGetType;
1012 typedef WebCore::WeakMember<T>& IteratorReferenceType;
1013 typedef T* const IteratorConstReferenceType;
1014 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; }
1015 static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return x->get(); }
1016 // FIXME: Similarly, there is no need for a distinction between PeekOutType
1017 // and PassOutType without reference counting.
1018 typedef T* PeekOutType;
1019 typedef T* PassOutType;
1021 template<typename U>
1022 static void store(const U& value, WebCore::WeakMember<T>& storage) { storage = value; }
1024 static PeekOutType peek(const WebCore::WeakMember<T>& value) { return value; }
1025 static PassOutType passOut(const WebCore::WeakMember<T>& value) { return value; }
1028 template<typename T> struct PtrHash<WebCore::Member<T> > : PtrHash<T*> {
1029 template<typename U>
1030 static unsigned hash(const U& key) { return PtrHash<T*>::hash(key); }
1031 static bool equal(T* a, const WebCore::Member<T>& b) { return a == b; }
1032 static bool equal(const WebCore::Member<T>& a, T* b) { return a == b; }
1033 template<typename U, typename V>
1034 static bool equal(const U& a, const V& b) { return a == b; }
1037 template<typename T> struct PtrHash<WebCore::WeakMember<T> > : PtrHash<WebCore::Member<T> > {
1040 template<typename P> struct PtrHash<WebCore::Persistent<P> > : PtrHash<P*> {
1041 using PtrHash<P*>::hash;
1042 static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); }
1043 using PtrHash<P*>::equal;
1044 static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
1045 static bool equal(P* a, const RefPtr<P>& b) { return a == b; }
1046 static bool equal(const RefPtr<P>& a, P* b) { return a == b; }
1049 // PtrHash is the default hash for hash tables with members.
1050 template<typename T> struct DefaultHash<WebCore::Member<T> > {
1051 typedef PtrHash<WebCore::Member<T> > Hash;
1054 template<typename T> struct DefaultHash<WebCore::WeakMember<T> > {
1055 typedef PtrHash<WebCore::WeakMember<T> > Hash;
1058 template<typename T> struct DefaultHash<WebCore::Persistent<T> > {
1059 typedef PtrHash<WebCore::Persistent<T> > Hash;
1062 template<typename T>
1063 struct NeedsTracing<WebCore::Member<T> > {
1064 static const bool value = true;
1067 template<typename T>
1068 struct IsWeak<WebCore::WeakMember<T> > {
1069 static const bool value = true;
1072 template<typename Table>
1073 struct IsWeak<WebCore::HeapHashTableBacking<Table> > {
1074 static const bool value = Table::ValueTraits::isWeak;
1077 template<typename T> inline T* getPtr(const WebCore::Member<T>& p)
1082 template<typename T, typename U>
1083 struct NeedsTracing<std::pair<T, U> > {
1084 static const bool value = NeedsTracing<T>::value || NeedsTracing<U>::value || IsWeak<T>::value || IsWeak<U>::value;
1087 template<typename T>
1088 struct NeedsTracing<OwnPtr<T> > {
1089 static const bool value = NeedsTracing<T>::value;
1092 // We define specialization of the NeedsTracing trait for off heap collections
1093 // since we don't support tracing them.
1094 template<typename T, size_t N>
1095 struct NeedsTracing<Vector<T, N> > {
1096 static const bool value = false;
1099 template<typename T, size_t N>
1100 struct NeedsTracing<Deque<T, N> > {
1101 static const bool value = false;
1104 template<typename T, typename U, typename V>
1105 struct NeedsTracing<HashCountedSet<T, U, V> > {
1106 static const bool value = false;
1109 template<typename T, typename U, typename V>
1110 struct NeedsTracing<HashSet<T, U, V> > {
1111 static const bool value = false;
1114 template<typename T, size_t U, typename V>
1115 struct NeedsTracing<ListHashSet<T, U, V> > {
1116 static const bool value = false;
1119 template<typename T, typename U, typename V>
1120 struct NeedsTracing<LinkedHashSet<T, U, V> > {
1121 static const bool value = false;
1124 template<typename T, typename U, typename V, typename W, typename X>
1125 struct NeedsTracing<HashMap<T, U, V, W, X> > {
1126 static const bool value = false;
1129 template<typename T, size_t inlineCapacity>
1130 struct NeedsTracing<ListHashSetNode<T, WebCore::HeapListHashSetAllocator<T, inlineCapacity> > *> {
1131 // All heap allocated node pointers need visiting to keep the nodes alive,
1132 // regardless of whether they contain pointers to other heap allocated
1134 static const bool value = true;