OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 24 matching lines...) Expand all Loading... |
35 #include "heap/ThreadState.h" | 35 #include "heap/ThreadState.h" |
36 #include "wtf/Assertions.h" | 36 #include "wtf/Assertions.h" |
37 #include "wtf/Deque.h" | 37 #include "wtf/Deque.h" |
38 #include "wtf/Forward.h" | 38 #include "wtf/Forward.h" |
39 #include "wtf/HashMap.h" | 39 #include "wtf/HashMap.h" |
40 #include "wtf/HashSet.h" | 40 #include "wtf/HashSet.h" |
41 #include "wtf/HashTraits.h" | 41 #include "wtf/HashTraits.h" |
42 #include "wtf/ListHashSet.h" | 42 #include "wtf/ListHashSet.h" |
43 #include "wtf/OwnPtr.h" | 43 #include "wtf/OwnPtr.h" |
44 #include "wtf/RefPtr.h" | 44 #include "wtf/RefPtr.h" |
| 45 #include "wtf/TypeTraits.h" |
45 | 46 |
46 #ifndef NDEBUG | 47 #ifndef NDEBUG |
47 #define DEBUG_ONLY(x) x | 48 #define DEBUG_ONLY(x) x |
48 #else | 49 #else |
49 #define DEBUG_ONLY(x) | 50 #define DEBUG_ONLY(x) |
50 #endif | 51 #endif |
51 | 52 |
52 namespace WebCore { | 53 namespace WebCore { |
53 | 54 |
54 class FinalizedHeapObjectHeader; | 55 class FinalizedHeapObjectHeader; |
(...skipping 29 matching lines...) Expand all Loading... |
84 // reachable. There is a GCInfo struct for each class that directly | 85 // reachable. There is a GCInfo struct for each class that directly |
85 // inherits from GarbageCollected or GarbageCollectedFinalized. | 86 // inherits from GarbageCollected or GarbageCollectedFinalized. |
86 struct GCInfo { | 87 struct GCInfo { |
87 bool hasFinalizer() const { return m_nonTrivialFinalizer; } | 88 bool hasFinalizer() const { return m_nonTrivialFinalizer; } |
88 const char* m_typeMarker; | 89 const char* m_typeMarker; |
89 TraceCallback m_trace; | 90 TraceCallback m_trace; |
90 FinalizationCallback m_finalize; | 91 FinalizationCallback m_finalize; |
91 bool m_nonTrivialFinalizer; | 92 bool m_nonTrivialFinalizer; |
92 }; | 93 }; |
93 | 94 |
94 // Template struct to detect whether type T inherits from | |
95 // GarbageCollectedFinalized. | |
96 template<typename T> | |
97 struct IsGarbageCollectedFinalized { | |
98 typedef char TrueType; | |
99 struct FalseType { | |
100 char dummy[2]; | |
101 }; | |
102 template<typename U> static TrueType has(GarbageCollectedFinalized<U>*); | |
103 static FalseType has(...); | |
104 static bool const value = sizeof(has(static_cast<T*>(0))) == sizeof(TrueType
); | |
105 }; | |
106 | |
107 // The FinalizerTraitImpl specifies how to finalize objects. Object | 95 // The FinalizerTraitImpl specifies how to finalize objects. Object |
108 // that inherit from GarbageCollectedFinalized are finalized by | 96 // that inherit from GarbageCollectedFinalized are finalized by |
109 // calling their 'finalize' method which by default will call the | 97 // calling their 'finalize' method which by default will call the |
110 // destructor on the object. | 98 // destructor on the object. |
111 template<typename T, bool isGarbageCollectedFinalized> | 99 template<typename T, bool isGarbageCollectedFinalized> |
112 struct FinalizerTraitImpl; | 100 struct FinalizerTraitImpl; |
113 | 101 |
114 template<typename T> | 102 template<typename T> |
115 struct FinalizerTraitImpl<T, true> { | 103 struct FinalizerTraitImpl<T, true> { |
116 static void finalize(void* obj) { static_cast<T*>(obj)->finalize(); }; | 104 static void finalize(void* obj) { static_cast<T*>(obj)->finalize(); }; |
117 }; | 105 }; |
118 | 106 |
119 template<typename T> | 107 template<typename T> |
120 struct FinalizerTraitImpl<T, false> { | 108 struct FinalizerTraitImpl<T, false> { |
121 static void finalize(void* obj) { }; | 109 static void finalize(void* obj) { }; |
122 }; | 110 }; |
123 | 111 |
124 // The FinalizerTrait is used to determine if a type requires | 112 // The FinalizerTrait is used to determine if a type requires |
125 // finalization and what finalization means. | 113 // finalization and what finalization means. |
126 // | 114 // |
127 // By default classes that inherit from GarbageCollectedFinalized need | 115 // By default classes that inherit from GarbageCollectedFinalized need |
128 // finalization and finalization means calling the 'finalize' method | 116 // finalization and finalization means calling the 'finalize' method |
129 // of the object. The FinalizerTrait can be specialized if the default | 117 // of the object. The FinalizerTrait can be specialized if the default |
130 // behavior is not desired. | 118 // behavior is not desired. |
131 template<typename T> | 119 template<typename T> |
132 struct FinalizerTrait { | 120 struct FinalizerTrait { |
133 static const bool nonTrivialFinalizer = IsGarbageCollectedFinalized<T>::valu
e; | 121 static const bool nonTrivialFinalizer = WTF::IsSubclassOfTemplate<T, Garbage
CollectedFinalized>::value; |
134 static void finalize(void* obj) { FinalizerTraitImpl<T, nonTrivialFinalizer>
::finalize(obj); } | 122 static void finalize(void* obj) { FinalizerTraitImpl<T, nonTrivialFinalizer>
::finalize(obj); } |
135 }; | 123 }; |
136 | 124 |
137 // Macros to declare and define GCInfo structures for objects | |
138 // allocated in the Blink garbage-collected heap. | |
139 #define DECLARE_GC_INFO \ | |
140 public: \ | |
141 static const GCInfo s_gcInfo; \ | |
142 template<typename Any> friend struct FinalizerTrait; \ | |
143 private: \ | |
144 | |
145 #define DEFINE_GC_INFO(Type) \ | |
146 const GCInfo Type::s_gcInfo = { \ | |
147 #Type, \ | |
148 TraceTrait<Type>::trace, \ | |
149 FinalizerTrait<Type>::finalize, \ | |
150 FinalizerTrait<Type>::nonTrivialFinalizer, \ | |
151 }; \ | |
152 | |
153 // Trait to get the GCInfo structure for types that have their | 125 // Trait to get the GCInfo structure for types that have their |
154 // instances allocated in the Blink garbage-collected heap. | 126 // instances allocated in the Blink garbage-collected heap. |
155 template<typename T> | 127 template<typename T> struct GCInfoTrait; |
156 struct GCInfoTrait { | |
157 static const GCInfo* get() | |
158 { | |
159 return &T::s_gcInfo; | |
160 } | |
161 }; | |
162 | 128 |
163 template<typename T> | 129 template<typename T> |
164 const char* getTypeMarker() | 130 const char* getTypeMarker() |
165 { | 131 { |
166 return GCInfoTrait<T>::get()->m_typeMarker; | 132 return GCInfoTrait<T>::get()->m_typeMarker; |
167 } | 133 } |
168 | 134 |
169 // The TraceTrait is used to specify how to mark an object pointer and | 135 // The TraceTrait is used to specify how to mark an object pointer and |
170 // how to trace all of the pointers in the object. | 136 // how to trace all of the pointers in the object. |
171 // | 137 // |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 | 504 |
539 template<typename T> bool ObjectAliveTrait<T>::isAlive(Visitor* visitor, T obj) | 505 template<typename T> bool ObjectAliveTrait<T>::isAlive(Visitor* visitor, T obj) |
540 { | 506 { |
541 return visitor->isMarked(obj); | 507 return visitor->isMarked(obj); |
542 } | 508 } |
543 template<typename T> bool ObjectAliveTrait<Member<T> >::isAlive(Visitor* visitor
, const Member<T>& obj) | 509 template<typename T> bool ObjectAliveTrait<Member<T> >::isAlive(Visitor* visitor
, const Member<T>& obj) |
544 { | 510 { |
545 return visitor->isMarked(obj.get()); | 511 return visitor->isMarked(obj.get()); |
546 } | 512 } |
547 | 513 |
| 514 template<typename T> |
| 515 struct GCInfoAtBase { |
| 516 static const GCInfo* get() |
| 517 { |
| 518 static char pseudoTypeMarker = 'a'; |
| 519 static const GCInfo gcInfo = { |
| 520 &pseudoTypeMarker, |
| 521 TraceTrait<T>::trace, |
| 522 FinalizerTrait<T>::finalize, |
| 523 FinalizerTrait<T>::nonTrivialFinalizer, |
| 524 }; |
| 525 return &gcInfo; |
| 526 } |
| 527 }; |
| 528 |
| 529 template<typename T> class GarbageCollected; |
| 530 template<typename T, bool = WTF::IsSubclassOfTemplate<T, GarbageCollected>::valu
e> struct GetGarbageCollectedBase; |
| 531 |
| 532 template<typename T> |
| 533 struct GetGarbageCollectedBase<T, true> { |
| 534 typedef typename T::GarbageCollectedBase type; |
| 535 }; |
| 536 |
| 537 template<typename T> |
| 538 struct GetGarbageCollectedBase<T, false> { |
| 539 typedef T type; |
| 540 }; |
| 541 |
| 542 template<typename T> |
| 543 struct GCInfoTrait { |
| 544 static const GCInfo* get() |
| 545 { |
| 546 return GCInfoAtBase<typename GetGarbageCollectedBase<T>::type>::get(); |
| 547 } |
| 548 }; |
| 549 |
548 } | 550 } |
549 | 551 |
550 #endif | 552 #endif |
OLD | NEW |