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 struct AtomicInitializerTag {};
75
76 using IntegralType = uint32_t;
77 static constexpr auto kWriteBarrierSlotType =
78 WriteBarrierSlotType::kCompressed;
79
80 V8_INLINE CompressedPointer() : value_(0u) {}
81 V8_INLINE explicit CompressedPointer(const void* value,
82 AtomicInitializerTag) {
83 StoreAtomic(value);
84 }
85 V8_INLINE explicit CompressedPointer(const void* ptr)
86 : value_(Compress(ptr)) {}
87 V8_INLINE explicit CompressedPointer(std::nullptr_t) : value_(0u) {}
88 V8_INLINE explicit CompressedPointer(SentinelPointer)
89 : value_(kCompressedSentinel) {}
90
91 V8_INLINE const void* Load() const { return Decompress(value_); }
92 V8_INLINE const void* LoadAtomic() const {
93 return Decompress(
94 reinterpret_cast<const std::atomic<IntegralType>&>(value_).load(
95 std::memory_order_relaxed));
96 }
97
98 V8_INLINE void Store(const void* ptr) { value_ = Compress(ptr); }
99 V8_INLINE void StoreAtomic(const void* value) {
100 reinterpret_cast<std::atomic<IntegralType>&>(value_).store(
101 Compress(value), std::memory_order_relaxed);
102 }
103
104 V8_INLINE void Clear() { value_ = 0u; }
105 V8_INLINE bool IsCleared() const { return !value_; }
106
107 V8_INLINE bool IsSentinel() const { return value_ == kCompressedSentinel; }
108
109 V8_INLINE uint32_t GetAsInteger() const { return 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 V8_INLINE friend bool operator>(CompressedPointer a, CompressedPointer b) {
124 return a.value_ > b.value_;
125 }
126 V8_INLINE friend bool operator>=(CompressedPointer a, CompressedPointer b) {
127 return a.value_ >= b.value_;
128 }
129
130 static V8_INLINE IntegralType Compress(const void* ptr) {
131 static_assert(SentinelPointer::kSentinelValue ==
132 1 << api_constants::kPointerCompressionShift,
133 "The compression scheme relies on the sentinel encoded as 1 "
134 "<< kPointerCompressionShift");
135 static constexpr size_t kGigaCageMask =
136 ~(api_constants::kCagedHeapReservationAlignment - 1);
137 static constexpr size_t kPointerCompressionShiftMask =
138 (1 << api_constants::kPointerCompressionShift) - 1;
139
140 CPPGC_DCHECK(CageBaseGlobal::IsSet());
141 const uintptr_t base = CageBaseGlobal::Get();
142 CPPGC_DCHECK(!ptr || ptr == kSentinelPointer ||
143 (base & kGigaCageMask) ==
144 (reinterpret_cast<uintptr_t>(ptr) & kGigaCageMask));
146 (reinterpret_cast<uintptr_t>(ptr) & kPointerCompressionShiftMask) == 0);
147
148#if defined(CPPGC_2GB_CAGE)
149 // Truncate the pointer.
150 auto compressed =
151 static_cast<IntegralType>(reinterpret_cast<uintptr_t>(ptr));
152#else // !defined(CPPGC_2GB_CAGE)
153 const auto uptr = reinterpret_cast<uintptr_t>(ptr);
154 // Shift the pointer and truncate.
155 auto compressed = static_cast<IntegralType>(
156 uptr >> api_constants::kPointerCompressionShift);
157#endif // !defined(CPPGC_2GB_CAGE)
158 // Normal compressed pointers must have the MSB set.
159 CPPGC_DCHECK((!compressed || compressed == kCompressedSentinel) ||
160 (compressed & (1 << 31)));
161 return compressed;
162 }
163
164 static V8_INLINE void* Decompress(IntegralType ptr) {
165 CPPGC_DCHECK(CageBaseGlobal::IsSet());
166 const uintptr_t base = CageBaseGlobal::Get();
167 return Decompress(ptr, base);
168 }
169
170 static V8_INLINE void* Decompress(IntegralType ptr, uintptr_t base) {
171 CPPGC_DCHECK(CageBaseGlobal::IsSet());
172 CPPGC_DCHECK(base == CageBaseGlobal::Get());
173 // Treat compressed pointer as signed and cast it to uint64_t, which will
174 // sign-extend it.
175#if defined(CPPGC_2GB_CAGE)
176 const uint64_t mask = static_cast<uint64_t>(static_cast<int32_t>(ptr));
177#else // !defined(CPPGC_2GB_CAGE)
178 // Then, shift the result. It's important to shift the unsigned
179 // value, as otherwise it would result in undefined behavior.
180 const uint64_t mask = static_cast<uint64_t>(static_cast<int32_t>(ptr))
181 << api_constants::kPointerCompressionShift;
182#endif // !defined(CPPGC_2GB_CAGE)
183 return reinterpret_cast<void*>(mask & base);
184 }
185
186 private:
187#if defined(CPPGC_2GB_CAGE)
188 static constexpr IntegralType kCompressedSentinel =
189 SentinelPointer::kSentinelValue;
190#else // !defined(CPPGC_2GB_CAGE)
191 static constexpr IntegralType kCompressedSentinel =
192 SentinelPointer::kSentinelValue >>
193 api_constants::kPointerCompressionShift;
194#endif // !defined(CPPGC_2GB_CAGE)
195 // All constructors initialize `value_`. Do not add a default value here as it
196 // results in a non-atomic write on some builds, even when the atomic version
197 // of the constructor is used.
198 IntegralType value_;
199};
200
201#endif // defined(CPPGC_POINTER_COMPRESSION)
202
204 public:
206
207 using IntegralType = uintptr_t;
208 static constexpr auto kWriteBarrierSlotType =
209 WriteBarrierSlotType::kUncompressed;
210
211 V8_INLINE RawPointer() : ptr_(nullptr) {}
212 V8_INLINE explicit RawPointer(const void* ptr, AtomicInitializerTag) {
213 StoreAtomic(ptr);
214 }
215 V8_INLINE explicit RawPointer(const void* ptr) : ptr_(ptr) {}
216
217 V8_INLINE const void* Load() const { return ptr_; }
218 V8_INLINE const void* LoadAtomic() const {
219 return reinterpret_cast<const std::atomic<const void*>&>(ptr_).load(
220 std::memory_order_relaxed);
221 }
222
223 V8_INLINE void Store(const void* ptr) { ptr_ = ptr; }
224 V8_INLINE void StoreAtomic(const void* ptr) {
225 reinterpret_cast<std::atomic<const void*>&>(ptr_).store(
226 ptr, std::memory_order_relaxed);
227 }
228
229 V8_INLINE void Clear() { ptr_ = nullptr; }
230 V8_INLINE bool IsCleared() const { return !ptr_; }
231
232 V8_INLINE bool IsSentinel() const { return ptr_ == kSentinelPointer; }
233
234 V8_INLINE uintptr_t GetAsInteger() const {
235 return reinterpret_cast<uintptr_t>(ptr_);
236 }
237
239 return a.ptr_ == b.ptr_;
240 }
242 return a.ptr_ != b.ptr_;
243 }
245 return a.ptr_ < b.ptr_;
246 }
248 return a.ptr_ <= b.ptr_;
249 }
251 return a.ptr_ > b.ptr_;
252 }
254 return a.ptr_ >= b.ptr_;
255 }
256
257 private:
258 // All constructors initialize `ptr_`. Do not add a default value here as it
259 // results in a non-atomic write on some builds, even when the atomic version
260 // of the constructor is used.
261 const void* ptr_;
262};
263
264#if defined(CPPGC_POINTER_COMPRESSION)
265using DefaultMemberStorage = CompressedPointer;
266#else // !defined(CPPGC_POINTER_COMPRESSION)
268#endif // !defined(CPPGC_POINTER_COMPRESSION)
269
270} // namespace internal
271} // namespace cppgc
272
273#endif // INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_
Definition: member-storage.h:203
const void * Load() const
Definition: member-storage.h:217
const void * LoadAtomic() const
Definition: member-storage.h:218
RawPointer(const void *ptr)
Definition: member-storage.h:215
friend bool operator==(RawPointer a, RawPointer b)
Definition: member-storage.h:238
uintptr_t GetAsInteger() const
Definition: member-storage.h:234
friend bool operator>=(RawPointer a, RawPointer b)
Definition: member-storage.h:253
void Clear()
Definition: member-storage.h:229
friend bool operator<(RawPointer a, RawPointer b)
Definition: member-storage.h:244
RawPointer()
Definition: member-storage.h:211
RawPointer(const void *ptr, AtomicInitializerTag)
Definition: member-storage.h:212
friend bool operator!=(RawPointer a, RawPointer b)
Definition: member-storage.h:241
bool IsSentinel() const
Definition: member-storage.h:232
void Store(const void *ptr)
Definition: member-storage.h:223
uintptr_t IntegralType
Definition: member-storage.h:207
friend bool operator>(RawPointer a, RawPointer b)
Definition: member-storage.h:250
friend bool operator<=(RawPointer a, RawPointer b)
Definition: member-storage.h:247
bool IsCleared() const
Definition: member-storage.h:230
void StoreAtomic(const void *ptr)
Definition: member-storage.h:224
#define CPPGC_DCHECK(condition)
Definition: logging.h:36
bool operator<=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:515
bool operator!=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:368
bool operator>(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:530
bool operator==(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:348
WriteBarrierSlotType
Definition: member-storage.h:20
bool operator>=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:545
bool operator<(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:500
RawPointer DefaultMemberStorage
Definition: member-storage.h:267
Definition: allocation.h:38
constexpr internal::SentinelPointer kSentinelPointer
Definition: sentinel-pointer.h:35
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_TRIVIAL_ABI
Definition: v8config.h:743