Repository navigation
Expand file tree
/
Copy pathtagged_ptr.h
More file actions
65 lines (52 loc) · 1.71 KB
/
Copy pathtagged_ptr.h
File metadata and controls
65 lines (52 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
template <typename pointer_type, typename enum_type>
struct tagged_ptr {
pointer_type *Ptr;
constexpr tagged_ptr() : Ptr(0) { }
constexpr tagged_ptr(pointer_type *InPtr, enum_type Type) : Ptr(InPtr) {
AddTag(Type);
}
static constexpr u64 PtrBitShiftAmount = 52;
static constexpr u64 SignBit = 1ULL << (PtrBitShiftAmount - 1);
static constexpr u64 TagMask = ~((1ULL << PtrBitShiftAmount) - 1);
static constexpr u64 DataMask = ~TagMask;
static_assert(std::is_enum_v<enum_type>);
inline constexpr void AddTag(enum_type Type) {
u64 Tag = static_cast<u64>(Type) << PtrBitShiftAmount;
u64 PtrCleared = std::bit_cast<u64>(Ptr) & DataMask;
u64 Result = Tag | PtrCleared;
Ptr = std::bit_cast<pointer_type *>(Result);
}
inline constexpr enum_type GetTag() const {
u64 N = std::bit_cast<u64>(Ptr);
N >>= PtrBitShiftAmount;
enum_type Result = static_cast<enum_type>(N);
return Result;
}
inline constexpr pointer_type *GetPointerWithoutTag(pointer_type *Ptr) const {
u64 N = std::bit_cast<u64>(Ptr);
N &= DataMask;
#if defined(__x86_64__) || defined(_M_X64)
if (N & SignBit) {
N |= TagMask;
}
#endif
return std::bit_cast<pointer_type *>(N);
}
inline constexpr bool operator==(const tagged_ptr<pointer_type, enum_type> &B) const {
return Ptr == B.Ptr;
}
inline constexpr bool operator!=(const tagged_ptr<pointer_type, enum_type> &B) const {
return Ptr != B.Ptr;
}
inline constexpr pointer_type operator*() const {
pointer_type *Pointer = GetPointerWithoutTag(this->Ptr);
return *Pointer;
}
inline constexpr pointer_type *operator->() const {
return GetPointerWithoutTag(this->Ptr);
}
explicit operator bool() const {
return GetPointerWithoutTag(this->Ptr) != 0;
}
};