2 * Copyright (C) 2013 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 "heap/HeapExport.h"
35 #include "heap/ThreadState.h"
36 #include "wtf/Assertions.h"
37 #include "wtf/Deque.h"
38 #include "wtf/Forward.h"
39 #include "wtf/HashMap.h"
40 #include "wtf/HashSet.h"
41 #include "wtf/HashTraits.h"
42 #include "wtf/ListHashSet.h"
43 #include "wtf/OwnPtr.h"
44 #include "wtf/RefPtr.h"
45 #include "wtf/TypeTraits.h"
48 #define DEBUG_ONLY(x) x
55 class FinalizedHeapObjectHeader;
56 template<typename T> class GarbageCollectedFinalized;
57 class HeapObjectHeader;
58 template<typename T> class Member;
59 template<typename T> class WeakMember;
62 template<bool needsTracing, bool isWeak, bool markWeakMembersStrongly, typename T, typename Traits> struct CollectionBackingTraceTrait;
64 // The TraceMethodDelegate is used to convert a trace method for type T to a TraceCallback.
65 // This allows us to pass a type's trace method as a parameter to the PersistentNode
66 // constructor. The PersistentNode constructor needs the specific trace method due an issue
67 // with the Windows compiler which instantiates even unused variables. This causes problems
68 // in header files where we have only forward declarations of classes.
69 template<typename T, void (T::*method)(Visitor*)>
70 struct TraceMethodDelegate {
71 static void trampoline(Visitor* visitor, void* self) { (reinterpret_cast<T*>(self)->*method)(visitor); }
74 // GCInfo contains meta-data associated with objects allocated in the
75 // Blink heap. This meta-data consists of a function pointer used to
76 // trace the pointers in the object during garbage collection, an
77 // indication of whether or not the object needs a finalization
78 // callback, and a function pointer used to finalize the object when
79 // the garbage collector determines that the object is no longer
80 // reachable. There is a GCInfo struct for each class that directly
81 // inherits from GarbageCollected or GarbageCollectedFinalized.
83 bool hasFinalizer() const { return m_nonTrivialFinalizer; }
84 TraceCallback m_trace;
85 FinalizationCallback m_finalize;
86 bool m_nonTrivialFinalizer;
89 // The FinalizerTraitImpl specifies how to finalize objects. Object
90 // that inherit from GarbageCollectedFinalized are finalized by
91 // calling their 'finalize' method which by default will call the
92 // destructor on the object.
93 template<typename T, bool isGarbageCollectedFinalized>
94 struct FinalizerTraitImpl;
97 struct FinalizerTraitImpl<T, true> {
98 static void finalize(void* obj) { static_cast<T*>(obj)->finalizeGarbageCollectedObject(); };
102 struct FinalizerTraitImpl<T, false> {
103 static void finalize(void* obj) { };
106 // The FinalizerTrait is used to determine if a type requires
107 // finalization and what finalization means.
109 // By default classes that inherit from GarbageCollectedFinalized need
110 // finalization and finalization means calling the 'finalize' method
111 // of the object. The FinalizerTrait can be specialized if the default
112 // behavior is not desired.
114 struct FinalizerTrait {
115 static const bool nonTrivialFinalizer = WTF::IsSubclassOfTemplate<T, GarbageCollectedFinalized>::value;
116 static void finalize(void* obj) { FinalizerTraitImpl<T, nonTrivialFinalizer>::finalize(obj); }
119 // Trait to get the GCInfo structure for types that have their
120 // instances allocated in the Blink garbage-collected heap.
121 template<typename T> struct GCInfoTrait;
123 template<typename T> class GarbageCollected;
124 class GarbageCollectedMixin;
125 template<typename T, bool = WTF::IsSubclassOfTemplate<T, GarbageCollected>::value> class NeedsAdjustAndMark;
128 class NeedsAdjustAndMark<T, true> {
130 static const bool value = false;
134 class NeedsAdjustAndMark<T, false> {
136 static const bool value = WTF::IsSubclass<T, GarbageCollectedMixin>::value;
139 template<typename T, bool = NeedsAdjustAndMark<T>::value> class DefaultTraceTrait;
141 // The TraceTrait is used to specify how to mark an object pointer and
142 // how to trace all of the pointers in the object.
144 // By default, the 'trace' method implemented on an object itself is
145 // used to trace the pointers to other heap objects inside the object.
147 // However, the TraceTrait can be specialized to use a different
148 // implementation. A common case where a TraceTrait specialization is
149 // needed is when multiple inheritance leads to pointers that are not
150 // to the start of the object in the Blink garbage-collected heap. In
151 // that case the pointer has to be adjusted before marking.
155 // Default implementation of TraceTrait<T>::trace just statically
156 // dispatches to the trace method of the class T.
157 static void trace(Visitor* visitor, void* self)
159 static_cast<T*>(self)->trace(visitor);
162 static void mark(Visitor* visitor, const T* t)
164 DefaultTraceTrait<T>::mark(visitor, t);
168 static void checkGCInfo(Visitor* visitor, const T* t)
170 DefaultTraceTrait<T>::checkGCInfo(visitor, t);
175 template<typename T> class TraceTrait<const T> : public TraceTrait<T> { };
177 template<typename Collection>
178 struct OffHeapCollectionTraceTrait;
181 struct ObjectAliveTrait {
182 static bool isAlive(Visitor*, T);
186 struct ObjectAliveTrait<Member<T> > {
187 static bool isAlive(Visitor*, const Member<T>&);
190 // Visitor is used to traverse the Blink object graph. Used for the
191 // marking phase of the mark-sweep garbage collector.
193 // Pointers are marked and pushed on the marking stack by calling the
194 // |mark| method with the pointer as an argument.
196 // Pointers within objects are traced by calling the |trace| methods
197 // with the object as an argument. Tracing objects will mark all of the
198 // contained pointers and push them on the marking stack.
199 class HEAP_EXPORT Visitor {
201 virtual ~Visitor() { }
203 // One-argument templated mark method. This uses the static type of
204 // the argument to get the TraceTrait. By default, the mark method
205 // of the TraceTrait just calls the virtual two-argument mark method on this
206 // visitor, where the second argument is the static trace method of the trait.
213 TraceTrait<T>::checkGCInfo(this, t);
215 TraceTrait<T>::mark(this, t);
218 // Member version of the one-argument templated trace method.
220 void trace(const Member<T>& t)
225 // Fallback method used only when we need to trace raw pointers of T.
226 // This is the case when a member is a union where we do not support members.
233 // WeakMember version of the templated trace method. It doesn't keep
234 // the traced thing alive, but will write null to the WeakMember later
235 // if the pointed-to object is dead.
237 void trace(const WeakMember<T>& t)
239 registerWeakCell(t.cell());
242 // Fallback trace method for part objects to allow individual
243 // trace methods to trace through a part object with
244 // visitor->trace(m_partObject).
246 void trace(const T& t)
248 const_cast<T&>(t).trace(this);
251 // The following mark methods are for off-heap collections.
252 template<typename T, size_t inlineCapacity>
253 void trace(const Vector<T, inlineCapacity, WTF::DefaultAllocator>& vector)
255 OffHeapCollectionTraceTrait<Vector<T, inlineCapacity, WTF::DefaultAllocator> >::trace(this, vector);
258 template<typename T, typename U, typename V>
259 void trace(const HashSet<T, U, V, WTF::DefaultAllocator>& hashSet)
261 OffHeapCollectionTraceTrait<HashSet<T, U, V, WTF::DefaultAllocator> >::trace(this, hashSet);
264 template<typename T, size_t inlineCapacity, typename U>
265 void trace(const ListHashSet<T, inlineCapacity, U>& hashSet)
267 OffHeapCollectionTraceTrait<ListHashSet<T, inlineCapacity, U> >::trace(this, hashSet);
270 template<typename T, size_t N>
271 void trace(const Deque<T, N>& deque)
273 OffHeapCollectionTraceTrait<Deque<T, N> >::trace(this, deque);
276 template<typename T, typename U, typename V, typename W, typename X>
277 void trace(const HashMap<T, U, V, W, X, WTF::DefaultAllocator>& map)
279 OffHeapCollectionTraceTrait<HashMap<T, U, V, W, X, WTF::DefaultAllocator> >::trace(this, map);
282 // OwnPtrs that are traced are treated as part objects and the
283 // trace method of the owned object is called.
285 void trace(const OwnPtr<T>& t)
291 // This trace method is to trace a RefPtrWillBeMember when ENABLE(OILPAN)
293 // Remove this once we remove RefPtrWillBeMember.
295 void trace(const RefPtr<T>&)
298 // RefPtrs should never be traced.
299 ASSERT_NOT_REACHED();
304 // Similarly, this trace method is to trace a RawPtrWillBeMember
305 // when ENABLE(OILPAN) is not enabled.
306 // Remove this once we remove RawPtrWillBeMember.
308 void trace(const RawPtr<T>&)
313 // This method marks an object and adds it to the set of objects
314 // that should have their trace method called. Since not all
315 // objects have vtables we have to have the callback as an
316 // explicit argument, but we can use the templated one-argument
317 // mark method above to automatically provide the callback
319 virtual void mark(const void*, TraceCallback) = 0;
321 // Used to mark objects during conservative scanning.
322 virtual void mark(HeapObjectHeader*, TraceCallback) = 0;
323 virtual void mark(FinalizedHeapObjectHeader*, TraceCallback) = 0;
325 // If the object calls this during the regular trace callback, then the
326 // WeakPointerCallback argument may be called later, when the strong roots
327 // have all been found. The WeakPointerCallback will normally use isAlive
328 // to find out whether some pointers are pointing to dying objects. When
329 // the WeakPointerCallback is done the object must have purged all pointers
330 // to objects where isAlive returned false. In the weak callback it is not
331 // allowed to touch other objects (except using isAlive) or to allocate on
332 // the GC heap. Note that even removing things from HeapHashSet or
333 // HeapHashMap can cause an allocation if the backing store resizes, but
334 // these collections know to remove WeakMember elements safely.
336 // The weak pointer callbacks are run on the thread that owns the
337 // object and other threads are not stopped during the
338 // callbacks. Since isAlive is used in the callback to determine
339 // if objects pointed to are alive it is crucial that the object
340 // pointed to belong to the same thread as the object receiving
341 // the weak callback. Since other threads have been resumed the
342 // mark bits are not valid for objects from other threads.
343 virtual void registerWeakMembers(const void* object, WeakPointerCallback callback) { registerWeakMembers(object, object, callback); }
344 virtual void registerWeakMembers(const void*, const void*, WeakPointerCallback) = 0;
346 template<typename T, void (T::*method)(Visitor*)>
347 void registerWeakMembers(const T* obj)
349 registerWeakMembers(obj, &TraceMethodDelegate<T, method>::trampoline);
352 // For simple cases where you just want to zero out a cell when the thing
353 // it is pointing at is garbage, you can use this. This will register a
354 // callback for each cell that needs to be zeroed, so if you have a lot of
355 // weak cells in your object you should still consider using
356 // registerWeakMembers above.
358 // In contrast to registerWeakMembers, the weak cell callbacks are
359 // run on the thread performing garbage collection. Therefore, all
360 // threads are stopped during weak cell callbacks.
362 void registerWeakCell(T** cell)
364 registerWeakCell(reinterpret_cast<void**>(cell), &handleWeakCell<T>);
367 virtual bool isMarked(const void*) = 0;
369 template<typename T> inline bool isAlive(T obj) { return ObjectAliveTrait<T>::isAlive(this, obj); }
370 template<typename T> inline bool isAlive(const Member<T>& member)
372 return isAlive(member.get());
376 void checkGCInfo(const void*, const GCInfo*);
379 // Macro to declare methods needed for each typed heap.
380 #define DECLARE_VISITOR_METHODS(Type) \
381 DEBUG_ONLY(void checkGCInfo(const Type*, const GCInfo*);) \
382 virtual void mark(const Type*, TraceCallback) = 0; \
383 virtual bool isMarked(const Type*) = 0;
385 FOR_EACH_TYPED_HEAP(DECLARE_VISITOR_METHODS)
386 #undef DECLARE_VISITOR_METHODS
389 virtual void registerWeakCell(void**, WeakPointerCallback) = 0;
393 static void handleWeakCell(Visitor* self, void* obj)
395 T** cell = reinterpret_cast<T**>(obj);
396 if (*cell && !self->isAlive(*cell))
401 template<typename T, typename HashFunctions, typename Traits>
402 struct OffHeapCollectionTraceTrait<WTF::HashSet<T, HashFunctions, Traits, WTF::DefaultAllocator> > {
403 typedef WTF::HashSet<T, HashFunctions, Traits, WTF::DefaultAllocator> HashSet;
405 static void trace(Visitor* visitor, const HashSet& set)
409 if (WTF::ShouldBeTraced<Traits>::value) {
410 HashSet& iterSet = const_cast<HashSet&>(set);
411 for (typename HashSet::iterator it = iterSet.begin(), end = iterSet.end(); it != end; ++it)
412 CollectionBackingTraceTrait<WTF::ShouldBeTraced<Traits>::value, Traits::isWeak, false, T, Traits>::mark(visitor, *it);
414 COMPILE_ASSERT(!Traits::isWeak, WeakOffHeapCollectionsConsideredDangerous0);
418 template<typename T, size_t inlineCapacity, typename HashFunctions>
419 struct OffHeapCollectionTraceTrait<WTF::ListHashSet<T, inlineCapacity, HashFunctions> > {
420 typedef WTF::ListHashSet<T, inlineCapacity, HashFunctions> ListHashSet;
422 static void trace(Visitor* visitor, const ListHashSet& set)
426 ListHashSet& iterSet = const_cast<ListHashSet&>(set);
427 for (typename ListHashSet::iterator it = iterSet.begin(), end = iterSet.end(); it != end; ++it)
432 template<typename Key, typename Value, typename HashFunctions, typename KeyTraits, typename ValueTraits>
433 struct OffHeapCollectionTraceTrait<WTF::HashMap<Key, Value, HashFunctions, KeyTraits, ValueTraits, WTF::DefaultAllocator> > {
434 typedef WTF::HashMap<Key, Value, HashFunctions, KeyTraits, ValueTraits, WTF::DefaultAllocator> HashMap;
436 static void trace(Visitor* visitor, const HashMap& map)
440 if (WTF::ShouldBeTraced<KeyTraits>::value || WTF::ShouldBeTraced<ValueTraits>::value) {
441 HashMap& iterMap = const_cast<HashMap&>(map);
442 for (typename HashMap::iterator it = iterMap.begin(), end = iterMap.end(); it != end; ++it) {
443 CollectionBackingTraceTrait<WTF::ShouldBeTraced<KeyTraits>::value, KeyTraits::isWeak, false, Key, KeyTraits>::mark(visitor, it->key);
444 CollectionBackingTraceTrait<WTF::ShouldBeTraced<ValueTraits>::value, ValueTraits::isWeak, false, Value, ValueTraits>::mark(visitor, it->value);
447 COMPILE_ASSERT(!KeyTraits::isWeak, WeakOffHeapCollectionsConsideredDangerous1);
448 COMPILE_ASSERT(!ValueTraits::isWeak, WeakOffHeapCollectionsConsideredDangerous2);
452 // We trace vectors by using the trace trait on each element, which means you
453 // can have vectors of general objects (not just pointers to objects) that can
455 template<typename T, size_t N>
456 struct OffHeapCollectionTraceTrait<WTF::Vector<T, N, WTF::DefaultAllocator> > {
457 typedef WTF::Vector<T, N, WTF::DefaultAllocator> Vector;
459 static void trace(Visitor* visitor, const Vector& vector)
461 if (vector.isEmpty())
463 for (typename Vector::const_iterator it = vector.begin(), end = vector.end(); it != end; ++it)
464 TraceTrait<T>::trace(visitor, const_cast<T*>(it));
468 template<typename T, size_t N>
469 struct OffHeapCollectionTraceTrait<WTF::Deque<T, N> > {
470 typedef WTF::Deque<T, N> Deque;
472 static void trace(Visitor* visitor, const Deque& deque)
476 for (typename Deque::const_iterator it = deque.begin(), end = deque.end(); it != end; ++it)
477 TraceTrait<T>::trace(visitor, const_cast<T*>(&(*it)));
481 template<typename T, typename Traits = WTF::VectorTraits<T> >
482 class HeapVectorBacking;
483 template<typename Key, typename Value, typename Extractor, typename Traits, typename KeyTraits>
484 class HeapHashTableBacking;
487 class DefaultTraceTrait<T, false> {
489 static void mark(Visitor* visitor, const T* t)
491 // Default mark method of the trait just calls the two-argument mark
492 // method on the visitor. The second argument is the static trace method
493 // of the trait, which by default calls the instance method
494 // trace(Visitor*) on the object.
495 visitor->mark(const_cast<T*>(t), &TraceTrait<T>::trace);
499 static void checkGCInfo(Visitor* visitor, const T* t)
501 visitor->checkGCInfo(const_cast<T*>(t), GCInfoTrait<T>::get());
507 class DefaultTraceTrait<T, true> {
509 static void mark(Visitor* visitor, const T* self)
511 self->adjustAndMark(visitor);
515 static void checkGCInfo(Visitor*, const T*) { }
519 template<typename T, bool = NeedsAdjustAndMark<T>::value> class DefaultObjectAliveTrait;
522 class DefaultObjectAliveTrait<T, false> {
524 static bool isAlive(Visitor* visitor, T obj)
526 return visitor->isMarked(obj);
531 class DefaultObjectAliveTrait<T, true> {
533 static bool isAlive(Visitor* visitor, T obj)
535 return obj->isAlive(visitor);
539 template<typename T> bool ObjectAliveTrait<T>::isAlive(Visitor* visitor, T obj)
541 return DefaultObjectAliveTrait<T>::isAlive(visitor, obj);
543 template<typename T> bool ObjectAliveTrait<Member<T> >::isAlive(Visitor* visitor, const Member<T>& obj)
545 return visitor->isMarked(obj.get());
548 // The GarbageCollectedMixin interface and helper macro
549 // USING_GARBAGE_COLLECTED_MIXIN can be used to automatically define
550 // TraceTrait/ObjectAliveTrait on non-leftmost deriving classes
551 // which need to be garbage collected.
553 // Consider the following case:
555 // class A : public GarbageCollected, public B {};
557 // We can't correctly handle "Member<B> p = &a" as we can't compute addr of
558 // object header statically. This can be solved by using GarbageCollectedMixin:
559 // class B : public GarbageCollectedMixin {};
560 // class A : public GarbageCollected, public B {
561 // USING_GARBAGE_COLLECTED_MIXIN(A)
564 // With the helper, as long as we are using Member<B>, TypeTrait<B> will
565 // dispatch adjustAndMark dynamically to find collect addr of the object header.
566 // Note that this is only enabled for Member<B>. For Member<A> which we can
567 // compute the object header addr statically, this dynamic dispatch is not used.
569 class GarbageCollectedMixin {
571 virtual void adjustAndMark(Visitor*) const = 0;
572 virtual bool isAlive(Visitor*) const = 0;
575 #define USING_GARBAGE_COLLECTED_MIXIN(TYPE) \
577 virtual void adjustAndMark(Visitor* visitor) const OVERRIDE \
579 typedef WTF::IsSubclassOfTemplate<TYPE, WebCore::GarbageCollected> IsSubclassOfGarbageCollected; \
580 COMPILE_ASSERT(IsSubclassOfGarbageCollected::value, OnlyGarbageCollectedObjectsCanHaveGarbageCollectedMixins); \
581 visitor->mark(this, &TraceTrait<TYPE>::trace);\
583 virtual bool isAlive(Visitor* visitor) const OVERRIDE \
585 return visitor->isAlive(this); \
589 #define WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(TYPE) USING_GARBAGE_COLLECTED_MIXIN(TYPE)
591 #define WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(TYPE)
595 struct GCInfoAtBase {
596 static const GCInfo* get()
598 static const GCInfo gcInfo = {
599 TraceTrait<T>::trace,
600 FinalizerTrait<T>::finalize,
601 FinalizerTrait<T>::nonTrivialFinalizer,
607 template<typename T> class GarbageCollected;
608 template<typename T, bool = WTF::IsSubclassOfTemplate<T, GarbageCollected>::value> struct GetGarbageCollectedBase;
611 struct GetGarbageCollectedBase<T, true> {
612 typedef typename T::GarbageCollectedBase type;
616 struct GetGarbageCollectedBase<T, false> {
622 static const GCInfo* get()
624 return GCInfoAtBase<typename GetGarbageCollectedBase<T>::type>::get();