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_EMBEDDER_DATA_SLOT_H_
6 #define V8_OBJECTS_EMBEDDER_DATA_SLOT_H_
7 
8 #include <utility>
9 
10 #include "src/common/assert-scope.h"
11 #include "src/common/globals.h"
12 #include "src/objects/slots.h"
13 
14 // Has to be the last include (doesn't have include guards):
15 #include "src/objects/object-macros.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 class EmbedderDataArray;
21 class JSObject;
22 class Object;
23 
24 // An EmbedderDataSlot instance describes a kEmbedderDataSlotSize field ("slot")
25 // holding an embedder data which may contain raw aligned pointer or a tagged
26 // pointer (smi or heap object).
27 // Its address() is the address of the slot.
28 // The slot's contents can be read and written using respective load_XX() and
29 // store_XX() methods.
30 // Storing heap object through this slot may require triggering write barriers
31 // so this operation must be done via static store_tagged() methods.
32 class EmbedderDataSlot
33     : public SlotBase<EmbedderDataSlot, Address, kTaggedSize> {
34  public:
EmbedderDataSlot()35   EmbedderDataSlot() : SlotBase(kNullAddress) {}
36   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 #else
42   static constexpr int kTaggedPayloadOffset = 0;
43 #endif
44 
45 #ifdef V8_COMPRESS_POINTERS
46   // The raw payload is located in the other tagged part of the full pointer.
47   static constexpr int kRawPayloadOffset = kTaggedSize - kTaggedPayloadOffset;
48 #endif
49   static constexpr int kRequiredPtrAlignment = kSmiTagSize;
50 
51   // Opaque type used for storing raw embedder data.
52   using RawData = Address;
53 
54   V8_INLINE Object load_tagged() const;
55   V8_INLINE void store_smi(Smi value);
56 
57   // Setting an arbitrary tagged value requires triggering a write barrier
58   // which requires separate object and offset values, therefore these static
59   // functions also has the target object parameter.
60   static V8_INLINE void store_tagged(EmbedderDataArray array, int entry_index,
61                                      Object value);
62   static V8_INLINE void store_tagged(JSObject object, int embedder_field_index,
63                                      Object value);
64 
65   // Tries reinterpret the value as an aligned pointer and sets *out_result to
66   // the pointer-like value. Note, that some Smis could still look like an
67   // aligned pointers.
68   // Returns true on success.
69   V8_INLINE bool ToAlignedPointer(void** out_result) const;
70 
71   // Returns true if the pointer was successfully stored or false it the pointer
72   // was improperly aligned.
73   V8_INLINE V8_WARN_UNUSED_RESULT bool store_aligned_pointer(void* ptr);
74 
75   V8_INLINE RawData load_raw(const DisallowHeapAllocation& no_gc) const;
76   V8_INLINE void store_raw(RawData data, const DisallowHeapAllocation& no_gc);
77 
78  private:
79   // Stores given value to the embedder data slot in a concurrent-marker
80   // friendly manner (tagged part of the slot is written atomically).
81   V8_INLINE void gc_safe_store(Address value);
82 };
83 
84 }  // namespace internal
85 }  // namespace v8
86 
87 #include "src/objects/object-macros-undef.h"
88 
89 #endif  // V8_OBJECTS_EMBEDDER_DATA_SLOT_H_
90