embedder-data-slot-inl.h 5.58 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_EMBEDDER_DATA_SLOT_INL_H_
#define V8_OBJECTS_EMBEDDER_DATA_SLOT_INL_H_

8 9
#include "src/objects/embedder-data-slot.h"

10
#include "src/heap/heap-write-barrier-inl.h"
11
#include "src/objects-inl.h"
12
#include "src/objects/embedder-data-array.h"
13
#include "src/objects/js-objects-inl.h"
14
#include "src/v8memory.h"
15 16 17 18 19 20 21 22 23 24 25

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

namespace v8 {
namespace internal {

EmbedderDataSlot::EmbedderDataSlot(EmbedderDataArray array, int entry_index)
    : SlotBase(FIELD_ADDR(array,
                          EmbedderDataArray::OffsetOfElementAt(entry_index))) {}

26
EmbedderDataSlot::EmbedderDataSlot(JSObject object, int embedder_field_index)
27 28 29
    : SlotBase(FIELD_ADDR(
          object, object->GetEmbedderFieldOffset(embedder_field_index))) {}

30
Object EmbedderDataSlot::load_tagged() const {
31 32 33 34 35
  return ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Load();
}

void EmbedderDataSlot::store_smi(Smi value) {
  ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Store(value);
36
#ifdef V8_COMPRESS_POINTERS
37
  // See gc_safe_store() for the reasons behind two stores.
38 39
  ObjectSlot(address() + kRawPayloadOffset).Relaxed_Store(Smi::kZero);
#endif
40 41 42 43
}

// static
void EmbedderDataSlot::store_tagged(EmbedderDataArray array, int entry_index,
44
                                    Object value) {
45 46
  int slot_offset = EmbedderDataArray::OffsetOfElementAt(entry_index);
  ObjectSlot(FIELD_ADDR(array, slot_offset + kTaggedPayloadOffset))
47
      .Relaxed_Store(value);
48
  WRITE_BARRIER(array, slot_offset + kTaggedPayloadOffset, value);
49
#ifdef V8_COMPRESS_POINTERS
50
  // See gc_safe_store() for the reasons behind two stores.
51 52 53
  ObjectSlot(FIELD_ADDR(array, slot_offset + kRawPayloadOffset))
      .Relaxed_Store(Smi::kZero);
#endif
54 55 56
}

// static
57
void EmbedderDataSlot::store_tagged(JSObject object, int embedder_field_index,
58
                                    Object value) {
59 60
  int slot_offset = object->GetEmbedderFieldOffset(embedder_field_index);
  ObjectSlot(FIELD_ADDR(object, slot_offset + kTaggedPayloadOffset))
61
      .Relaxed_Store(value);
62
  WRITE_BARRIER(object, slot_offset + kTaggedPayloadOffset, value);
63
#ifdef V8_COMPRESS_POINTERS
64
  // See gc_safe_store() for the reasons behind two stores.
65 66 67
  ObjectSlot(FIELD_ADDR(object, slot_offset + kRawPayloadOffset))
      .Relaxed_Store(Smi::kZero);
#endif
68 69 70
}

bool EmbedderDataSlot::ToAlignedPointer(void** out_pointer) const {
71 72 73 74
  // We don't care about atomicity of access here because embedder slots
  // are accessed this way only from the main thread via API during "mutator"
  // phase which is propely synched with GC (concurrent marker may still look
  // at the tagged part of the embedder slot but read-only access is ok).
75 76 77 78 79 80 81
#ifdef V8_COMPRESS_POINTERS
  // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
  // fields (external pointers, doubles and BigInt data) are only kTaggedSize
  // aligned so we have to use unaligned pointer friendly way of accessing them
  // in order to avoid undefined behavior in C++ code.
  Address raw_value = ReadUnalignedValue<Address>(address());
#else
82
  Address raw_value = *location();
83
#endif
84 85
  *out_pointer = reinterpret_cast<void*>(raw_value);
  return HAS_SMI_TAG(raw_value);
86 87 88 89 90
}

bool EmbedderDataSlot::store_aligned_pointer(void* ptr) {
  Address value = reinterpret_cast<Address>(ptr);
  if (!HAS_SMI_TAG(value)) return false;
91
  gc_safe_store(value);
92 93 94 95 96
  return true;
}

EmbedderDataSlot::RawData EmbedderDataSlot::load_raw(
    const DisallowHeapAllocation& no_gc) const {
97 98 99 100
  // We don't care about atomicity of access here because embedder slots
  // are accessed this way only by serializer from the main thread when
  // GC is not active (concurrent marker may still look at the tagged part
  // of the embedder slot but read-only access is ok).
101 102 103 104 105 106 107
#ifdef V8_COMPRESS_POINTERS
  // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
  // fields (external pointers, doubles and BigInt data) are only kTaggedSize
  // aligned so we have to use unaligned pointer friendly way of accessing them
  // in order to avoid undefined behavior in C++ code.
  return ReadUnalignedValue<Address>(address());
#else
108
  return *location();
109
#endif
110 111
}

112
void EmbedderDataSlot::store_raw(EmbedderDataSlot::RawData data,
113
                                 const DisallowHeapAllocation& no_gc) {
114 115 116 117
  gc_safe_store(data);
}

void EmbedderDataSlot::gc_safe_store(Address value) {
118
#ifdef V8_COMPRESS_POINTERS
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  STATIC_ASSERT(kSmiShiftSize == 0);
  STATIC_ASSERT(SmiValuesAre31Bits());
  STATIC_ASSERT(kTaggedSize == kInt32Size);
  // We have to do two 32-bit stores here because
  // 1) tagged part modifications must be atomic to be properly synchronized
  //    with the concurrent marker.
  // 2) atomicity of full pointer store is not guaranteed for embedder slots
  //    since the address of the slot may not be kSystemPointerSize aligned
  //    (only kTaggedSize alignment is guaranteed).
  // TODO(ishell, v8:8875): revisit this once the allocation alignment
  // inconsistency is fixed.
  Address lo = static_cast<intptr_t>(static_cast<int32_t>(value));
  ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Store(Smi(lo));
  Address hi = value >> 32;
  ObjectSlot(address() + kRawPayloadOffset).Relaxed_Store(Object(hi));
#else
  ObjectSlot(address() + kTaggedPayloadOffset).Relaxed_Store(Smi(value));
136
#endif
137 138 139 140 141 142 143 144
}

}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_EMBEDDER_DATA_SLOT_INL_H_