5 #ifndef V8_POINTER_WITH_PAYLOAD_H_ 6 #define V8_POINTER_WITH_PAYLOAD_H_ 11 #include "include/v8config.h" 12 #include "src/base/logging.h" 17 template <
typename Po
interType>
19 static constexpr
int value =
20 alignof(PointerType) >= 8 ? 3 :
alignof(PointerType) >= 4 ? 2 : 1;
35 template <
typename Po
interType,
typename PayloadType,
int NumPayloadBits>
39 typename std::remove_const<PointerType>::type>::value;
41 kAvailBits >= NumPayloadBits,
42 "Ptr does not have sufficient alignment for the selected amount of " 46 static constexpr
uintptr_t kPointerMask = ~kPayloadMask;
52 : pointer_(reinterpret_cast<uintptr_t>(pointer)) {
53 DCHECK_EQ(GetPointer(), pointer);
54 DCHECK_EQ(GetPayload(), static_cast<PayloadType>(0));
58 : pointer_(static_cast<uintptr_t>(payload)) {
59 DCHECK_EQ(GetPointer(),
nullptr);
60 DCHECK_EQ(GetPayload(), payload);
64 update(pointer, payload);
67 V8_INLINE PointerType* GetPointer()
const {
68 return reinterpret_cast<PointerType*
>(pointer_ & kPointerMask);
71 V8_INLINE PointerType* operator->()
const {
return GetPointer(); }
73 V8_INLINE
void update(PointerType* new_pointer, PayloadType new_payload) {
74 pointer_ =
reinterpret_cast<uintptr_t>(new_pointer) |
75 static_cast<uintptr_t>(new_payload);
76 DCHECK_EQ(GetPayload(), new_payload);
77 DCHECK_EQ(GetPointer(), new_pointer);
80 V8_INLINE
void SetPointer(PointerType* newptr) {
81 DCHECK_EQ(reinterpret_cast<uintptr_t>(newptr) & kPayloadMask, 0);
82 pointer_ =
reinterpret_cast<uintptr_t>(newptr) | (pointer_ & kPayloadMask);
83 DCHECK_EQ(GetPointer(), newptr);
86 V8_INLINE PayloadType GetPayload()
const {
87 return static_cast<PayloadType
>(pointer_ & kPayloadMask);
90 V8_INLINE
void SetPayload(PayloadType new_payload) {
92 DCHECK_EQ(new_payload_ptr & kPayloadMask, new_payload_ptr);
93 pointer_ = (pointer_ & kPointerMask) | new_payload_ptr;
94 DCHECK_EQ(GetPayload(), new_payload);
104 #endif // V8_POINTER_WITH_PAYLOAD_H_