v8 11.3.244 (node 20.3.0)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
member-storage.h
Go to the documentation of this file.
1// Copyright 2022 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 INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_
6#define INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_
7
8#include <atomic>
9#include <cstddef>
10#include <type_traits>
11
15#include "v8config.h" // NOLINT(build/include_directory)
16
17namespace cppgc {
18namespace internal {
19
23};
24
25#if defined(CPPGC_POINTER_COMPRESSION)
26
27#if defined(__clang__)
28// Attribute const allows the compiler to assume that CageBaseGlobal::g_base_
29// doesn't change (e.g. across calls) and thereby avoid redundant loads.
30#define CPPGC_CONST __attribute__((const))
31#define CPPGC_REQUIRE_CONSTANT_INIT \
32 __attribute__((require_constant_initialization))
33#else // defined(__clang__)
34#define CPPGC_CONST
35#define CPPGC_REQUIRE_CONSTANT_INIT
36#endif // defined(__clang__)
37
38class V8_EXPORT CageBaseGlobal final {
39 public:
40 V8_INLINE CPPGC_CONST static uintptr_t Get() {
41 CPPGC_DCHECK(IsBaseConsistent());
42 return g_base_.base;
43 }
44
45 V8_INLINE CPPGC_CONST static bool IsSet() {
46 CPPGC_DCHECK(IsBaseConsistent());
47 return (g_base_.base & ~kLowerHalfWordMask) != 0;
48 }
49
50 private:
51 // We keep the lower halfword as ones to speed up decompression.
52 static constexpr uintptr_t kLowerHalfWordMask =
53 (api_constants::kCagedHeapReservationAlignment - 1);
54
55 static union alignas(api_constants::kCachelineSize) Base {
56 uintptr_t base;
57 char cache_line[api_constants::kCachelineSize];
58 } g_base_ CPPGC_REQUIRE_CONSTANT_INIT;
59
60 CageBaseGlobal() = delete;
61
62 V8_INLINE static bool IsBaseConsistent() {
63 return kLowerHalfWordMask == (g_base_.base & kLowerHalfWordMask);
64 }
65
66 friend class CageBaseGlobalUpdater;
67};
68
69#undef CPPGC_REQUIRE_CONSTANT_INIT
70#undef CPPGC_CONST
71
72class V8_TRIVIAL_ABI CompressedPointer final {
73 public:
74 using IntegralType = uint32_t;
75 static constexpr auto kWriteBarrierSlotType =
76 WriteBarrierSlotType::kCompressed;
77
78 V8_INLINE CompressedPointer() : value_(0u) {}
79 V8_INLINE explicit CompressedPointer(const void* ptr)
80 : value_(Compress(ptr)) {}
81 V8_INLINE explicit CompressedPointer(std::nullptr_t) : value_(0u) {}
82 V8_INLINE explicit CompressedPointer(SentinelPointer)
83 : value_(kCompressedSentinel) {}
84
85 V8_INLINE const void* Load() const { return Decompress(value_); }
86 V8_INLINE const void* LoadAtomic() const {
87 return Decompress(
88 reinterpret_cast<const std::atomic<IntegralType>&>(value_).load(
89 std::memory_order_relaxed));
90 }
91
92 V8_INLINE void Store(const void* ptr) { value_ = Compress(ptr); }
93 V8_INLINE void StoreAtomic(const void* value) {
94 reinterpret_cast<std::atomic<IntegralType>&>(value_).store(
95 Compress(value), std::memory_order_relaxed);
96 }
97
98 V8_INLINE void Clear() { value_ = 0u; }
99 V8_INLINE bool IsCleared() const { return !value_; }
100
101 V8_INLINE bool IsSentinel() const { return value_ == kCompressedSentinel; }
102
103 V8_INLINE uint32_t GetAsInteger() const { return value_; }
104
105 V8_INLINE friend bool operator==(CompressedPointer a, CompressedPointer b) {
106 return a.value_ == b.value_;
107 }
108 V8_INLINE friend bool operator!=(CompressedPointer a, CompressedPointer b) {
109 return a.value_ != b.value_;
110 }
111 V8_INLINE friend bool operator<(CompressedPointer a, CompressedPointer b) {
112 return a.value_ < b.value_;
113 }
114 V8_INLINE friend bool operator<=(CompressedPointer a, CompressedPointer b) {
115 return a.value_ <= b.value_;
116 }
117 V8_INLINE friend bool operator>(CompressedPointer a, CompressedPointer b) {
118 return a.value_ > b.value_;
119 }
120 V8_INLINE friend bool operator>=(CompressedPointer a, CompressedPointer b) {
121 return a.value_ >= b.value_;
122 }
123
124 static V8_INLINE IntegralType Compress(const void* ptr) {
125 static_assert(
126 SentinelPointer::kSentinelValue == 0b10,
127 "The compression scheme relies on the sentinel encoded as 0b10");
128 static constexpr size_t kGigaCageMask =
129 ~(api_constants::kCagedHeapReservationAlignment - 1);
130
131 CPPGC_DCHECK(CageBaseGlobal::IsSet());
132 const uintptr_t base = CageBaseGlobal::Get();
133 CPPGC_DCHECK(!ptr || ptr == kSentinelPointer ||
134 (base & kGigaCageMask) ==
135 (reinterpret_cast<uintptr_t>(ptr) & kGigaCageMask));
136
137#if defined(CPPGC_2GB_CAGE)
138 // Truncate the pointer.
139 auto compressed =
140 static_cast<IntegralType>(reinterpret_cast<uintptr_t>(ptr));
141#else // !defined(CPPGC_2GB_CAGE)
142 const auto uptr = reinterpret_cast<uintptr_t>(ptr);
143 // Shift the pointer by one and truncate.
144 auto compressed = static_cast<IntegralType>(uptr >> 1);
145#endif // !defined(CPPGC_2GB_CAGE)
146 // Normal compressed pointers must have the MSB set.
147 CPPGC_DCHECK((!compressed || compressed == kCompressedSentinel) ||
148 (compressed & (1 << 31)));
149 return compressed;
150 }
151
152 static V8_INLINE void* Decompress(IntegralType ptr) {
153 CPPGC_DCHECK(CageBaseGlobal::IsSet());
154 const uintptr_t base = CageBaseGlobal::Get();
155 // Treat compressed pointer as signed and cast it to uint64_t, which will
156 // sign-extend it.
157#if defined(CPPGC_2GB_CAGE)
158 const uint64_t mask = static_cast<uint64_t>(static_cast<int32_t>(ptr));
159#else // !defined(CPPGC_2GB_CAGE)
160 // Then, shift the result by one. It's important to shift the unsigned
161 // value, as otherwise it would result in undefined behavior.
162 const uint64_t mask = static_cast<uint64_t>(static_cast<int32_t>(ptr)) << 1;
163#endif // !defined(CPPGC_2GB_CAGE)
164 return reinterpret_cast<void*>(mask & base);
165 }
166
167 private:
168#if defined(CPPGC_2GB_CAGE)
169 static constexpr IntegralType kCompressedSentinel =
170 SentinelPointer::kSentinelValue;
171#else // !defined(CPPGC_2GB_CAGE)
172 static constexpr IntegralType kCompressedSentinel =
173 SentinelPointer::kSentinelValue >> 1;
174#endif // !defined(CPPGC_2GB_CAGE)
175 // All constructors initialize `value_`. Do not add a default value here as it
176 // results in a non-atomic write on some builds, even when the atomic version
177 // of the constructor is used.
178 IntegralType value_;
179};
180
181#endif // defined(CPPGC_POINTER_COMPRESSION)
182
184 public:
185 using IntegralType = uintptr_t;
186 static constexpr auto kWriteBarrierSlotType =
187 WriteBarrierSlotType::kUncompressed;
188
189 V8_INLINE RawPointer() : ptr_(nullptr) {}
190 V8_INLINE explicit RawPointer(const void* ptr) : ptr_(ptr) {}
191
192 V8_INLINE const void* Load() const { return ptr_; }
193 V8_INLINE const void* LoadAtomic() const {
194 return reinterpret_cast<const std::atomic<const void*>&>(ptr_).load(
195 std::memory_order_relaxed);
196 }
197
198 V8_INLINE void Store(const void* ptr) { ptr_ = ptr; }
199 V8_INLINE void StoreAtomic(const void* ptr) {
200 reinterpret_cast<std::atomic<const void*>&>(ptr_).store(
201 ptr, std::memory_order_relaxed);
202 }
203
204 V8_INLINE void Clear() { ptr_ = nullptr; }
205 V8_INLINE bool IsCleared() const { return !ptr_; }
206
207 V8_INLINE bool IsSentinel() const { return ptr_ == kSentinelPointer; }
208
209 V8_INLINE uintptr_t GetAsInteger() const {
210 return reinterpret_cast<uintptr_t>(ptr_);
211 }
212
214 return a.ptr_ == b.ptr_;
215 }
217 return a.ptr_ != b.ptr_;
218 }
220 return a.ptr_ < b.ptr_;
221 }
223 return a.ptr_ <= b.ptr_;
224 }
226 return a.ptr_ > b.ptr_;
227 }
229 return a.ptr_ >= b.ptr_;
230 }
231
232 private:
233 // All constructors initialize `ptr_`. Do not add a default value here as it
234 // results in a non-atomic write on some builds, even when the atomic version
235 // of the constructor is used.
236 const void* ptr_;
237};
238
239#if defined(CPPGC_POINTER_COMPRESSION)
240using DefaultMemberStorage = CompressedPointer;
241#else // !defined(CPPGC_POINTER_COMPRESSION)
243#endif // !defined(CPPGC_POINTER_COMPRESSION)
244
245} // namespace internal
246} // namespace cppgc
247
248#endif // INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_
V8_INLINE friend bool operator!=(RawPointer a, RawPointer b)
V8_INLINE friend bool operator<(RawPointer a, RawPointer b)
V8_INLINE const void * Load() const
V8_INLINE bool IsSentinel() const
V8_INLINE friend bool operator==(RawPointer a, RawPointer b)
V8_INLINE const void * LoadAtomic() const
V8_INLINE void StoreAtomic(const void *ptr)
V8_INLINE void Store(const void *ptr)
V8_INLINE RawPointer(const void *ptr)
V8_INLINE bool IsCleared() const
V8_INLINE friend bool operator>(RawPointer a, RawPointer b)
V8_INLINE friend bool operator>=(RawPointer a, RawPointer b)
V8_INLINE uintptr_t GetAsInteger() const
V8_INLINE friend bool operator<=(RawPointer a, RawPointer b)
#define CPPGC_DCHECK(condition)
Definition logging.h:36
V8_INLINE bool operator<(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition member.h:491
V8_INLINE bool operator>(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition member.h:521
V8_INLINE bool operator==(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition member.h:339
V8_INLINE bool operator!=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition member.h:359
V8_INLINE bool operator<=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition member.h:506
RawPointer DefaultMemberStorage
V8_INLINE bool operator>=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition member.h:536
constexpr internal::SentinelPointer kSentinelPointer
#define V8_EXPORT
Definition v8config.h:719
#define V8_INLINE
Definition v8config.h:460
#define V8_TRIVIAL_ABI
Definition v8config.h:675