5#ifndef INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_
6#define INCLUDE_CPPGC_INTERNAL_MEMBER_STORAGE_H_
25#if defined(CPPGC_POINTER_COMPRESSION)
30#define CPPGC_CONST __attribute__((const))
31#define CPPGC_REQUIRE_CONSTANT_INIT \
32 __attribute__((require_constant_initialization))
35#define CPPGC_REQUIRE_CONSTANT_INIT
40 V8_INLINE CPPGC_CONST
static uintptr_t Get() {
45 V8_INLINE CPPGC_CONST
static bool IsSet() {
47 return (g_base_.base & ~kLowerHalfWordMask) != 0;
52 static constexpr uintptr_t kLowerHalfWordMask =
53 (api_constants::kCagedHeapReservationAlignment - 1);
55 static union alignas(api_constants::kCachelineSize) Base {
57 char cache_line[api_constants::kCachelineSize];
58 } g_base_ CPPGC_REQUIRE_CONSTANT_INIT;
60 CageBaseGlobal() =
delete;
62 V8_INLINE static bool IsBaseConsistent() {
63 return kLowerHalfWordMask == (g_base_.base & kLowerHalfWordMask);
66 friend class CageBaseGlobalUpdater;
69#undef CPPGC_REQUIRE_CONSTANT_INIT
74 struct AtomicInitializerTag {};
76 using IntegralType = uint32_t;
77 static constexpr auto kWriteBarrierSlotType =
78 WriteBarrierSlotType::kCompressed;
80 V8_INLINE CompressedPointer() : value_(0u) {}
81 V8_INLINE explicit CompressedPointer(
const void* value,
82 AtomicInitializerTag) {
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) {}
91 V8_INLINE const void* Load()
const {
return Decompress(value_); }
92 V8_INLINE const void* LoadAtomic()
const {
94 reinterpret_cast<const std::atomic<IntegralType>&
>(value_).load(
95 std::memory_order_relaxed));
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);
105 V8_INLINE bool IsCleared()
const {
return !value_; }
107 V8_INLINE bool IsSentinel()
const {
return value_ == kCompressedSentinel; }
109 V8_INLINE uint32_t GetAsInteger()
const {
return value_; }
112 return a.value_ == b.value_;
115 return a.value_ != b.value_;
118 return a.value_ < b.value_;
121 return a.value_ <= b.value_;
124 return a.value_ > b.value_;
127 return a.value_ >= b.value_;
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;
141 const uintptr_t base = CageBaseGlobal::Get();
143 (base & kGigaCageMask) ==
144 (
reinterpret_cast<uintptr_t
>(ptr) & kGigaCageMask));
146 (
reinterpret_cast<uintptr_t
>(ptr) & kPointerCompressionShiftMask) == 0);
148#if defined(CPPGC_2GB_CAGE)
151 static_cast<IntegralType
>(
reinterpret_cast<uintptr_t
>(ptr));
153 const auto uptr =
reinterpret_cast<uintptr_t
>(ptr);
155 auto compressed =
static_cast<IntegralType
>(
156 uptr >> api_constants::kPointerCompressionShift);
159 CPPGC_DCHECK((!compressed || compressed == kCompressedSentinel) ||
160 (compressed & (1 << 31)));
164 static V8_INLINE void* Decompress(IntegralType ptr) {
166 const uintptr_t base = CageBaseGlobal::Get();
167 return Decompress(ptr, base);
170 static V8_INLINE void* Decompress(IntegralType ptr, uintptr_t base) {
175#if defined(CPPGC_2GB_CAGE)
176 const uint64_t mask =
static_cast<uint64_t
>(
static_cast<int32_t
>(ptr));
180 const uint64_t mask =
static_cast<uint64_t
>(
static_cast<int32_t
>(ptr))
181 << api_constants::kPointerCompressionShift;
183 return reinterpret_cast<void*
>(mask & base);
187#if defined(CPPGC_2GB_CAGE)
188 static constexpr IntegralType kCompressedSentinel =
189 SentinelPointer::kSentinelValue;
191 static constexpr IntegralType kCompressedSentinel =
192 SentinelPointer::kSentinelValue >>
193 api_constants::kPointerCompressionShift;
208 static constexpr auto kWriteBarrierSlotType =
209 WriteBarrierSlotType::kUncompressed;
219 return reinterpret_cast<const std::atomic<const void*>&
>(ptr_).load(
220 std::memory_order_relaxed);
225 reinterpret_cast<std::atomic<const void*>&
>(ptr_).store(
226 ptr, std::memory_order_relaxed);
235 return reinterpret_cast<uintptr_t
>(ptr_);
239 return a.ptr_ == b.ptr_;
242 return a.ptr_ != b.ptr_;
245 return a.ptr_ < b.ptr_;
248 return a.ptr_ <= b.ptr_;
251 return a.ptr_ > b.ptr_;
254 return a.ptr_ >= b.ptr_;
264#if defined(CPPGC_POINTER_COMPRESSION)
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
Definition: member-storage.h:205
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_TRIVIAL_ABI
Definition: v8config.h:743