slots.h 9.36 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_SLOTS_H_
#define V8_OBJECTS_SLOTS_H_

8
#include "src/base/memory.h"
9
#include "src/common/globals.h"
10 11 12 13

namespace v8 {
namespace internal {

14
class Object;
15

16 17
template <typename Subclass, typename Data,
          size_t SlotDataAlignment = sizeof(Data)>
18 19
class SlotBase {
 public:
20
  using TData = Data;
21

22 23
  static constexpr size_t kSlotDataSize = sizeof(Data);
  static constexpr size_t kSlotDataAlignment = SlotDataAlignment;
24

25
  Subclass& operator++() {  // Prefix increment.
26
    ptr_ += kSlotDataSize;
27 28
    return *static_cast<Subclass*>(this);
  }
29 30
  Subclass operator++(int) {  // Postfix increment.
    Subclass result = *static_cast<Subclass*>(this);
31
    ptr_ += kSlotDataSize;
32 33 34
    return result;
  }
  Subclass& operator--() {  // Prefix decrement.
35
    ptr_ -= kSlotDataSize;
36 37 38 39
    return *static_cast<Subclass*>(this);
  }
  Subclass operator--(int) {  // Postfix decrement.
    Subclass result = *static_cast<Subclass*>(this);
40
    ptr_ -= kSlotDataSize;
41 42
    return result;
  }
43

44 45
  bool operator<(const SlotBase& other) const { return ptr_ < other.ptr_; }
  bool operator<=(const SlotBase& other) const { return ptr_ <= other.ptr_; }
46 47
  bool operator>(const SlotBase& other) const { return ptr_ > other.ptr_; }
  bool operator>=(const SlotBase& other) const { return ptr_ >= other.ptr_; }
48 49 50
  bool operator==(const SlotBase& other) const { return ptr_ == other.ptr_; }
  bool operator!=(const SlotBase& other) const { return ptr_ != other.ptr_; }
  size_t operator-(const SlotBase& other) const {
51
    DCHECK_GE(ptr_, other.ptr_);
52
    return static_cast<size_t>((ptr_ - other.ptr_) / kSlotDataSize);
53
  }
54 55
  Subclass operator-(int i) const { return Subclass(ptr_ - i * kSlotDataSize); }
  Subclass operator+(int i) const { return Subclass(ptr_ + i * kSlotDataSize); }
56
  friend Subclass operator+(int i, const Subclass& slot) {
57
    return Subclass(slot.ptr_ + i * kSlotDataSize);
58
  }
59
  Subclass& operator+=(int i) {
60
    ptr_ += i * kSlotDataSize;
61 62
    return *static_cast<Subclass*>(this);
  }
63
  Subclass operator-(int i) { return Subclass(ptr_ - i * kSlotDataSize); }
64
  Subclass& operator-=(int i) {
65
    ptr_ -= i * kSlotDataSize;
66 67
    return *static_cast<Subclass*>(this);
  }
68

69 70
  void* ToVoidPtr() const { return reinterpret_cast<void*>(address()); }

71
  Address address() const { return ptr_; }
72
  // For symmetry with Handle.
73
  TData* location() const { return reinterpret_cast<TData*>(ptr_); }
74 75 76

 protected:
  explicit SlotBase(Address ptr) : ptr_(ptr) {
77
    DCHECK(IsAligned(ptr, kSlotDataAlignment));
78 79 80 81 82 83 84 85 86
  }

 private:
  // This field usually describes an on-heap address (a slot within an object),
  // so its type should not be a pointer to another C++ wrapper class.
  // Type safety is provided by well-defined conversion operations.
  Address ptr_;
};

87 88
// An FullObjectSlot instance describes a kSystemPointerSize-sized field
// ("slot") holding a tagged pointer (smi or strong heap object).
89 90
// Its address() is the address of the slot.
// The slot's contents can be read and written using operator* and store().
91
class FullObjectSlot : public SlotBase<FullObjectSlot, Address> {
92
 public:
93
  using TObject = Object;
94
  using THeapObjectSlot = FullHeapObjectSlot;
95 96

