embedder-data-slot.h 4.61 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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_H_
#define V8_OBJECTS_EMBEDDER_DATA_SLOT_H_

#include <utility>

10 11
#include "src/common/assert-scope.h"
#include "src/common/globals.h"
12
#include "src/objects/slots.h"
13 14 15 16 17 18 19 20

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

namespace v8 {
namespace internal {

class EmbedderDataArray;
21 22
class JSObject;
class Object;
23 24 25 26 27 28 29 30 31 32

// An EmbedderDataSlot instance describes a kEmbedderDataSlotSize field ("slot")
// holding an embedder data which may contain raw aligned pointer or a tagged
// pointer (smi or heap object).
// Its address() is the address of the slot.
// The slot's contents can be read and written using respective load_XX() and
// store_XX() methods.
// Storing heap object through this slot may require triggering write barriers
// so this operation must be done via static store_tagged() methods.
class EmbedderDataSlot
33
    : public SlotBase<EmbedderDataSlot, Address, kTaggedSize> {
34 35 36
 public:
  EmbedderDataSlot() : SlotBase(kNullAddress) {}
  V8_INLINE EmbedderDataSlot(EmbedderDataArray array, int entry_index);
37
  V8_INLINE EmbedderDataSlot(JSObject object, int embedder_field_index);
38

39
#if defined(V8_TARGET_BIG_ENDIAN) && defined(V8_COMPRESS_POINTERS)
40
  static constexpr int kTaggedPayloadOffset = kTaggedSize;
41 42
#else
  static constexpr int kTaggedPayloadOffset = 0;
43 44
#endif

45
#ifdef V8_COMPRESS_POINTERS
46 47 48 49 50
  // The raw payload is located in the other "tagged" part of the full pointer
  // and cotains the upper part of aligned address. The raw part is not expected
  // to look like a tagged value.
  // When V8_HEAP_SANDBOX is defined the raw payload contains an index into the
  // external pointer table.
51
  static constexpr int kRawPayloadOffset = kTaggedSize - kTaggedPayloadOffset;
52 53 54 55
#endif
  static constexpr int kRequiredPtrAlignment = kSmiTagSize;

  // Opaque type used for storing raw embedder data.
56
  using RawData = Address;
57

58 59
  V8_INLINE void AllocateExternalPointerEntry(Isolate* isolate);

60
  V8_INLINE Object load_tagged() const;
61 62 63 64
  V8_INLINE void store_smi(Smi value);

  // Setting an arbitrary tagged value requires triggering a write barrier
  // which requires separate object and offset values, therefore these static
65
  // functions also has the target object parameter.
66
  static V8_INLINE void store_tagged(EmbedderDataArray array, int entry_index,
67
                                     Object value);
68
  static V8_INLINE void store_tagged(JSObject object, int embedder_field_index,
69
                                     Object value);
70

71 72 73 74
  // Tries reinterpret the value as an aligned pointer and sets *out_result to
  // the pointer-like value. Note, that some Smis could still look like an
  // aligned pointers.
  // Returns true on success.
75 76 77
  // When V8 heap sandbox is enabled, calling this method when the raw part of
  // the slot does not contain valid external pointer table index is undefined
  // behaviour and most likely result in crashes.
78
  V8_INLINE bool ToAlignedPointer(IsolateRoot isolate, void** out_result) const;
79

80 81 82 83 84 85 86 87 88
  // Same as ToAlignedPointer() but with a workaround for V8 heap sandbox.
  // When V8 heap sandbox is enabled, this method doesn't crash when the raw
  // part of the slot contains "undefined" instead of a correct external table
  // entry index (see Factory::InitializeJSObjectBody() for details).
  // Returns true when the external pointer table index was pointing to a valid
  // entry, otherwise false.
  //
  // Call this function if you are not sure whether the slot contains valid
  // external pointer or not.
89
  V8_INLINE bool ToAlignedPointerSafe(IsolateRoot isolate,
90 91
                                      void** out_result) const;

92 93
  // Returns true if the pointer was successfully stored or false it the pointer
  // was improperly aligned.
94 95
  V8_INLINE V8_WARN_UNUSED_RESULT bool store_aligned_pointer(Isolate* isolate,
                                                             void* ptr);
96

97
  V8_INLINE RawData load_raw(Isolate* isolate,
98
                             const DisallowGarbageCollection& no_gc) const;
99
  V8_INLINE void store_raw(Isolate* isolate, RawData data,
100
                           const DisallowGarbageCollection& no_gc);
101 102 103 104

 private:
  // Stores given value to the embedder data slot in a concurrent-marker
  // friendly manner (tagged part of the slot is written atomically).
105
  V8_INLINE void gc_safe_store(Isolate* isolate, Address value);
106 107 108 109 110 111 112 113
};

}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_EMBEDDER_DATA_SLOT_H_