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(SentinelPointer::kSentinelValue ==
126 1 << api_constants::kPointerCompressionShift,
127 "The compression scheme relies on the sentinel encoded as 1 "
128 "<< kPointerCompressionShift");
129 static constexpr size_t kGigaCageMask =
130 ~(api_constants::kCagedHeapReservationAlignment - 1);
131 static constexpr size_t kPointerCompressionShiftMask =
132 (1 << api_constants::kPointerCompressionShift) - 1;
133
134 CPPGC_DCHECK(CageBaseGlobal::IsSet());
135 const uintptr_t base = CageBaseGlobal::Get();
136 CPPGC_DCHECK(!ptr || ptr == kSentinelPointer ||
137 (base & kGigaCageMask) ==
138 (reinterpret_cast<uintptr_t>(ptr) & kGigaCageMask));
140 (reinterpret_cast<uintptr_t>(ptr) & kPointerCompressionShiftMask) == 0);
141
142#if defined(CPPGC_2GB_CAGE)
143 // Truncate the pointer.
144 auto compressed =
145 static_cast<IntegralType>(reinterpret_cast<uintptr_t>(ptr));
146#else // !defined(CPPGC_2GB_CAGE)
147 const auto uptr = reinterpret_cast<uintptr_t>(ptr);
148 // Shift the pointer and truncate.
149 auto compressed = static_cast<IntegralType>(
150 uptr >> api_constants::kPointerCompressionShift);
151#endif // !defined(CPPGC_2GB_CAGE)
152 // Normal compressed pointers must have the MSB set.
153 CPPGC_DCHECK((!compressed || compressed == kCompressedSentinel) ||
154 (compressed & (1 << 31)));
155 return compressed;
156 }
157
158 static V8_INLINE void* Decompress(IntegralType ptr) {
159 CPPGC_DCHECK(CageBaseGlobal::IsSet());
160 const uintptr_t base = CageBaseGlobal::Get();
161 return Decompress(ptr, base);
162 }
163
164 static V8_INLINE void* Decompress(IntegralType ptr, uintptr_t base) {
165 CPPGC_DCHECK(CageBaseGlobal::IsSet());
166 CPPGC_DCHECK(base == CageBaseGlobal::Get());
167 // Treat compressed pointer as signed and cast it to uint64_t, which will
168 // sign-extend it.
169#if defined(CPPGC_2GB_CAGE)
170 const uint64_t mask = static_cast<uint64_t>(static_cast<int32_t>(ptr));
171#else // !defined(CPPGC_2GB_CAGE)
172 // Then, shift the result. It's important to shift the unsigned
173 // value, as otherwise it would result in undefined behavior.
174 const uint64_t mask = static_cast<uint64_t>(static_cast<int32_t>(ptr))
175 << api_constants::kPointerCompressionShift;
176#endif // !defined(CPPGC_2GB_CAGE)
177 return reinterpret_cast<void*>(mask & base);
178 }
179
180 private:
181#if defined(CPPGC_2GB_CAGE)
182 static constexpr IntegralType kCompressedSentinel =
183 SentinelPointer::kSentinelValue;
184#else // !defined(CPPGC_2GB_CAGE)
185 static constexpr IntegralType kCompressedSentinel =
186 SentinelPointer::kSentinelValue >>
187 api_constants::kPointerCompressionShift;
188#endif // !defined(CPPGC_2GB_CAGE)
189 // All constructors initialize `value_`. Do not add a default value here as it
190 // results in a non-atomic write on some builds, even when the atomic version
191 // of the constructor is used.
192 IntegralType value_;
193};
194
195#endif // defined(CPPGC_POINTER_COMPRESSION)
196
198 public:
199 using IntegralType = uintptr_t;
200 static constexpr auto kWriteBarrierSlotType =
201 WriteBarrierSlotType::kUncompressed;
202
203 V8_INLINE RawPointer() : ptr_(nullptr) {}
204 V8_INLINE explicit RawPointer(const void* ptr) : ptr_(ptr) {}
205
206 V8_INLINE const void* Load() const { return ptr_; }
207 V8_INLINE const void* LoadAtomic() const {
208 return reinterpret_cast<const std::atomic<const void*>&>(ptr_).load(
209 std::memory_order_relaxed);
210 }
211
212 V8_INLINE void Store(const void* ptr) { ptr_ = ptr; }
213 V8_INLINE void StoreAtomic(const void* ptr) {
214 reinterpret_cast<std::atomic<const void*>&>(ptr_).store(
215 ptr, std::memory_order_relaxed);
216 }
217
218 V8_INLINE void Clear() { ptr_ = nullptr; }
219 V8_INLINE bool IsCleared() const { return !ptr_; }
220
221 V8_INLINE bool IsSentinel() const { return ptr_ == kSentinelPointer; }
222
223 V8_INLINE uintptr_t GetAsInteger() const {
224 return reinterpret_cast<uintptr_t>(ptr_);
225 }
226
228 return a.ptr_ == b.ptr_;
229 }
231 return a.ptr_ != b.ptr_;
232 }
234 return a.ptr_ < b.ptr_;
235 }
237 return a.ptr_ <= b.ptr_;
238 }
240 return a.ptr_ > b.ptr_;
241 }
243 return a.ptr_ >= b.ptr_;
244 }
245
246 private:
247 // All constructors initialize `ptr_`. Do not add a default value here as it
248 // results in a non-atomic write on some builds, even when the atomic version
249 // of the constructor is used.
250 const void* ptr_;
251};
252
253#if defined(CPPGC_POINTER_COMPRESSION)
254using DefaultMemberStorage = CompressedPointer;
255#else // !defined(CPPGC_POINTER_COMPRESSION)
257#endif // !defined(CPPGC_POINTER_COMPRESSION)
258
259} // namespace internal
260} // namespace cppgc
261
262#endif // INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_
Definition: member-storage.h:197
const void * Load() const
Definition: member-storage.h:206
const void * LoadAtomic() const
Definition: member-storage.h:207
RawPointer(const void *ptr)
Definition: member-storage.h:204
friend bool operator==(RawPointer a, RawPointer b)
Definition: member-storage.h:227
uintptr_t GetAsInteger() const
Definition: member-storage.h:223
friend bool operator>=(RawPointer a, RawPointer b)
Definition: member-storage.h:242
void Clear()
Definition: member-storage.h:218
friend bool operator<(RawPointer a, RawPointer b)
Definition: member-storage.h:233
RawPointer()
Definition: member-storage.h:203
friend bool operator!=(RawPointer a, RawPointer b)
Definition: member-storage.h:230
bool IsSentinel() const
Definition: member-storage.h:221
void Store(const void *ptr)
Definition: member-storage.h:212
uintptr_t IntegralType
Definition: member-storage.h:199
friend bool operator>(RawPointer a, RawPointer b)
Definition: member-storage.h:239
friend bool operator<=(RawPointer a, RawPointer b)
Definition: member-storage.h:236
bool IsCleared() const
Definition: member-storage.h:219
void StoreAtomic(const void *ptr)
Definition: member-storage.h:213
#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:506
bool operator!=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:359
bool operator>(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:521
bool operator==(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:339
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:536
bool operator<(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
Definition: member.h:491
RawPointer DefaultMemberStorage
Definition: member-storage.h:256
Definition: allocation.h:38
constexpr internal::SentinelPointer kSentinelPointer
Definition: sentinel-pointer.h:35
#define V8_EXPORT
Definition: v8config.h:762
#define V8_INLINE
Definition: v8config.h:477
#define V8_TRIVIAL_ABI
Definition: v8config.h:712