Line data Source code
1 : // Copyright 2018 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.
4 :
5 : #ifndef V8_OBJECTS_HEAP_OBJECT_H_
6 : #define V8_OBJECTS_HEAP_OBJECT_H_
7 :
8 : #include "src/globals.h"
9 :
10 : #include "src/objects.h"
11 :
12 : // Has to be the last include (doesn't have include guards):
13 : #include "src/objects/object-macros.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 : // HeapObject is the superclass for all classes describing heap allocated
19 : // objects.
20 : class HeapObject : public Object {
21 : public:
22 140289952 : bool is_null() const { return ptr() == kNullAddress; }
23 :
24 : // [map]: Contains a map which contains the object's reflective
25 : // information.
26 : inline Map map() const;
27 : inline void set_map(Map value);
28 :
29 : inline MapWordSlot map_slot() const;
30 :
31 : // The no-write-barrier version. This is OK if the object is white and in
32 : // new space, or if the value is an immortal immutable object, like the maps
33 : // of primitive (non-JS) objects like strings, heap numbers etc.
34 : inline void set_map_no_write_barrier(Map value);
35 :
36 : // Get the map using acquire load.
37 : inline Map synchronized_map() const;
38 : inline MapWord synchronized_map_word() const;
39 :
40 : // Set the map using release store
41 : inline void synchronized_set_map(Map value);
42 : inline void synchronized_set_map_word(MapWord map_word);
43 :
44 : // Initialize the map immediately after the object is allocated.
45 : // Do not use this outside Heap.
46 : inline void set_map_after_allocation(
47 : Map value, WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
48 :
49 : // During garbage collection, the map word of a heap object does not
50 : // necessarily contain a map pointer.
51 : inline MapWord map_word() const;
52 : inline void set_map_word(MapWord map_word);
53 :
54 : // TODO(v8:7464): Once RO_SPACE is shared between isolates, this method can be
55 : // removed as ReadOnlyRoots will be accessible from a global variable. For now
56 : // this method exists to help remove GetIsolate/GetHeap from HeapObject, in a
57 : // way that doesn't require passing Isolate/Heap down huge call chains or to
58 : // places where it might not be safe to access it.
59 : inline ReadOnlyRoots GetReadOnlyRoots() const;
60 :
61 : #define IS_TYPE_FUNCTION_DECL(Type) V8_INLINE bool Is##Type() const;
62 : HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
63 : #undef IS_TYPE_FUNCTION_DECL
64 :
65 : V8_INLINE bool IsExternal(Isolate* isolate) const;
66 :
67 : // Oddball checks are faster when they are raw pointer comparisons, so the
68 : // isolate/read-only roots overloads should be preferred where possible.
69 : #define IS_TYPE_FUNCTION_DECL(Type, Value) \
70 : V8_INLINE bool Is##Type(Isolate* isolate) const; \
71 : V8_INLINE bool Is##Type(ReadOnlyRoots roots) const; \
72 : V8_INLINE bool Is##Type() const;
73 : ODDBALL_LIST(IS_TYPE_FUNCTION_DECL)
74 : #undef IS_TYPE_FUNCTION_DECL
75 :
76 : V8_INLINE bool IsNullOrUndefined(Isolate* isolate) const;
77 : V8_INLINE bool IsNullOrUndefined(ReadOnlyRoots roots) const;
78 : V8_INLINE bool IsNullOrUndefined() const;
79 :
80 : #define DECL_STRUCT_PREDICATE(NAME, Name, name) V8_INLINE bool Is##Name() const;
81 : STRUCT_LIST(DECL_STRUCT_PREDICATE)
82 : #undef DECL_STRUCT_PREDICATE
83 :
84 : // Converts an address to a HeapObject pointer.
85 : static inline HeapObject FromAddress(Address address);
86 :
87 : // Returns the address of this HeapObject.
88 3775009023 : inline Address address() const { return ptr() - kHeapObjectTag; }
89 :
90 : // Iterates over pointers contained in the object (including the Map).
91 : // If it's not performance critical iteration use the non-templatized
92 : // version.
93 : void Iterate(ObjectVisitor* v);
94 :
95 : template <typename ObjectVisitor>
96 : inline void IterateFast(ObjectVisitor* v);
97 :
98 : // Iterates over all pointers contained in the object except the
99 : // first map pointer. The object type is given in the first
100 : // parameter. This function does not access the map pointer in the
101 : // object, and so is safe to call while the map pointer is modified.
102 : // If it's not performance critical iteration use the non-templatized
103 : // version.
104 : void IterateBody(ObjectVisitor* v);
105 : void IterateBody(Map map, int object_size, ObjectVisitor* v);
106 :
107 : template <typename ObjectVisitor>
108 : inline void IterateBodyFast(ObjectVisitor* v);
109 :
110 : template <typename ObjectVisitor>
111 : inline void IterateBodyFast(Map map, int object_size, ObjectVisitor* v);
112 :
113 : // Returns true if the object contains a tagged value at given offset.
114 : // It is used for invalid slots filtering. If the offset points outside
115 : // of the object or to the map word, the result is UNDEFINED (!!!).
116 : bool IsValidSlot(Map map, int offset);
117 :
118 : // Returns the heap object's size in bytes
119 : inline int Size() const;
120 :
121 : // Given a heap object's map pointer, returns the heap size in bytes
122 : // Useful when the map pointer field is used for other purposes.
123 : // GC internal.
124 : inline int SizeFromMap(Map map) const;
125 :
126 : // Returns the field at offset in obj, as a read/write Object reference.
127 : // Does no checking, and is safe to use during GC, while maps are invalid.
128 : // Does not invoke write barrier, so should only be assigned to
129 : // during marking GC.
130 : inline ObjectSlot RawField(int byte_offset) const;
131 : static inline ObjectSlot RawField(const HeapObject obj, int offset);
132 : inline MaybeObjectSlot RawMaybeWeakField(int byte_offset) const;
133 : static inline MaybeObjectSlot RawMaybeWeakField(HeapObject obj, int offset);
134 :
135 : DECL_CAST(HeapObject)
136 :
137 : // Return the write barrier mode for this. Callers of this function
138 : // must be able to present a reference to an DisallowHeapAllocation
139 : // object as a sign that they are not going to use this function
140 : // from code that allocates and thus invalidates the returned write
141 : // barrier mode.
142 : inline WriteBarrierMode GetWriteBarrierMode(
143 : const DisallowHeapAllocation& promise);
144 :
145 : // Dispatched behavior.
146 : void HeapObjectShortPrint(std::ostream& os); // NOLINT
147 : #ifdef OBJECT_PRINT
148 : void PrintHeader(std::ostream& os, const char* id); // NOLINT
149 : #endif
150 : DECL_PRINTER(HeapObject)
151 : DECL_VERIFIER(HeapObject)
152 : #ifdef VERIFY_HEAP
153 : inline void VerifyObjectField(Isolate* isolate, int offset);
154 : inline void VerifySmiField(int offset);
155 : inline void VerifyMaybeObjectField(Isolate* isolate, int offset);
156 :
157 : // Verify a pointer is a valid HeapObject pointer that points to object
158 : // areas in the heap.
159 : static void VerifyHeapPointer(Isolate* isolate, Object p);
160 : #endif
161 :
162 : static inline AllocationAlignment RequiredAlignment(Map map);
163 :
164 : // Whether the object needs rehashing. That is the case if the object's
165 : // content depends on FLAG_hash_seed. When the object is deserialized into
166 : // a heap with a different hash seed, these objects need to adapt.
167 : inline bool NeedsRehashing() const;
168 :
169 : // Rehashing support is not implemented for all objects that need rehashing.
170 : // With objects that need rehashing but cannot be rehashed, rehashing has to
171 : // be disabled.
172 : bool CanBeRehashed() const;
173 :
174 : // Rehash the object based on the layout inferred from its map.
175 : void RehashBasedOnMap(Isolate* isolate);
176 :
177 : // Layout description.
178 : #define HEAP_OBJECT_FIELDS(V) \
179 : V(kMapOffset, kTaggedSize) \
180 : /* Header size. */ \
181 : V(kHeaderSize, 0)
182 :
183 : DEFINE_FIELD_OFFSET_CONSTANTS(Object::kHeaderSize, HEAP_OBJECT_FIELDS)
184 : #undef HEAP_OBJECT_FIELDS
185 :
186 : STATIC_ASSERT(kMapOffset == Internals::kHeapObjectMapOffset);
187 :
188 : inline Address GetFieldAddress(int field_offset) const;
189 :
190 : protected:
191 : // Special-purpose constructor for subclasses that have fast paths where
192 : // their ptr() is a Smi.
193 : enum class AllowInlineSmiStorage { kRequireHeapObjectTag, kAllowBeingASmi };
194 : inline HeapObject(Address ptr, AllowInlineSmiStorage allow_smi);
195 :
196 20754921454 : OBJECT_CONSTRUCTORS(HeapObject, Object);
197 : };
198 :
199 : // Helper class for objects that can never be in RO space.
200 : class NeverReadOnlySpaceObject {
201 : public:
202 : // The Heap the object was allocated in. Used also to access Isolate.
203 : static inline Heap* GetHeap(const HeapObject object);
204 :
205 : // Convenience method to get current isolate.
206 : static inline Isolate* GetIsolate(const HeapObject object);
207 : };
208 :
209 : } // namespace internal
210 : } // namespace v8
211 :
212 : #include "src/objects/object-macros-undef.h"
213 :
214 : #endif // V8_OBJECTS_HEAP_OBJECT_H_
|