1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_OBJECTS_VISITING_H_
6 #define V8_OBJECTS_VISITING_H_
8 #include "src/allocation.h"
10 // This file provides base classes and auxiliary methods for defining
11 // static object visitors used during GC.
12 // Visiting HeapObject body with a normal ObjectVisitor requires performing
13 // two switches on object's instance type to determine object size and layout
14 // and one or more virtual method calls on visitor itself.
15 // Static visitor is different: it provides a dispatch table which contains
16 // pointers to specialized visit functions. Each map has the visitor_id
17 // field which contains an index of specialized visitor to use.
23 // Base class for all static visitors.
24 class StaticVisitorBase : public AllStatic {
26 #define VISITOR_ID_LIST(V) \
29 V(ShortcutCandidate) \
35 V(FixedFloat64Array) \
36 V(ConstantPoolArray) \
47 V(DataObjectGeneric) \
74 V(SharedFunctionInfo) \
82 // For data objects, JS objects and structs along with generic visitor which
83 // can visit object of any size we provide visitors specialized by
84 // object size in words.
85 // Ids of specialized visitors are declared in a linear order (without
86 // holes) starting from the id of visitor specialized for 2 words objects
87 // (base visitor id) and ending with the id of generic visitor.
88 // Method GetVisitorIdForSize depends on this ordering to calculate visitor
89 // id of specialized visitor from given instance size, base visitor id and
90 // generic visitor's id.
92 #define VISITOR_ID_ENUM_DECL(id) kVisit##id,
93 VISITOR_ID_LIST(VISITOR_ID_ENUM_DECL)
94 #undef VISITOR_ID_ENUM_DECL
96 kVisitDataObject = kVisitDataObject2,
97 kVisitJSObject = kVisitJSObject2,
98 kVisitStruct = kVisitStruct2,
99 kMinObjectSizeInWords = 2
102 // Visitor ID should fit in one byte.
103 STATIC_ASSERT(kVisitorIdCount <= 256);
105 // Determine which specialized visitor should be used for given instance type
106 // and instance type.
107 static VisitorId GetVisitorId(int instance_type, int instance_size);
109 static VisitorId GetVisitorId(Map* map) {
110 return GetVisitorId(map->instance_type(), map->instance_size());
113 // For visitors that allow specialization by size calculate VisitorId based
114 // on size, base visitor id and generic visitor id.
115 static VisitorId GetVisitorIdForSize(VisitorId base, VisitorId generic,
117 DCHECK((base == kVisitDataObject) || (base == kVisitStruct) ||
118 (base == kVisitJSObject));
119 DCHECK(IsAligned(object_size, kPointerSize));
120 DCHECK(kMinObjectSizeInWords * kPointerSize <= object_size);
121 DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
123 const VisitorId specialization = static_cast<VisitorId>(
124 base + (object_size >> kPointerSizeLog2) - kMinObjectSizeInWords);
126 return Min(specialization, generic);
131 template <typename Callback>
132 class VisitorDispatchTable {
134 void CopyFrom(VisitorDispatchTable* other) {
135 // We are not using memcpy to guarantee that during update
136 // every element of callbacks_ array will remain correct
137 // pointer (memcpy might be implemented as a byte copying loop).
138 for (int i = 0; i < StaticVisitorBase::kVisitorIdCount; i++) {
139 base::NoBarrier_Store(&callbacks_[i], other->callbacks_[i]);
143 inline Callback GetVisitorById(StaticVisitorBase::VisitorId id) {
144 return reinterpret_cast<Callback>(callbacks_[id]);
147 inline Callback GetVisitor(Map* map) {
148 return reinterpret_cast<Callback>(callbacks_[map->visitor_id()]);
151 void Register(StaticVisitorBase::VisitorId id, Callback callback) {
152 DCHECK(id < StaticVisitorBase::kVisitorIdCount); // id is unsigned.
153 callbacks_[id] = reinterpret_cast<base::AtomicWord>(callback);
156 template <typename Visitor, StaticVisitorBase::VisitorId base,
157 StaticVisitorBase::VisitorId generic, int object_size_in_words>
158 void RegisterSpecialization() {
159 static const int size = object_size_in_words * kPointerSize;
160 Register(StaticVisitorBase::GetVisitorIdForSize(base, generic, size),
161 &Visitor::template VisitSpecialized<size>);
165 template <typename Visitor, StaticVisitorBase::VisitorId base,
166 StaticVisitorBase::VisitorId generic>
167 void RegisterSpecializations() {
168 STATIC_ASSERT((generic - base + StaticVisitorBase::kMinObjectSizeInWords) ==
170 RegisterSpecialization<Visitor, base, generic, 2>();
171 RegisterSpecialization<Visitor, base, generic, 3>();
172 RegisterSpecialization<Visitor, base, generic, 4>();
173 RegisterSpecialization<Visitor, base, generic, 5>();
174 RegisterSpecialization<Visitor, base, generic, 6>();
175 RegisterSpecialization<Visitor, base, generic, 7>();
176 RegisterSpecialization<Visitor, base, generic, 8>();
177 RegisterSpecialization<Visitor, base, generic, 9>();
178 Register(generic, &Visitor::Visit);
182 base::AtomicWord callbacks_[StaticVisitorBase::kVisitorIdCount];
186 template <typename StaticVisitor>
187 class BodyVisitorBase : public AllStatic {
189 INLINE(static void IteratePointers(Heap* heap, HeapObject* object,
190 int start_offset, int end_offset)) {
191 Object** start_slot =
192 reinterpret_cast<Object**>(object->address() + start_offset);
194 reinterpret_cast<Object**>(object->address() + end_offset);
195 StaticVisitor::VisitPointers(heap, start_slot, end_slot);
200 template <typename StaticVisitor, typename BodyDescriptor, typename ReturnType>
201 class FlexibleBodyVisitor : public BodyVisitorBase<StaticVisitor> {
203 INLINE(static ReturnType Visit(Map* map, HeapObject* object)) {
204 int object_size = BodyDescriptor::SizeOf(map, object);
205 BodyVisitorBase<StaticVisitor>::IteratePointers(
206 map->GetHeap(), object, BodyDescriptor::kStartOffset, object_size);
207 return static_cast<ReturnType>(object_size);
210 template <int object_size>
211 static inline ReturnType VisitSpecialized(Map* map, HeapObject* object) {
212 DCHECK(BodyDescriptor::SizeOf(map, object) == object_size);
213 BodyVisitorBase<StaticVisitor>::IteratePointers(
214 map->GetHeap(), object, BodyDescriptor::kStartOffset, object_size);
215 return static_cast<ReturnType>(object_size);
220 template <typename StaticVisitor, typename BodyDescriptor, typename ReturnType>
221 class FixedBodyVisitor : public BodyVisitorBase<StaticVisitor> {
223 INLINE(static ReturnType Visit(Map* map, HeapObject* object)) {
224 BodyVisitorBase<StaticVisitor>::IteratePointers(
225 map->GetHeap(), object, BodyDescriptor::kStartOffset,
226 BodyDescriptor::kEndOffset);
227 return static_cast<ReturnType>(BodyDescriptor::kSize);
232 // Base class for visitors used for a linear new space iteration.
233 // IterateBody returns size of visited object.
234 // Certain types of objects (i.e. Code objects) are not handled
235 // by dispatch table of this visitor because they cannot appear
238 // This class is intended to be used in the following way:
240 // class SomeVisitor : public StaticNewSpaceVisitor<SomeVisitor> {
244 // This is an example of Curiously recurring template pattern
245 // (see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern).
246 // We use CRTP to guarantee aggressive compile time optimizations (i.e.
247 // inlining and specialization of StaticVisitor::VisitPointers methods).
248 template <typename StaticVisitor>
249 class StaticNewSpaceVisitor : public StaticVisitorBase {
251 static void Initialize();
253 INLINE(static int IterateBody(Map* map, HeapObject* obj)) {
254 return table_.GetVisitor(map)(map, obj);
257 INLINE(static void VisitPointers(Heap* heap, Object** start, Object** end)) {
258 for (Object** p = start; p < end; p++) StaticVisitor::VisitPointer(heap, p);
262 INLINE(static int VisitJSFunction(Map* map, HeapObject* object)) {
263 Heap* heap = map->GetHeap();
265 HeapObject::RawField(object, JSFunction::kPropertiesOffset),
266 HeapObject::RawField(object, JSFunction::kCodeEntryOffset));
268 // Don't visit code entry. We are using this visitor only during scavenges.
271 heap, HeapObject::RawField(object,
272 JSFunction::kCodeEntryOffset + kPointerSize),
273 HeapObject::RawField(object, JSFunction::kNonWeakFieldsEndOffset));
274 return JSFunction::kSize;
277 INLINE(static int VisitByteArray(Map* map, HeapObject* object)) {
278 return reinterpret_cast<ByteArray*>(object)->ByteArraySize();
281 INLINE(static int VisitFixedDoubleArray(Map* map, HeapObject* object)) {
282 int length = reinterpret_cast<FixedDoubleArray*>(object)->length();
283 return FixedDoubleArray::SizeFor(length);
286 INLINE(static int VisitFixedTypedArray(Map* map, HeapObject* object)) {
287 return reinterpret_cast<FixedTypedArrayBase*>(object)->size();
290 INLINE(static int VisitJSObject(Map* map, HeapObject* object)) {
291 return JSObjectVisitor::Visit(map, object);
294 INLINE(static int VisitSeqOneByteString(Map* map, HeapObject* object)) {
295 return SeqOneByteString::cast(object)
296 ->SeqOneByteStringSize(map->instance_type());
299 INLINE(static int VisitSeqTwoByteString(Map* map, HeapObject* object)) {
300 return SeqTwoByteString::cast(object)
301 ->SeqTwoByteStringSize(map->instance_type());
304 INLINE(static int VisitFreeSpace(Map* map, HeapObject* object)) {
305 return FreeSpace::cast(object)->Size();
308 INLINE(static int VisitJSArrayBuffer(Map* map, HeapObject* object));
309 INLINE(static int VisitJSTypedArray(Map* map, HeapObject* object));
310 INLINE(static int VisitJSDataView(Map* map, HeapObject* object));
312 class DataObjectVisitor {
314 template <int object_size>
315 static inline int VisitSpecialized(Map* map, HeapObject* object) {
319 INLINE(static int Visit(Map* map, HeapObject* object)) {
320 return map->instance_size();
324 typedef FlexibleBodyVisitor<StaticVisitor, StructBodyDescriptor, int>
327 typedef FlexibleBodyVisitor<StaticVisitor, JSObject::BodyDescriptor, int>
330 typedef int (*Callback)(Map* map, HeapObject* object);
332 static VisitorDispatchTable<Callback> table_;
336 template <typename StaticVisitor>
337 VisitorDispatchTable<typename StaticNewSpaceVisitor<StaticVisitor>::Callback>
338 StaticNewSpaceVisitor<StaticVisitor>::table_;
341 // Base class for visitors used to transitively mark the entire heap.
342 // IterateBody returns nothing.
343 // Certain types of objects might not be handled by this base class and
344 // no visitor function is registered by the generic initialization. A
345 // specialized visitor function needs to be provided by the inheriting
346 // class itself for those cases.
348 // This class is intended to be used in the following way:
350 // class SomeVisitor : public StaticMarkingVisitor<SomeVisitor> {
354 // This is an example of Curiously recurring template pattern.
355 template <typename StaticVisitor>
356 class StaticMarkingVisitor : public StaticVisitorBase {
358 static void Initialize();
360 INLINE(static void IterateBody(Map* map, HeapObject* obj)) {
361 table_.GetVisitor(map)(map, obj);
364 INLINE(static void VisitPropertyCell(Map* map, HeapObject* object));
365 INLINE(static void VisitCodeEntry(Heap* heap, Address entry_address));
366 INLINE(static void VisitEmbeddedPointer(Heap* heap, RelocInfo* rinfo));
367 INLINE(static void VisitCell(Heap* heap, RelocInfo* rinfo));
368 INLINE(static void VisitDebugTarget(Heap* heap, RelocInfo* rinfo));
369 INLINE(static void VisitCodeTarget(Heap* heap, RelocInfo* rinfo));
370 INLINE(static void VisitCodeAgeSequence(Heap* heap, RelocInfo* rinfo));
371 INLINE(static void VisitExternalReference(RelocInfo* rinfo)) {}
372 INLINE(static void VisitRuntimeEntry(RelocInfo* rinfo)) {}
373 // Skip the weak next code link in a code object.
374 INLINE(static void VisitNextCodeLink(Heap* heap, Object** slot)) {}
376 // TODO(mstarzinger): This should be made protected once refactoring is done.
377 // Mark non-optimize code for functions inlined into the given optimized
378 // code. This will prevent it from being flushed.
379 static void MarkInlinedFunctionsCode(Heap* heap, Code* code);
382 INLINE(static void VisitMap(Map* map, HeapObject* object));
383 INLINE(static void VisitCode(Map* map, HeapObject* object));
384 INLINE(static void VisitSharedFunctionInfo(Map* map, HeapObject* object));
385 INLINE(static void VisitConstantPoolArray(Map* map, HeapObject* object));
386 INLINE(static void VisitAllocationSite(Map* map, HeapObject* object));
387 INLINE(static void VisitWeakCollection(Map* map, HeapObject* object));
388 INLINE(static void VisitJSFunction(Map* map, HeapObject* object));
389 INLINE(static void VisitJSRegExp(Map* map, HeapObject* object));
390 INLINE(static void VisitJSArrayBuffer(Map* map, HeapObject* object));
391 INLINE(static void VisitJSTypedArray(Map* map, HeapObject* object));
392 INLINE(static void VisitJSDataView(Map* map, HeapObject* object));
393 INLINE(static void VisitNativeContext(Map* map, HeapObject* object));
395 // Mark pointers in a Map and its TransitionArray together, possibly
396 // treating transitions or back pointers weak.
397 static void MarkMapContents(Heap* heap, Map* map);
398 static void MarkTransitionArray(Heap* heap, TransitionArray* transitions);
400 // Code flushing support.
401 INLINE(static bool IsFlushable(Heap* heap, JSFunction* function));
402 INLINE(static bool IsFlushable(Heap* heap, SharedFunctionInfo* shared_info));
404 // Helpers used by code flushing support that visit pointer fields and treat
405 // references to code objects either strongly or weakly.
406 static void VisitSharedFunctionInfoStrongCode(Heap* heap, HeapObject* object);
407 static void VisitSharedFunctionInfoWeakCode(Heap* heap, HeapObject* object);
408 static void VisitJSFunctionStrongCode(Heap* heap, HeapObject* object);
409 static void VisitJSFunctionWeakCode(Heap* heap, HeapObject* object);
411 class DataObjectVisitor {
414 static inline void VisitSpecialized(Map* map, HeapObject* object) {}
416 INLINE(static void Visit(Map* map, HeapObject* object)) {}
419 typedef FlexibleBodyVisitor<StaticVisitor, FixedArray::BodyDescriptor, void>
422 typedef FlexibleBodyVisitor<StaticVisitor, JSObject::BodyDescriptor, void>
425 typedef FlexibleBodyVisitor<StaticVisitor, StructBodyDescriptor, void>
428 typedef void (*Callback)(Map* map, HeapObject* object);
430 static VisitorDispatchTable<Callback> table_;
434 template <typename StaticVisitor>
435 VisitorDispatchTable<typename StaticMarkingVisitor<StaticVisitor>::Callback>
436 StaticMarkingVisitor<StaticVisitor>::table_;
439 class WeakObjectRetainer;
442 // A weak list is single linked list where each element has a weak pointer to
443 // the next element. Given the head of the list, this function removes dead
444 // elements from the list and if requested records slots for next-element
445 // pointers. The template parameter T is a WeakListVisitor that defines how to
446 // access the next-element pointers.
448 Object* VisitWeakList(Heap* heap, Object* list, WeakObjectRetainer* retainer);
450 } // namespace v8::internal
452 #endif // V8_OBJECTS_VISITING_H_