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) \ |
| 87 | V(Oddball) \ |
| 88 | V(Code) \ |
| 89 | V(Map) \ |
| 90 | V(PropertyCell) \ |
| 91 | V(SharedFunctionInfo) \ |
| 92 | V(JSFunction) \ |
| 93 | V(JSWeakMap) \ |
| 94 | V(JSRegExp) |
| 95 | |
| 96 | // For data objects, JS objects and structs along with generic visitor which |
| 97 | // can visit object of any size we provide visitors specialized by |
| 98 | // object size in words. |
| 99 | // Ids of specialized visitors are declared in a linear order (without |
| 100 | // holes) starting from the id of visitor specialized for 2 words objects |
| 101 | // (base visitor id) and ending with the id of generic visitor. |
| 102 | // Method GetVisitorIdForSize depends on this ordering to calculate visitor |
| 103 | // id of specialized visitor from given instance size, base visitor id and |
| 104 | // generic visitor's id. |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 105 | enum VisitorId { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 106 | #define VISITOR_ID_ENUM_DECL(id) kVisit##id, |
| 107 | VISITOR_ID_LIST(VISITOR_ID_ENUM_DECL) |
| 108 | #undef VISITOR_ID_ENUM_DECL |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 109 | kVisitorIdCount, |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 110 | kVisitDataObject = kVisitDataObject2, |
| 111 | kVisitJSObject = kVisitJSObject2, |
| 112 | kVisitStruct = kVisitStruct2, |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 113 | kMinObjectSizeInWords = 2 |
| 114 | }; |
| 115 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 116 | // Visitor ID should fit in one byte. |
| 117 | STATIC_ASSERT(kVisitorIdCount <= 256); |
| 118 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 119 | // Determine which specialized visitor should be used for given instance type |
| 120 | // and instance type. |
| 121 | static VisitorId GetVisitorId(int instance_type, int instance_size); |
| 122 | |
| 123 | static VisitorId GetVisitorId(Map* map) { |
| 124 | return GetVisitorId(map->instance_type(), map->instance_size()); |
| 125 | } |
| 126 | |
| 127 | // For visitors that allow specialization by size calculate VisitorId based |
| 128 | // on size, base visitor id and generic visitor id. |
| 129 | static VisitorId GetVisitorIdForSize(VisitorId base, |
| 130 | VisitorId generic, |
| 131 | int object_size) { |
| 132 | ASSERT((base == kVisitDataObject) || |
| 133 | (base == kVisitStruct) || |
| 134 | (base == kVisitJSObject)); |
| 135 | ASSERT(IsAligned(object_size, kPointerSize)); |
| 136 | ASSERT(kMinObjectSizeInWords * kPointerSize <= object_size); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 137 | ASSERT(object_size < Page::kMaxNonCodeHeapObjectSize); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 138 | |
| 139 | const VisitorId specialization = static_cast<VisitorId>( |
| 140 | base + (object_size >> kPointerSizeLog2) - kMinObjectSizeInWords); |
| 141 | |
| 142 | return Min(specialization, generic); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | template<typename Callback> |
| 148 | class VisitorDispatchTable { |
| 149 | public: |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 150 | void CopyFrom(VisitorDispatchTable* other) { |
| 151 | // We are not using memcpy to guarantee that during update |
| 152 | // every element of callbacks_ array will remain correct |
| 153 | // pointer (memcpy might be implemented as a byte copying loop). |
| 154 | for (int i = 0; i < StaticVisitorBase::kVisitorIdCount; i++) { |
| 155 | NoBarrier_Store(&callbacks_[i], other->callbacks_[i]); |
| 156 | } |
| 157 | } |
| 158 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 159 | inline Callback GetVisitorById(StaticVisitorBase::VisitorId id) { |
| 160 | return reinterpret_cast<Callback>(callbacks_[id]); |
| 161 | } |
| 162 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 163 | inline Callback GetVisitor(Map* map) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 164 | return reinterpret_cast<Callback>(callbacks_[map->visitor_id()]); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void Register(StaticVisitorBase::VisitorId id, Callback callback) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 168 | ASSERT(id < StaticVisitorBase::kVisitorIdCount); // id is unsigned. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 169 | callbacks_[id] = reinterpret_cast<AtomicWord>(callback); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | template<typename Visitor, |
| 173 | StaticVisitorBase::VisitorId base, |
| 174 | StaticVisitorBase::VisitorId generic, |
| 175 | int object_size_in_words> |
| 176 | void RegisterSpecialization() { |
| 177 | static const int size = object_size_in_words * kPointerSize; |
| 178 | Register(StaticVisitorBase::GetVisitorIdForSize(base, generic, size), |
| 179 | &Visitor::template VisitSpecialized<size>); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | template<typename Visitor, |
| 184 | StaticVisitorBase::VisitorId base, |
| 185 | StaticVisitorBase::VisitorId generic> |
| 186 | void RegisterSpecializations() { |
| 187 | STATIC_ASSERT( |
| 188 | (generic - base + StaticVisitorBase::kMinObjectSizeInWords) == 10); |
| 189 | RegisterSpecialization<Visitor, base, generic, 2>(); |
| 190 | RegisterSpecialization<Visitor, base, generic, 3>(); |
| 191 | RegisterSpecialization<Visitor, base, generic, 4>(); |
| 192 | RegisterSpecialization<Visitor, base, generic, 5>(); |
| 193 | RegisterSpecialization<Visitor, base, generic, 6>(); |
| 194 | RegisterSpecialization<Visitor, base, generic, 7>(); |
| 195 | RegisterSpecialization<Visitor, base, generic, 8>(); |
| 196 | RegisterSpecialization<Visitor, base, generic, 9>(); |
| 197 | Register(generic, &Visitor::Visit); |
| 198 | } |
| 199 | |
| 200 | private: |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 201 | AtomicWord callbacks_[StaticVisitorBase::kVisitorIdCount]; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | |
| 205 | template<typename StaticVisitor> |
| 206 | class BodyVisitorBase : public AllStatic { |
| 207 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 208 | INLINE(static void IteratePointers(Heap* heap, |
| 209 | HeapObject* object, |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 210 | int start_offset, |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 211 | int end_offset)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 212 | Object** start_slot = reinterpret_cast<Object**>(object->address() + |
| 213 | start_offset); |
| 214 | Object** end_slot = reinterpret_cast<Object**>(object->address() + |
| 215 | end_offset); |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 216 | StaticVisitor::VisitPointers(heap, start_slot, end_slot); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 217 | } |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | template<typename StaticVisitor, typename BodyDescriptor, typename ReturnType> |
| 222 | class FlexibleBodyVisitor : public BodyVisitorBase<StaticVisitor> { |
| 223 | public: |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 224 | INLINE(static ReturnType Visit(Map* map, HeapObject* object)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 225 | int object_size = BodyDescriptor::SizeOf(map, object); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 226 | BodyVisitorBase<StaticVisitor>::IteratePointers( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 227 | map->GetHeap(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 228 | object, |
| 229 | BodyDescriptor::kStartOffset, |
| 230 | object_size); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 231 | return static_cast<ReturnType>(object_size); |
| 232 | } |
| 233 | |
| 234 | template<int object_size> |
| 235 | static inline ReturnType VisitSpecialized(Map* map, HeapObject* object) { |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 236 | ASSERT(BodyDescriptor::SizeOf(map, object) == object_size); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 237 | BodyVisitorBase<StaticVisitor>::IteratePointers( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 238 | map->GetHeap(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 239 | object, |
| 240 | BodyDescriptor::kStartOffset, |
| 241 | object_size); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 242 | return static_cast<ReturnType>(object_size); |
| 243 | } |
| 244 | }; |
| 245 | |
| 246 | |
| 247 | template<typename StaticVisitor, typename BodyDescriptor, typename ReturnType> |
| 248 | class FixedBodyVisitor : public BodyVisitorBase<StaticVisitor> { |
| 249 | public: |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 250 | INLINE(static ReturnType Visit(Map* map, HeapObject* object)) { |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 251 | BodyVisitorBase<StaticVisitor>::IteratePointers( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 252 | map->GetHeap(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 253 | object, |
| 254 | BodyDescriptor::kStartOffset, |
| 255 | BodyDescriptor::kEndOffset); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 256 | return static_cast<ReturnType>(BodyDescriptor::kSize); |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | |
| 261 | // Base class for visitors used for a linear new space iteration. |
| 262 | // IterateBody returns size of visited object. |
| 263 | // Certain types of objects (i.e. Code objects) are not handled |
| 264 | // by dispatch table of this visitor because they cannot appear |
| 265 | // in the new space. |
| 266 | // |
| 267 | // This class is intended to be used in the following way: |
| 268 | // |
| 269 | // class SomeVisitor : public StaticNewSpaceVisitor<SomeVisitor> { |
| 270 | // ... |
| 271 | // } |
| 272 | // |
| 273 | // This is an example of Curiously recurring template pattern |
| 274 | // (see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern). |
| 275 | // We use CRTP to guarantee aggressive compile time optimizations (i.e. |
| 276 | // inlining and specialization of StaticVisitor::VisitPointers methods). |
| 277 | template<typename StaticVisitor> |
| 278 | class StaticNewSpaceVisitor : public StaticVisitorBase { |
| 279 | public: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 280 | static void Initialize(); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 281 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 282 | INLINE(static int IterateBody(Map* map, HeapObject* obj)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 283 | return table_.GetVisitor(map)(map, obj); |
| 284 | } |
| 285 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 286 | INLINE(static void VisitPointers(Heap* heap, Object** start, Object** end)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 287 | for (Object** p = start; p < end; p++) StaticVisitor::VisitPointer(heap, p); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | private: |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 291 | INLINE(static int VisitJSFunction(Map* map, HeapObject* object)) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 292 | Heap* heap = map->GetHeap(); |
| 293 | VisitPointers(heap, |
danno@chromium.org | 2ab0c3b | 2012-10-05 08:50:56 +0000 | [diff] [blame] | 294 | HeapObject::RawField(object, JSFunction::kPropertiesOffset), |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 295 | HeapObject::RawField(object, JSFunction::kCodeEntryOffset)); |
| 296 | |
| 297 | // Don't visit code entry. We are using this visitor only during scavenges. |
| 298 | |
| 299 | VisitPointers( |
| 300 | heap, |
| 301 | HeapObject::RawField(object, |
| 302 | JSFunction::kCodeEntryOffset + kPointerSize), |
| 303 | HeapObject::RawField(object, |
| 304 | JSFunction::kNonWeakFieldsEndOffset)); |
| 305 | return JSFunction::kSize; |
| 306 | } |
| 307 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 308 | INLINE(static int VisitByteArray(Map* map, HeapObject* object)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 309 | return reinterpret_cast<ByteArray*>(object)->ByteArraySize(); |
| 310 | } |
| 311 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 312 | INLINE(static int VisitFixedDoubleArray(Map* map, HeapObject* object)) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 313 | int length = reinterpret_cast<FixedDoubleArray*>(object)->length(); |
| 314 | return FixedDoubleArray::SizeFor(length); |
| 315 | } |
| 316 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 317 | INLINE(static int VisitJSObject(Map* map, HeapObject* object)) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 318 | return JSObjectVisitor::Visit(map, object); |
| 319 | } |
| 320 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 321 | INLINE(static int VisitSeqOneByteString(Map* map, HeapObject* object)) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 322 | return SeqOneByteString::cast(object)-> |
| 323 | SeqOneByteStringSize(map->instance_type()); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 324 | } |
| 325 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 326 | INLINE(static int VisitSeqTwoByteString(Map* map, HeapObject* object)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 327 | return SeqTwoByteString::cast(object)-> |
| 328 | SeqTwoByteStringSize(map->instance_type()); |
| 329 | } |
| 330 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 331 | INLINE(static int VisitFreeSpace(Map* map, HeapObject* object)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 332 | return FreeSpace::cast(object)->Size(); |
| 333 | } |
| 334 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 335 | class DataObjectVisitor { |
| 336 | public: |
| 337 | template<int object_size> |
| 338 | static inline int VisitSpecialized(Map* map, HeapObject* object) { |
| 339 | return object_size; |
| 340 | } |
| 341 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 342 | INLINE(static int Visit(Map* map, HeapObject* object)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 343 | return map->instance_size(); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | typedef FlexibleBodyVisitor<StaticVisitor, |
| 348 | StructBodyDescriptor, |
| 349 | int> StructVisitor; |
| 350 | |
| 351 | typedef FlexibleBodyVisitor<StaticVisitor, |
| 352 | JSObject::BodyDescriptor, |
| 353 | int> JSObjectVisitor; |
| 354 | |
| 355 | typedef int (*Callback)(Map* map, HeapObject* object); |
| 356 | |
| 357 | static VisitorDispatchTable<Callback> table_; |
| 358 | }; |
| 359 | |
| 360 | |
| 361 | template<typename StaticVisitor> |
| 362 | VisitorDispatchTable<typename StaticNewSpaceVisitor<StaticVisitor>::Callback> |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 363 | StaticNewSpaceVisitor<StaticVisitor>::table_; |
| 364 | |
| 365 | |
| 366 | // Base class for visitors used to transitively mark the entire heap. |
| 367 | // IterateBody returns nothing. |
| 368 | // Certain types of objects might not be handled by this base class and |
| 369 | // no visitor function is registered by the generic initialization. A |
| 370 | // specialized visitor function needs to be provided by the inheriting |
| 371 | // class itself for those cases. |
| 372 | // |
| 373 | // This class is intended to be used in the following way: |
| 374 | // |
| 375 | // class SomeVisitor : public StaticMarkingVisitor<SomeVisitor> { |
| 376 | // ... |
| 377 | // } |
| 378 | // |
| 379 | // This is an example of Curiously recurring template pattern. |
| 380 | template<typename StaticVisitor> |
| 381 | class StaticMarkingVisitor : public StaticVisitorBase { |
| 382 | public: |
| 383 | static void Initialize(); |
| 384 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 385 | INLINE(static void IterateBody(Map* map, HeapObject* obj)) { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 386 | table_.GetVisitor(map)(map, obj); |
| 387 | } |
| 388 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 389 | INLINE(static void VisitCodeEntry(Heap* heap, Address entry_address)); |
| 390 | INLINE(static void VisitEmbeddedPointer(Heap* heap, RelocInfo* rinfo)); |
| 391 | INLINE(static void VisitGlobalPropertyCell(Heap* heap, RelocInfo* rinfo)); |
| 392 | INLINE(static void VisitDebugTarget(Heap* heap, RelocInfo* rinfo)); |
| 393 | INLINE(static void VisitCodeTarget(Heap* heap, RelocInfo* rinfo)); |
| 394 | INLINE(static void VisitCodeAgeSequence(Heap* heap, RelocInfo* rinfo)); |
| 395 | INLINE(static void VisitExternalReference(RelocInfo* rinfo)) { } |
| 396 | INLINE(static void VisitRuntimeEntry(RelocInfo* rinfo)) { } |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 397 | |
| 398 | // TODO(mstarzinger): This should be made protected once refactoring is done. |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 399 | // Mark non-optimize code for functions inlined into the given optimized |
| 400 | // code. This will prevent it from being flushed. |
| 401 | static void MarkInlinedFunctionsCode(Heap* heap, Code* code); |
| 402 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 403 | protected: |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 404 | INLINE(static void VisitMap(Map* map, HeapObject* object)); |
| 405 | INLINE(static void VisitCode(Map* map, HeapObject* object)); |
| 406 | INLINE(static void VisitSharedFunctionInfo(Map* map, HeapObject* object)); |
| 407 | INLINE(static void VisitJSFunction(Map* map, HeapObject* object)); |
| 408 | INLINE(static void VisitJSRegExp(Map* map, HeapObject* object)); |
| 409 | INLINE(static void VisitNativeContext(Map* map, HeapObject* object)); |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 410 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 411 | // Mark pointers in a Map and its TransitionArray together, possibly |
| 412 | // treating transitions or back pointers weak. |
| 413 | static void MarkMapContents(Heap* heap, Map* map); |
| 414 | static void MarkTransitionArray(Heap* heap, TransitionArray* transitions); |
| 415 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 416 | // Code flushing support. |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 417 | INLINE(static bool IsFlushable(Heap* heap, JSFunction* function)); |
| 418 | INLINE(static bool IsFlushable(Heap* heap, SharedFunctionInfo* shared_info)); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 419 | |
| 420 | // Helpers used by code flushing support that visit pointer fields and treat |
| 421 | // references to code objects either strongly or weakly. |
| 422 | static void VisitSharedFunctionInfoStrongCode(Heap* heap, HeapObject* object); |
| 423 | static void VisitSharedFunctionInfoWeakCode(Heap* heap, HeapObject* object); |
| 424 | static void VisitJSFunctionStrongCode(Heap* heap, HeapObject* object); |
| 425 | static void VisitJSFunctionWeakCode(Heap* heap, HeapObject* object); |
| 426 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 427 | class DataObjectVisitor { |
| 428 | public: |
| 429 | template<int size> |
| 430 | static inline void VisitSpecialized(Map* map, HeapObject* object) { |
| 431 | } |
| 432 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 433 | INLINE(static void Visit(Map* map, HeapObject* object)) { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 434 | } |
| 435 | }; |
| 436 | |
| 437 | typedef FlexibleBodyVisitor<StaticVisitor, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 438 | FixedArray::BodyDescriptor, |
| 439 | void> FixedArrayVisitor; |
| 440 | |
| 441 | typedef FlexibleBodyVisitor<StaticVisitor, |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 442 | JSObject::BodyDescriptor, |
| 443 | void> JSObjectVisitor; |
| 444 | |
| 445 | typedef FlexibleBodyVisitor<StaticVisitor, |
| 446 | StructBodyDescriptor, |
| 447 | void> StructObjectVisitor; |
| 448 | |
| 449 | typedef void (*Callback)(Map* map, HeapObject* object); |
| 450 | |
| 451 | static VisitorDispatchTable<Callback> table_; |
| 452 | }; |
| 453 | |
| 454 | |
| 455 | template<typename StaticVisitor> |
| 456 | VisitorDispatchTable<typename StaticMarkingVisitor<StaticVisitor>::Callback> |
| 457 | StaticMarkingVisitor<StaticVisitor>::table_; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 458 | |
| 459 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 460 | } } // namespace v8::internal |
| 461 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 462 | #endif // V8_OBJECTS_VISITING_H_ |