  // Tagged value stored in this slot is guaranteed to never be a weak pointer.
97
  static constexpr bool kCanBeWeak = false;
98

99 100
  FullObjectSlot() : SlotBase(kNullAddress) {}
  explicit FullObjectSlot(Address ptr) : SlotBase(ptr) {}
101
  explicit FullObjectSlot(const Address* ptr)
102
      : SlotBase(reinterpret_cast<Address>(ptr)) {}
103
  inline explicit FullObjectSlot(Object* object);
104
  template <typename T>
105
  explicit FullObjectSlot(SlotBase<T, TData, kSlotDataAlignment> slot)
106
      : SlotBase(slot.address()) {}
107

108 109 110 111
  // Compares memory representation of a value stored in the slot with given
  // raw value.
  inline bool contains_value(Address raw_value) const;

112
  inline const Object operator*() const;
113
  inline void store(Object value) const;
114

115 116 117 118
  inline Object Acquire_Load() const;
  inline Object Relaxed_Load() const;
  inline void Relaxed_Store(Object value) const;
  inline void Release_Store(Object value) const;
119
  inline Object Relaxed_CompareAndSwap(Object old, Object target) const;
120
  inline Object Release_CompareAndSwap(Object old, Object target) const;
121 122
};

123 124
// A FullMaybeObjectSlot instance describes a kSystemPointerSize-sized field
// ("slot") holding a possibly-weak tagged pointer (think: MaybeObject).
125 126
// Its address() is the address of the slot.
// The slot's contents can be read and written using operator* and store().
127 128
class FullMaybeObjectSlot
    : public SlotBase<FullMaybeObjectSlot, Address, kSystemPointerSize> {
129
 public:
130
  using TObject = MaybeObject;
131
  using THeapObjectSlot = FullHeapObjectSlot;
132 133

  // Tagged value stored in this slot can be a weak pointer.
134
  static constexpr bool kCanBeWeak = true;
135

136 137
  FullMaybeObjectSlot() : SlotBase(kNullAddress) {}
  explicit FullMaybeObjectSlot(Address ptr) : SlotBase(ptr) {}
138
  explicit FullMaybeObjectSlot(Object* ptr)
139
      : SlotBase(reinterpret_cast<Address>(ptr)) {}
140 141
  explicit FullMaybeObjectSlot(MaybeObject* ptr)
      : SlotBase(reinterpret_cast<Address>(ptr)) {}
142
  template <typename T>
143
  explicit FullMaybeObjectSlot(SlotBase<T, TData, kSlotDataAlignment> slot)
144
      : SlotBase(slot.address()) {}
145

146
  inline const MaybeObject operator*() const;
147
  inline void store(MaybeObject value) const;
148

149
  inline MaybeObject Relaxed_Load() const;
150
  inline void Relaxed_Store(MaybeObject value) const;
151
  inline void Release_CompareAndSwap(MaybeObject old, MaybeObject target) const;
152 153
};

154 155
// A FullHeapObjectSlot instance describes a kSystemPointerSize-sized field
// ("slot") holding a weak or strong pointer to a heap object (think:
156
// HeapObjectReference).
157 158 159 160
// Its address() is the address of the slot.
// The slot's contents can be read and written using operator* and store().
// In case it is known that that slot contains a strong heap object pointer,
// ToHeapObject() can be used to retrieve that heap object.
161
class FullHeapObjectSlot : public SlotBase<FullHeapObjectSlot, Address> {
162
 public:
163 164
  FullHeapObjectSlot() : SlotBase(kNullAddress) {}
  explicit FullHeapObjectSlot(Address ptr) : SlotBase(ptr) {}
165
  explicit FullHeapObjectSlot(Object* ptr)
166
      : SlotBase(reinterpret_cast<Address>(ptr)) {}
167
  template <typename T>
168
  explicit FullHeapObjectSlot(SlotBase<T, TData, kSlotDataAlignment> slot)
169
      : SlotBase(slot.address()) {}
170

171
  inline const HeapObjectReference operator*() const;
172
  inline void store(HeapObjectReference value) const;
173

174
  inline HeapObject ToHeapObject() const;
175

176
  inline void StoreHeapObject(HeapObject value) const;
177 178
};

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
// TODO(ishell, v8:8875): When pointer compression is enabled the [u]intptr_t
// and double fields are only kTaggedSize aligned so in order to avoid undefined
// behavior in C++ code we use this iterator adaptor when using STL algorithms
// with unaligned pointers.
// It will be removed once all v8:8875 is fixed and all the full pointer and
// double values in compressed V8 heap are properly aligned.
template <typename T>
class UnalignedSlot : public SlotBase<UnalignedSlot<T>, T, 1> {
 public:
  // This class is a stand-in for "T&" that uses custom read/write operations
  // for the actual memory accesses.
  class Reference {
   public:
    explicit Reference(Address address) : address_(address) {}
    Reference(const Reference&) V8_NOEXCEPT = default;

    Reference& operator=(const Reference& other) V8_NOEXCEPT {
196
      base::WriteUnalignedValue<T>(address_, other.value());
197 198 199
      return *this;
    }
    Reference& operator=(T value) {
200
      base::WriteUnalignedValue<T>(address_, value);
201 202 203 204 205 206 207 208 209
      return *this;
    }

    // Values of type UnalignedSlot::reference must be implicitly convertible
    // to UnalignedSlot::value_type.
    operator T() const { return value(); }

    void swap(Reference& other) {
      T tmp = value();
210 211
      base::WriteUnalignedValue<T>(address_, other.value());
      base::WriteUnalignedValue<T>(other.address_, tmp);
212 213 214 215 216 217 218 219 220 221 222
    }

    bool operator<(const Reference& other) const {
      return value() < other.value();
    }

    bool operator==(const Reference& other) const {
      return value() == other.value();
    }

   private:
223
    T value() const { return base::ReadUnalignedValue<T>(address_); }
224 225 226 227 228 229

    Address address_;
  };

  // The rest of this class follows C++'s "RandomAccessIterator" requirements.
  // Most of the heavy lifting is inherited from SlotBase.
230 231 232 233 234
  using difference_type = int;
  using value_type = T;
  using reference = Reference;
  using pointer = T*;
  using iterator_category = std::random_access_iterator_tag;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

  UnalignedSlot() : SlotBase<UnalignedSlot<T>, T, 1>(kNullAddress) {}
  explicit UnalignedSlot(Address address)
      : SlotBase<UnalignedSlot<T>, T, 1>(address) {}
  explicit UnalignedSlot(T* address)
      : SlotBase<UnalignedSlot<T>, T, 1>(reinterpret_cast<Address>(address)) {}

  Reference operator*() const {
    return Reference(SlotBase<UnalignedSlot<T>, T, 1>::address());
  }
  Reference operator[](difference_type i) const {
    return Reference(SlotBase<UnalignedSlot<T>, T, 1>::address() +
                     i * sizeof(T));
  }

  friend void swap(Reference lhs, Reference rhs) { lhs.swap(rhs); }

  friend difference_type operator-(UnalignedSlot a, UnalignedSlot b) {
    return static_cast<int>(a.address() - b.address()) / sizeof(T);
  }
};

257 258 259 260
}  // namespace internal
}  // namespace v8

#endif  // V8_OBJECTS_SLOTS_H_