heap-object.h 8.41 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_HEAP_OBJECT_H_
#define V8_OBJECTS_HEAP_OBJECT_H_

8 9
#include "src/common/globals.h"
#include "src/roots/roots.h"
10

11
#include "src/objects/objects.h"
12
#include "src/objects/tagged-field.h"
13 14 15 16 17 18 19

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

20 21
class Heap;

22 23
// HeapObject is the superclass for all classes describing heap allocated
// objects.
24
class HeapObject : public Object {
25
 public:
26 27 28
  bool is_null() const {
    return static_cast<Tagged_t>(ptr()) == static_cast<Tagged_t>(kNullAddress);
  }
29

30 31
  // [map]: Contains a map which contains the object's reflective
  // information.
32
  DECL_GETTER(map, Map)
33 34
  inline void set_map(Map value);

35
  inline ObjectSlot map_slot() const;
36 37 38 39 40 41

  // The no-write-barrier version.  This is OK if the object is white and in
  // new space, or if the value is an immortal immutable object, like the maps
  // of primitive (non-JS) objects like strings, heap numbers etc.
  inline void set_map_no_write_barrier(Map value);

42 43
  // Access the map using acquire load and release store.
  DECL_GETTER(synchronized_map, Map)
44
  inline void synchronized_set_map(Map value);
45

46 47 48 49
  // Compare-and-swaps map word using release store, returns true if the map
  // word was actually swapped.
  inline bool synchronized_compare_and_swap_map_word(MapWord old_map_word,
                                                     MapWord new_map_word);
50 51 52 53 54 55 56 57

  // Initialize the map immediately after the object is allocated.
  // Do not use this outside Heap.
  inline void set_map_after_allocation(
      Map value, WriteBarrierMode mode = UPDATE_WRITE_BARRIER);

  // During garbage collection, the map word of a heap object does not
  // necessarily contain a map pointer.
58
  DECL_GETTER(map_word, MapWord)
59 60
  inline void set_map_word(MapWord map_word);

61 62 63 64
  // Access the map word using acquire load and release store.
  DECL_GETTER(synchronized_map_word, MapWord)
  inline void synchronized_set_map_word(MapWord map_word);

65 66 67 68 69 70
  // TODO(v8:7464): Once RO_SPACE is shared between isolates, this method can be
  // removed as ReadOnlyRoots will be accessible from a global variable. For now
  // this method exists to help remove GetIsolate/GetHeap from HeapObject, in a
  // way that doesn't require passing Isolate/Heap down huge call chains or to
  // places where it might not be safe to access it.
  inline ReadOnlyRoots GetReadOnlyRoots() const;
71 72 73
  // This version is intended to be used for the isolate values produced by
  // i::GetIsolateForPtrCompr(HeapObject) function which may return nullptr.
  inline ReadOnlyRoots GetReadOnlyRoots(Isolate* isolate) const;
74

75 76 77
#define IS_TYPE_FUNCTION_DECL(Type) \
  V8_INLINE bool Is##Type() const;  \
  V8_INLINE bool Is##Type(Isolate* isolate) const;
78
  HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
79 80
  IS_TYPE_FUNCTION_DECL(HashTableBase)
  IS_TYPE_FUNCTION_DECL(SmallOrderedHashTable)
81 82
#undef IS_TYPE_FUNCTION_DECL

83
  bool IsExternal(Isolate* isolate) const;
84 85 86 87 88 89 90 91

// Oddball checks are faster when they are raw pointer comparisons, so the
// isolate/read-only roots overloads should be preferred where possible.
#define IS_TYPE_FUNCTION_DECL(Type, Value)            \
  V8_INLINE bool Is##Type(Isolate* isolate) const;    \
  V8_INLINE bool Is##Type(ReadOnlyRoots roots) const; \
  V8_INLINE bool Is##Type() const;
  ODDBALL_LIST(IS_TYPE_FUNCTION_DECL)
92
  IS_TYPE_FUNCTION_DECL(NullOrUndefined, /* unused */)
93 94
#undef IS_TYPE_FUNCTION_DECL

95 96 97
#define DECL_STRUCT_PREDICATE(NAME, Name, name) \
  V8_INLINE bool Is##Name() const;              \
  V8_INLINE bool Is##Name(Isolate* isolate) const;
98 99 100 101
  STRUCT_LIST(DECL_STRUCT_PREDICATE)
#undef DECL_STRUCT_PREDICATE

  // Converts an address to a HeapObject pointer.
102 103 104 105
  static inline HeapObject FromAddress(Address address) {
    DCHECK_TAG_ALIGNED(address);
    return HeapObject(address + kHeapObjectTag);
  }
106 107

  // Returns the address of this HeapObject.
108
  inline Address address() const { return ptr() - kHeapObjectTag; }
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

  // Iterates over pointers contained in the object (including the Map).
  // If it's not performance critical iteration use the non-templatized
  // version.
  void Iterate(ObjectVisitor* v);

