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 const auto uptr =
reinterpret_cast<uintptr_t
>(ptr);
150 auto compressed =
static_cast<IntegralType
>(
151 uptr >> api_constants::kPointerCompressionShift);
154 CPPGC_DCHECK((!compressed || compressed == kCompressedSentinel) ||
155 (compressed & (1 << 31)));
159 static V8_INLINE void* Decompress(IntegralType ptr) {
161 const uintptr_t base = CageBaseGlobal::Get();
162 return Decompress(ptr, base);
165 static V8_INLINE void* Decompress(IntegralType ptr, uintptr_t base) {
172 const uint64_t mask =
static_cast<uint64_t
>(
static_cast<int32_t
>(ptr))
173 << api_constants::kPointerCompressionShift;
177 return reinterpret_cast<void*
>(mask & base);
181 static constexpr IntegralType kCompressedSentinel =
182 SentinelPointer::kSentinelValue >>
183 api_constants::kPointerCompressionShift;
197 static constexpr auto kWriteBarrierSlotType =
198 WriteBarrierSlotType::kUncompressed;
208 return reinterpret_cast<const std::atomic<const void*>&
>(ptr_).load(
209 std::memory_order_relaxed);
214 reinterpret_cast<std::atomic<const void*>&
>(ptr_).store(
215 ptr, std::memory_order_relaxed);
224 return reinterpret_cast<uintptr_t
>(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_;
240 return a.ptr_ > b.ptr_;
243 return a.ptr_ >= b.ptr_;
253#if defined(CPPGC_POINTER_COMPRESSION)
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
Definition: member-storage.h:194
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_TRIVIAL_ABI
Definition: v8config.h:743