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 const auto uptr = reinterpret_cast<uintptr_t>(ptr);
149 // Shift the pointer and truncate.
150 auto compressed = static_cast<IntegralType>(
151 uptr >> api_constants::kPointerCompressionShift);
152 // Normal compressed pointers must have the MSB set. This is guaranteed by
153 // the cage alignment.
154 CPPGC_DCHECK((!compressed || compressed == kCompressedSentinel) ||
155 (compressed & (1 << 31)));
156 return compressed;
157 }
158
159 static V8_INLINE void* Decompress(IntegralType ptr) {
160 CPPGC_DCHECK(CageBaseGlobal::IsSet());
161 const uintptr_t base = CageBaseGlobal::Get();
162 return Decompress(ptr, base);
163 }
164
165 static V8_INLINE void* Decompress(IntegralType ptr, uintptr_t base) {
166 CPPGC_DCHECK(CageBaseGlobal::IsSet());
167 CPPGC_DCHECK(base == CageBaseGlobal::Get());
168 // Sign-extend compressed pointer to full width. This ensure that normal
169 // pointers have only 1s in the base part of the address. It's also
170 // important to shift the unsigned value, as otherwise it would result in
171 // undefined behavior.
172 const uint64_t mask = static_cast<uint64_t>(static_cast<int32_t>(ptr))
173 << api_constants::kPointerCompressionShift;
174 // Set the base part of the address for normal compressed pointers. Note
175 // that nullptr and the sentinel value do not have 1s in the base part and
176 // remain as-is in this operation.
177 return reinterpret_cast<void*>(mask & base);
178 }
179
180 private:
181 static constexpr IntegralType kCompressedSentinel =
182 SentinelPointer::kSentinelValue >>
183 api_constants::kPointerCompressionShift;
184 // All constructors initialize `value_`. Do not add a default value here as it
185 // results in a non-atomic write on some builds, even when the atomic version
186 // of the constructor is used.
187 IntegralType value_;
188};
189
190#endif // defined(CPPGC_POINTER_COMPRESSION)
191
193 public:
195
196 using IntegralType = uintptr_t;
197 static constexpr auto kWriteBarrierSlotType =
198 WriteBarrierSlotType::kUncompressed;
199
200 V8_INLINE RawPointer() : ptr_(nullptr) {}
201 V8_INLINE explicit RawPointer(const void* ptr, AtomicInitializerTag) {
202 StoreAtomic(ptr);
203 }
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:192
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:200
RawPointer(const void *ptr, AtomicInitializerTag)
Definition: member-storage.h:201
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:196
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: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:256
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