  template <typename ObjectVisitor>
  inline void IterateFast(ObjectVisitor* v);

  // Iterates over all pointers contained in the object except the
  // first map pointer.  The object type is given in the first
  // parameter. This function does not access the map pointer in the
  // object, and so is safe to call while the map pointer is modified.
  // If it's not performance critical iteration use the non-templatized
  // version.
  void IterateBody(ObjectVisitor* v);
  void IterateBody(Map map, int object_size, ObjectVisitor* v);

  template <typename ObjectVisitor>
  inline void IterateBodyFast(ObjectVisitor* v);

  template <typename ObjectVisitor>
  inline void IterateBodyFast(Map map, int object_size, ObjectVisitor* v);

  // Returns true if the object contains a tagged value at given offset.
  // It is used for invalid slots filtering. If the offset points outside
  // of the object or to the map word, the result is UNDEFINED (!!!).
136
  V8_EXPORT_PRIVATE bool IsValidSlot(Map map, int offset);
137 138 139 140 141 142 143

  // Returns the heap object's size in bytes
  inline int Size() const;

  // Given a heap object's map pointer, returns the heap size in bytes
  // Useful when the map pointer field is used for other purposes.
  // GC internal.
144
  V8_EXPORT_PRIVATE int SizeFromMap(Map map) const;
145

146
  // Returns the field at offset in obj, as a read/write Object reference.
147 148 149 150 151 152
  // Does no checking, and is safe to use during GC, while maps are invalid.
  // Does not invoke write barrier, so should only be assigned to
  // during marking GC.
  inline ObjectSlot RawField(int byte_offset) const;
  inline MaybeObjectSlot RawMaybeWeakField(int byte_offset) const;

153
  DECL_CAST(HeapObject)
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

  // Return the write barrier mode for this. Callers of this function
  // must be able to present a reference to an DisallowHeapAllocation
  // object as a sign that they are not going to use this function
  // from code that allocates and thus invalidates the returned write
  // barrier mode.
  inline WriteBarrierMode GetWriteBarrierMode(
      const DisallowHeapAllocation& promise);

  // Dispatched behavior.
  void HeapObjectShortPrint(std::ostream& os);  // NOLINT
#ifdef OBJECT_PRINT
  void PrintHeader(std::ostream& os, const char* id);  // NOLINT
#endif
  DECL_PRINTER(HeapObject)
169
  EXPORT_DECL_VERIFIER(HeapObject)
170 171 172 173 174 175 176
#ifdef VERIFY_HEAP
  inline void VerifyObjectField(Isolate* isolate, int offset);
  inline void VerifySmiField(int offset);
  inline void VerifyMaybeObjectField(Isolate* isolate, int offset);

  // Verify a pointer is a valid HeapObject pointer that points to object
  // areas in the heap.
177
  static void VerifyHeapPointer(Isolate* isolate, Object p);
178 179 180 181 182 183 184
#endif

  static inline AllocationAlignment RequiredAlignment(Map map);

  // Whether the object needs rehashing. That is the case if the object's
  // content depends on FLAG_hash_seed. When the object is deserialized into
  // a heap with a different hash seed, these objects need to adapt.
185
  bool NeedsRehashing() const;
186 187 188 189 190 191 192

  // Rehashing support is not implemented for all objects that need rehashing.
  // With objects that need rehashing but cannot be rehashed, rehashing has to
  // be disabled.
  bool CanBeRehashed() const;

  // Rehash the object based on the layout inferred from its map.
193
  void RehashBasedOnMap(ReadOnlyRoots root);
194 195

  // Layout description.
196 197 198 199 200 201 202
#define HEAP_OBJECT_FIELDS(V) \
  V(kMapOffset, kTaggedSize)  \
  /* Header size. */          \
  V(kHeaderSize, 0)

  DEFINE_FIELD_OFFSET_CONSTANTS(Object::kHeaderSize, HEAP_OBJECT_FIELDS)
#undef HEAP_OBJECT_FIELDS
203 204 205

  STATIC_ASSERT(kMapOffset == Internals::kHeapObjectMapOffset);

206 207
  using MapField = TaggedField<MapWord, HeapObject::kMapOffset>;

208 209
  inline Address GetFieldAddress(int field_offset) const;

210 211 212 213
 protected:
  // Special-purpose constructor for subclasses that have fast paths where
  // their ptr() is a Smi.
  enum class AllowInlineSmiStorage { kRequireHeapObjectTag, kAllowBeingASmi };
214
  inline HeapObject(Address ptr, AllowInlineSmiStorage allow_smi);
215

216
  OBJECT_CONSTRUCTORS(HeapObject, Object);
217 218
};

219 220 221
OBJECT_CONSTRUCTORS_IMPL(HeapObject, Object)
CAST_ACCESSOR(HeapObject)

222 223 224 225 226 227
}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_HEAP_OBJECT_H_