forked from ix-project/silo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
marked_ptr.h
170 lines (143 loc) · 2.81 KB
/
marked_ptr.h
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef _MARKED_PTR_H_
#define _MARKED_PTR_H_
#include <functional>
#include <iostream>
#include "macros.h"
#include "util.h"
template <typename T>
class marked_ptr {
public:
// can take the bottom 3 bits of a ptr [ptrs must be 8-byte aligned]
static const uintptr_t LowBitsMask = 0x7;
constexpr inline marked_ptr() : px(0) {}
template <typename U>
inline marked_ptr(U *px) : px(reinterpret_cast<uintptr_t>(px))
{
// type-safety
if (static_cast<T *>(px))
;
INVARIANT(!(this->px & LowBitsMask));
}
// both operator= and copy-ctor PROPAGATE mark bits
template <typename U>
inline marked_ptr(const marked_ptr<U> &o)
: px(o.px)
{
// type-safety
if (static_cast<T *>((U *) nullptr))
;
}
template <typename U>
inline marked_ptr &
operator=(const marked_ptr<U> &o)
{
// type-safety
if (static_cast<T *>((U *) nullptr))
;
px = o.px;
return *this;
}
inline
operator bool() const
{
return get();
}
inline T *
operator->() const
{
return get();
}
inline T &
operator*() const
{
return *get();
}
inline T *
get() const
{
return reinterpret_cast<T *>(px & ~LowBitsMask);
}
template <typename U>
inline void
reset(U *px)
{
INVARIANT(!(px & LowBitsMask));
// type-safety
if (static_cast<T *>(px))
;
this->px = reinterpret_cast<uintptr_t>(px);
}
inline uint8_t
get_flags() const
{
return px & LowBitsMask;
}
inline void
set_flags(uint8_t flags)
{
INVARIANT(!(flags & ~LowBitsMask));
px = (px & ~LowBitsMask) | flags;
}
inline void
or_flags(uint8_t flags)
{
INVARIANT(!(flags & ~LowBitsMask));
px |= flags;
}
template <typename U>
inline bool
operator==(const marked_ptr<U> &o) const
{
return get() == o.get();
}
template <typename U>
inline bool
operator!=(const marked_ptr<U> &o) const
{
return !operator==(o);
}
template <typename U>
inline bool
operator<(const marked_ptr<U> &o) const
{
return get() < o.get();
}
template <typename U>
inline bool
operator>=(const marked_ptr<U> &o) const
{
return !operator<(o);
}
template <typename U>
inline bool
operator>(const marked_ptr<U> &o) const
{
return get() > o.get();
}
template <typename U>
inline bool
operator<=(const marked_ptr<U> &o) const
{
return !operator>(o);
}
private:
uintptr_t px;
};
template <typename T>
inline std::ostream &
operator<<(std::ostream &o, const marked_ptr<T> &p)
{
o << "[px=" << util::hexify(p.get()) << ", flags=0x" << util::hexify(p.get_flags()) << "]";
return o;
}
namespace std {
template <typename T>
struct hash<marked_ptr<T>> {
inline size_t
operator()(marked_ptr<T> p) const
{
return hash<T *>()(p.get());
}
};
}
#endif /* _MARKED_PTR_H_ */