verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 28 | #ifndef V8_OBJECTS_VISITING_H_ |
| 29 | #define V8_OBJECTS_VISITING_H_ |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 30 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
| 32 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 33 | // This file provides base classes and auxiliary methods for defining |
| 34 | // static object visitors used during GC. |
| 35 | // Visiting HeapObject body with a normal ObjectVisitor requires performing |
| 36 | // two switches on object's instance type to determine object size and layout |
| 37 | // and one or more virtual method calls on visitor itself. |
| 38 | // Static visitor is different: it provides a dispatch table which contains |
| 39 | // pointers to specialized visit functions. Each map has the visitor_id |
| 40 | // field which contains an index of specialized visitor to use. |
| 41 | |
| 42 | namespace v8 { |
| 43 | namespace internal { |
| 44 | |
| 45 | |
| 46 | // Base class for all static visitors. |
| 47 | class StaticVisitorBase : public AllStatic { |
| 48 | public: |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 49 | #define VISITOR_ID_LIST(V) \ |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 50 | V(SeqOneByteString) \ |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 51 | V(SeqTwoByteString) \ |
| 52 | V(ShortcutCandidate) \ |
| 53 | V(ByteArray) \ |
| 54 | V(FreeSpace) \ |
| 55 | V(FixedArray) \ |
| 56 | V(FixedDoubleArray) \ |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 57 | V(NativeContext) \ |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 58 | V(DataObject2) \ |
| 59 | V(DataObject3) \ |
| 60 | V(DataObject4) \ |
| 61 | V(DataObject5) \ |
| 62 | V(DataObject6) \ |
| 63 | V(DataObject7) \ |
| 64 | V(DataObject8) \ |
| 65 | V(DataObject9) \ |
| 66 | V(DataObjectGeneric) \ |
| 67 | V(JSObject2) \ |
| 68 | V(JSObject3) \ |
| 69 | V(JSObject4) \ |
| 70 | V(JSObject5) \ |
| 71 | V(JSObject6) \ |
| 72 | V(JSObject7) \ |
| 73 | V(JSObject8) \ |
| 74 | V(JSObject9) \ |
| 75 | V(JSObjectGeneric) \ |
| 76 | V(Struct2) \ |
| 77 | V(Struct3) \ |
| 78 | V(Struct4) \ |
| 79 | V(Struct5) \ |
| 80 | V(Struct6) \ |
| 81 | V(Struct7) \ |
| 82 | V(Struct8) \ |
| 83 | V(Struct9) \ |
| 84 | V(StructGeneric) \ |
| 85 | V(ConsString) \ |
| 86 | V(SlicedString) \ |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 87 | V(Symbol) \ |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 88 | V(Oddball) \ |
| 89 | V(Code) \ |
| 90 | V(Map) \ |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 91 | V(Cell) \ |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 92 | V(PropertyCell) \ |
| 93 | V(SharedFunctionInfo) \ |
| 94 | V(JSFunction) \ |
| 95 | V(JSWeakMap) \ |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 96 | V(JSArrayBuffer) \ |
| 97 | V(JSTypedArray) \ |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 98 | V(JSDataView) \ |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 99 | V(JSRegExp) |
| 100 | |
| 101 | // For data objects, JS objects and structs along with generic visitor which |
| 102 | // can visit object of any size we provide visitors specialized by |
| 103 | // object size in words. |
| 104 | // Ids of specialized visitors are declared in a linear order (without |
| 105 | // holes) starting from the id of visitor specialized for 2 words objects |
| 106 | // (base visitor id) and ending with the id of generic visitor. |
| 107 | // Method GetVisitorIdForSize depends on this ordering to calculate visitor |
| 108 | // id of specialized visitor from given instance size, base visitor id and |
| 109 | // generic visitor's id. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 110 | enum VisitorId { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 111 | #define VISITOR_ID_ENUM_DECL(id) kVisit##id, |
| 112 | VISITOR_ID_LIST(VISITOR_ID_ENUM_DECL) |
| 113 | #undef VISITOR_ID_ENUM_DECL |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 114 | kVisitorIdCount, |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 115 | kVisitDataObject = kVisitDataObject2, |
| 116 | kVisitJSObject = kVisitJSObject2, |
| 117 | kVisitStruct = kVisitStruct2, |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 118 | kMinObjectSizeInWords = 2 |
| 119 | }; |
| 120 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 121 | // Visitor ID should fit in one byte. |
| 122 | STATIC_ASSERT(kVisitorIdCount <= 256); |
| 123 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 124 | // Determine which specialized visitor should be used for given instance type |
| 125 | // and instance type. |
| 126 | static VisitorId GetVisitorId(int instance_type, int instance_size); |
| 127 | |
| 128 | static VisitorId GetVisitorId(Map* map) { |
| 129 | return GetVisitorId(map->instance_type(), map->instance_size()); |
| 130 | } |
| 131 | |
| 132 | // For visitors that allow specialization by size calculate VisitorId based |
| 133 | // on size, base visitor id and generic visitor id. |
| 134 | static VisitorId GetVisitorIdForSize(VisitorId base, |
| 135 | VisitorId generic, |
| 136 | int object_size) { |
| 137 | ASSERT((base == kVisitDataObject) || |
| 138 | (base == kVisitStruct) || |
| 139 | (base == kVisitJSObject)); |
| 140 | ASSERT(IsAligned(object_size, kPointerSize)); |
| 141 | ASSERT(kMinObjectSizeInWords * kPointerSize <= object_size); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 142 | ASSERT(object_size < Page::kMaxNonCodeHeapObjectSize); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 143 | |
| 144 | const VisitorId specialization = static_cast<VisitorId>( |
| 145 | base + (object_size >> kPointerSizeLog2) - kMinObjectSizeInWords); |
| 146 | |
| 147 | return Min(specialization, generic); |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | |
| 152 | template<typename Callback> |
| 153 | class VisitorDispatchTable { |
| 154 | public: |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 155 | void CopyFrom(VisitorDispatchTable* other) { |
| 156 | // We are not using memcpy to guarantee that during update |
| 157 | // every element of callbacks_ array will remain correct |
| 158 | // pointer (memcpy might be implemented as a byte copying loop). |
| 159 | for (int i = 0; i < StaticVisitorBase::kVisitorIdCount; i++) { |
| 160 | NoBarrier_Store(&callbacks_[i], other->callbacks_[i]); |
| 161 | } |
| 162 | } |
| 163 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 164 | inline Callback GetVisitorById(StaticVisitorBase::VisitorId id) { |
| 165 | return reinterpret_cast<Callback>(callbacks_[id]); |
| 166 | } |
| 167 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 168 | inline Callback GetVisitor(Map* map) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 169 | return reinterpret_cast<Callback>(callbacks_[map->visitor_id()]); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void Register(StaticVisitorBase::VisitorId id, Callback callback) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 173 | ASSERT(id < StaticVisitorBase::kVisitorIdCount); // id is unsigned. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 174 | callbacks_[id] = reinterpret_cast<AtomicWord>(callback); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | template<typename Visitor, |
| 178 | StaticVisitorBase::VisitorId base, |
| 179 | StaticVisitorBase::VisitorId generic, |
| 180 | int object_size_in_words> |
| 181 | void RegisterSpecialization() { |
| 182 | static const int size = object_size_in_words * kPointerSize; |
| 183 | Register(StaticVisitorBase::GetVisitorIdForSize(base, generic, size), |
| 184 | &Visitor::template VisitSpecialized<size>); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | template<typename Visitor, |
| 189 | StaticVisitorBase::VisitorId base, |
| 190 | StaticVisitorBase::VisitorId generic> |
| 191 | void RegisterSpecializations() { |
| 192 | STATIC_ASSERT( |
| 193 | (generic - base + StaticVisitorBase::kMinObjectSizeInWords) == 10); |
| 194 | RegisterSpecialization<Visitor, base, generic, 2>(); |
| 195 | RegisterSpecialization<Visitor, base, generic, 3>(); |
| 196 | RegisterSpecialization<Visitor, base, generic, 4>(); |
| 197 | RegisterSpecialization<Visitor, base, generic, 5>(); |
| 198 | RegisterSpecialization<Visitor, base, generic, 6>(); |
| 199 | RegisterSpecialization<Visitor, base, generic, 7>(); |
| 200 | RegisterSpecialization<Visitor, base, generic, 8>(); |
| 201 | RegisterSpecialization<Visitor, base, generic, 9>(); |
| 202 | Register(generic, &Visitor::Visit); |
| 203 | } |
| 204 | |
| 205 | private: |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 206 | AtomicWord callbacks_[StaticVisitorBase::kVisitorIdCount]; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | |
| 210 | template<typename StaticVisitor> |
| 211 | class BodyVisitorBase : public AllStatic { |
| 212 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 213 | INLINE(static void IteratePointers(Heap* heap, |
| 214 | HeapObject* object, |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 215 | int start_offset, |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 216 | int end_offset)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 217 | Object** start_slot = reinterpret_cast<Object**>(object->address() + |
| 218 | start_offset); |
| 219 | Object** end_slot = reinterpret_cast<Object**>(object->address() + |
| 220 | end_offset); |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 221 | StaticVisitor::VisitPointers(heap, start_slot, end_slot); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 222 | } |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | template<typename StaticVisitor, typename BodyDescriptor, typename ReturnType> |
| 227 | class FlexibleBodyVisitor : public BodyVisitorBase<StaticVisitor> { |
| 228 | public: |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 229 | INLINE(static ReturnType Visit(Map* map, HeapObject* object)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 230 | int object_size = BodyDescriptor::SizeOf(map, object); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 231 | BodyVisitorBase<StaticVisitor>::IteratePointers( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 232 | map->GetHeap(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 233 | object, |
| 234 | BodyDescriptor::kStartOffset, |
| 235 | object_size); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 236 | return static_cast<ReturnType>(object_size); |
| 237 | } |
| 238 | |
| 239 | template<int object_size> |
| 240 | static inline ReturnType VisitSpecialized(Map* map, HeapObject* object) { |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 241 | ASSERT(BodyDescriptor::SizeOf(map, object) == object_size); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 242 | BodyVisitorBase<StaticVisitor>::IteratePointers( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 243 | map->GetHeap(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 244 | object, |
| 245 | BodyDescriptor::kStartOffset, |
| 246 | object_size); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 247 | return static_cast<ReturnType>(object_size); |
| 248 | } |
| 249 | }; |
| 250 | |
| 251 | |
| 252 | template<typename StaticVisitor, typename BodyDescriptor, typename ReturnType> |
| 253 | class FixedBodyVisitor : public BodyVisitorBase<StaticVisitor> { |
| 254 | public: |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 255 | INLINE(static ReturnType Visit(Map* map, HeapObject* object)) { |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 256 | BodyVisitorBase<StaticVisitor>::IteratePointers( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 257 | map->GetHeap(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 258 | object, |
| 259 | BodyDescriptor::kStartOffset, |
| 260 | BodyDescriptor::kEndOffset); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 261 | return static_cast<ReturnType>(BodyDescriptor::kSize); |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | // Base class for visitors used for a linear new space iteration. |
| 267 | // IterateBody returns size of visited object. |
| 268 | // Certain types of objects (i.e. Code objects) are not handled |
| 269 | // by dispatch table of this visitor because they cannot appear |
| 270 | // in the new space. |
| 271 | // |
| 272 | // This class is intended to be used in the following way: |
| 273 | // |
| 274 | // class SomeVisitor : public StaticNewSpaceVisitor<SomeVisitor> { |
| 275 | // ... |
| 276 | // } |
| 277 | // |
| 278 | // This is an example of Curiously recurring template pattern |
| 279 | // (see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern). |
| 280 | // We use CRTP to guarantee aggressive compile time optimizations (i.e. |
| 281 | // inlining and specialization of StaticVisitor::VisitPointers methods). |
| 282 | template<typename StaticVisitor> |
| 283 | class StaticNewSpaceVisitor : public StaticVisitorBase { |
| 284 | public: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 285 | static void Initialize(); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 286 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 287 | INLINE(static int IterateBody(Map* map, HeapObject* obj)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 288 | return table_.GetVisitor(map)(map, obj); |
| 289 | } |
| 290 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 291 | INLINE(static void VisitPointers(Heap* heap, Object** start, Object** end)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 292 | for (Object** p = start; p < end; p++) StaticVisitor::VisitPointer(heap, p); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | private: |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 296 | INLINE(static int VisitJSFunction(Map* map, HeapObject* object)) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 297 | Heap* heap = map->GetHeap(); |
| 298 | VisitPointers(heap, |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 299 | HeapObject::RawField(object, JSFunction::kPropertiesOffset), |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 300 | HeapObject::RawField(object, JSFunction::kCodeEntryOffset)); |
| 301 | |
| 302 | // Don't visit code entry. We are using this visitor only during scavenges. |
| 303 | |
| 304 | VisitPointers( |
| 305 | heap, |
| 306 | HeapObject::RawField(object, |
| 307 | JSFunction::kCodeEntryOffset + kPointerSize), |
| 308 | HeapObject::RawField(object, |
| 309 | JSFunction::kNonWeakFieldsEndOffset)); |
| 310 | return JSFunction::kSize; |
| 311 | } |
| 312 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 313 | INLINE(static int VisitByteArray(Map* map, HeapObject* object)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 314 | return reinterpret_cast<ByteArray*>(object)->ByteArraySize(); |
| 315 | } |
| 316 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 317 | INLINE(static int VisitFixedDoubleArray(Map* map, HeapObject* object)) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 318 | int length = reinterpret_cast<FixedDoubleArray*>(object)->length(); |
| 319 | return FixedDoubleArray::SizeFor(length); |
| 320 | } |
| 321 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 322 | INLINE(static int VisitJSObject(Map* map, HeapObject* object)) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 323 | return JSObjectVisitor::Visit(map, object); |
| 324 | } |
| 325 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 326 | INLINE(static int VisitSeqOneByteString(Map* map, HeapObject* object)) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 327 | return SeqOneByteString::cast(object)-> |
| 328 | SeqOneByteStringSize(map->instance_type()); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 329 | } |
| 330 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 331 | INLINE(static int VisitSeqTwoByteString(Map* map, HeapObject* object)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 332 | return SeqTwoByteString::cast(object)-> |
| 333 | SeqTwoByteStringSize(map->instance_type()); |
| 334 | } |
| 335 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 336 | INLINE(static int VisitFreeSpace(Map* map, HeapObject* object)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 337 | return FreeSpace::cast(object)->Size(); |
| 338 | } |
| 339 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 340 | INLINE(static int VisitJSArrayBuffer(Map* map, HeapObject* object)); |
| 341 | INLINE(static int VisitJSTypedArray(Map* map, HeapObject* object)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 342 | INLINE(static int VisitJSDataView(Map* map, HeapObject* object)); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 343 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 344 | class DataObjectVisitor { |
| 345 | public: |
| 346 | template<int object_size> |
| 347 | static inline int VisitSpecialized(Map* map, HeapObject* object) { |
| 348 | return object_size; |
| 349 | } |
| 350 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 351 | INLINE(static int Visit(Map* map, HeapObject* object)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 352 | return map->instance_size(); |
| 353 | } |
| 354 | }; |
| 355 | |
| 356 | typedef FlexibleBodyVisitor<StaticVisitor, |
| 357 | StructBodyDescriptor, |
| 358 | int> StructVisitor; |
| 359 | |
| 360 | typedef FlexibleBodyVisitor<StaticVisitor, |
| 361 | JSObject::BodyDescriptor, |
| 362 | int> JSObjectVisitor; |
| 363 | |
| 364 | typedef int (*Callback)(Map* map, HeapObject* object); |
| 365 | |
| 366 | static VisitorDispatchTable<Callback> table_; |
| 367 | }; |
| 368 | |
| 369 | |
| 370 | template<typename StaticVisitor> |
| 371 | VisitorDispatchTable<typename StaticNewSpaceVisitor<StaticVisitor>::Callback> |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 372 | StaticNewSpaceVisitor<StaticVisitor>::table_; |
| 373 | |
| 374 | |
| 375 | // Base class for visitors used to transitively mark the entire heap. |
| 376 | // IterateBody returns nothing. |
| 377 | // Certain types of objects might not be handled by this base class and |
| 378 | // no visitor function is registered by the generic initialization. A |
| 379 | // specialized visitor function needs to be provided by the inheriting |
| 380 | // class itself for those cases. |
| 381 | // |
| 382 | // This class is intended to be used in the following way: |
| 383 | // |
| 384 | // class SomeVisitor : public StaticMarkingVisitor<SomeVisitor> { |
| 385 | // ... |
| 386 | // } |
| 387 | // |
| 388 | // This is an example of Curiously recurring template pattern. |
| 389 | template<typename StaticVisitor> |
| 390 | class StaticMarkingVisitor : public StaticVisitorBase { |
| 391 | public: |
| 392 | static void Initialize(); |
| 393 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 394 | INLINE(static void IterateBody(Map* map, HeapObject* obj)) { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 395 | table_.GetVisitor(map)(map, obj); |
| 396 | } |
| 397 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 398 | INLINE(static void VisitPropertyCell(Map* map, HeapObject* object)); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 399 | INLINE(static void VisitCodeEntry(Heap* heap, Address entry_address)); |
| 400 | INLINE(static void VisitEmbeddedPointer(Heap* heap, RelocInfo* rinfo)); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 401 | INLINE(static void VisitCell(Heap* heap, RelocInfo* rinfo)); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 402 | INLINE(static void VisitDebugTarget(Heap* heap, RelocInfo* rinfo)); |
| 403 | INLINE(static void VisitCodeTarget(Heap* heap, RelocInfo* rinfo)); |
| 404 | INLINE(static void VisitCodeAgeSequence(Heap* heap, RelocInfo* rinfo)); |
| 405 | INLINE(static void VisitExternalReference(RelocInfo* rinfo)) { } |
| 406 | INLINE(static void VisitRuntimeEntry(RelocInfo* rinfo)) { } |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 407 | |
| 408 | // TODO(mstarzinger): This should be made protected once refactoring is done. |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 409 | // Mark non-optimize code for functions inlined into the given optimized |
| 410 | // code. This will prevent it from being flushed. |
| 411 | static void MarkInlinedFunctionsCode(Heap* heap, Code* code); |
| 412 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 413 | protected: |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 414 | INLINE(static void VisitMap(Map* map, HeapObject* object)); |
| 415 | INLINE(static void VisitCode(Map* map, HeapObject* object)); |
| 416 | INLINE(static void VisitSharedFunctionInfo(Map* map, HeapObject* object)); |
| 417 | INLINE(static void VisitJSFunction(Map* map, HeapObject* object)); |
| 418 | INLINE(static void VisitJSRegExp(Map* map, HeapObject* object)); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 419 | INLINE(static void VisitJSArrayBuffer(Map* map, HeapObject* object)); |
| 420 | INLINE(static void VisitJSTypedArray(Map* map, HeapObject* object)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 421 | INLINE(static void VisitJSDataView(Map* map, HeapObject* object)); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 422 | INLINE(static void VisitNativeContext(Map* map, HeapObject* object)); |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 423 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 424 | // Mark pointers in a Map and its TransitionArray together, possibly |
| 425 | // treating transitions or back pointers weak. |
| 426 | static void MarkMapContents(Heap* heap, Map* map); |
| 427 | static void MarkTransitionArray(Heap* heap, TransitionArray* transitions); |
| 428 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 429 | // Code flushing support. |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 430 | INLINE(static bool IsFlushable(Heap* heap, JSFunction* function)); |
| 431 | INLINE(static bool IsFlushable(Heap* heap, SharedFunctionInfo* shared_info)); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 432 | |
| 433 | // Helpers used by code flushing support that visit pointer fields and treat |
| 434 | // references to code objects either strongly or weakly. |
| 435 | static void VisitSharedFunctionInfoStrongCode(Heap* heap, HeapObject* object); |
| 436 | static void VisitSharedFunctionInfoWeakCode(Heap* heap, HeapObject* object); |
| 437 | static void VisitJSFunctionStrongCode(Heap* heap, HeapObject* object); |
| 438 | static void VisitJSFunctionWeakCode(Heap* heap, HeapObject* object); |
| 439 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 440 | class DataObjectVisitor { |
| 441 | public: |
| 442 | template<int size> |
| 443 | static inline void VisitSpecialized(Map* map, HeapObject* object) { |
| 444 | } |
| 445 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 446 | INLINE(static void Visit(Map* map, HeapObject* object)) { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 447 | } |
| 448 | }; |
| 449 | |
| 450 | typedef FlexibleBodyVisitor<StaticVisitor, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 451 | FixedArray::BodyDescriptor, |
| 452 | void> FixedArrayVisitor; |
| 453 | |
| 454 | typedef FlexibleBodyVisitor<StaticVisitor, |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 455 | JSObject::BodyDescriptor, |
| 456 | void> JSObjectVisitor; |
| 457 | |
| 458 | typedef FlexibleBodyVisitor<StaticVisitor, |
| 459 | StructBodyDescriptor, |
| 460 | void> StructObjectVisitor; |
| 461 | |
| 462 | typedef void (*Callback)(Map* map, HeapObject* object); |
| 463 | |
| 464 | static VisitorDispatchTable<Callback> table_; |
| 465 | }; |
| 466 | |
| 467 | |
| 468 | template<typename StaticVisitor> |
| 469 | VisitorDispatchTable<typename StaticMarkingVisitor<StaticVisitor>::Callback> |
| 470 | StaticMarkingVisitor<StaticVisitor>::table_; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 471 | |
| 472 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 473 | } } // namespace v8::internal |
| 474 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 475 | #endif // V8_OBJECTS_VISITING_H_ |