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 using IntegralType = uint32_t;
75 static constexpr auto kWriteBarrierSlotType =
76 WriteBarrierSlotType::kCompressed;
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) {}
85 V8_INLINE const void* Load()
const {
return Decompress(value_); }
86 V8_INLINE const void* LoadAtomic()
const {
88 reinterpret_cast<const std::atomic<IntegralType>&
>(value_).load(
89 std::memory_order_relaxed));
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);
99 V8_INLINE bool IsCleared()
const {
return !value_; }
101 V8_INLINE bool IsSentinel()
const {
return value_ == kCompressedSentinel; }
103 V8_INLINE uint32_t GetAsInteger()
const {
return value_; }
106 return a.value_ == b.value_;
109 return a.value_ != b.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 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;
135 const uintptr_t base = CageBaseGlobal::Get();
137 (base & kGigaCageMask) ==
138 (
reinterpret_cast<uintptr_t
>(ptr) & kGigaCageMask));
140 (
reinterpret_cast<uintptr_t
>(ptr) & kPointerCompressionShiftMask) == 0);
142#if defined(CPPGC_2GB_CAGE)
145 static_cast<IntegralType
>(
reinterpret_cast<uintptr_t
>(ptr));
147 const auto uptr =
reinterpret_cast<uintptr_t
>(ptr);
149 auto compressed =
static_cast<IntegralType
>(
150 uptr >> api_constants::kPointerCompressionShift);
153 CPPGC_DCHECK((!compressed || compressed == kCompressedSentinel) ||
154 (compressed & (1 << 31)));
158 static V8_INLINE void* Decompress(IntegralType ptr) {
160 const uintptr_t base = CageBaseGlobal::Get();
163#if defined(CPPGC_2GB_CAGE)
164 const uint64_t mask =
static_cast<uint64_t
>(
static_cast<int32_t
>(ptr));
168 const uint64_t mask =
static_cast<uint64_t
>(
static_cast<int32_t
>(ptr))
169 << api_constants::kPointerCompressionShift;
171 return reinterpret_cast<void*
>(mask & base);
175#if defined(CPPGC_2GB_CAGE)
176 static constexpr IntegralType kCompressedSentinel =
177 SentinelPointer::kSentinelValue;
179 static constexpr IntegralType kCompressedSentinel =
180 SentinelPointer::kSentinelValue >>
181 api_constants::kPointerCompressionShift;
194 static constexpr auto kWriteBarrierSlotType =
195 WriteBarrierSlotType::kUncompressed;
202 return reinterpret_cast<const std::atomic<const void*>&
>(ptr_).load(
203 std::memory_order_relaxed);
208 reinterpret_cast<std::atomic<const void*>&
>(ptr_).store(
209 ptr, std::memory_order_relaxed);
218 return reinterpret_cast<uintptr_t
>(ptr_);
222 return a.ptr_ == b.ptr_;
225 return a.ptr_ != b.ptr_;
228 return a.ptr_ < b.ptr_;
231 return a.ptr_ <= b.ptr_;
234 return a.ptr_ > b.ptr_;
237 return a.ptr_ >= b.ptr_;
247#if defined(CPPGC_POINTER_COMPRESSION)
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)
V8_INLINE bool operator<(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
V8_INLINE bool operator>(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
V8_INLINE bool operator==(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
V8_INLINE bool operator!=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
V8_INLINE bool operator<=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
RawPointer DefaultMemberStorage
V8_INLINE bool operator>=(const BasicMember< T1, WeaknessTag1, WriteBarrierPolicy1, CheckingPolicy1, StorageType > &member1, const BasicMember< T2, WeaknessTag2, WriteBarrierPolicy2, CheckingPolicy2, StorageType > &member2)
constexpr internal::SentinelPointer kSentinelPointer