-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathref.hh
More file actions
211 lines (179 loc) · 5.22 KB
/
Copy pathref.hh
File metadata and controls
211 lines (179 loc) · 5.22 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#pragma once
#include <cstdint>
#include <type_traits>
#include <utility>
template <class T, typename = void>
class sref {
private:
constexpr explicit sref(T* p) noexcept : ptr_(p) { }
// Allow access to ptr_ from up-conversions
template<typename, typename>
friend class sref;
public:
constexpr sref() noexcept : ptr_(nullptr) { }
// Yes, we need to duplicate the standard and is_convertible
// constructors and assignment methods, even though the
// is_convertible ones logically suffice. If we don't write the
// standard methods, C++ will helpfully give us default
// implementations for them.
sref(const sref &o) : ptr_(o.ptr_) {
if (ptr_)
ptr_->inc();
}
template<typename U, typename = typename
std::enable_if<std::is_convertible<U*, T*>::value>::type>
sref(const sref<U> &o) : ptr_(o.ptr_) {
if (ptr_)
ptr_->inc();
}
sref(sref &&o) noexcept : ptr_(o.ptr_)
{
o.ptr_ = nullptr;
}
template<typename U, typename = typename
std::enable_if<std::is_convertible<U*, T*>::value>::type>
sref(sref<U> &&o) noexcept : ptr_(o.ptr_)
{
o.ptr_ = nullptr;
}
~sref() {
if (ptr_)
ptr_->dec();
}
sref& operator=(const sref& o) {
T *optr = o.ptr_;
if (optr != ptr_) {
if (optr)
optr->inc();
if (ptr_)
ptr_->dec();
ptr_ = optr;
}
return *this;
}
template<typename U, typename = typename
std::enable_if<std::is_convertible<U*, T*>::value>::type>
sref& operator=(const sref<U>& o) {
T *optr = o.ptr_;
if (optr != ptr_) {
if (optr)
optr->inc();
if (ptr_)
ptr_->dec();
ptr_ = optr;
}
return *this;
}
sref& operator=(sref&& o) {
if (ptr_)
ptr_->dec();
ptr_ = o.ptr_;
o.ptr_ = nullptr;
return *this;
}
template<typename U, typename = typename
std::enable_if<std::is_convertible<U*, T*>::value>::type>
sref& operator=(sref<U>&& o) {
if (ptr_)
ptr_->dec();
ptr_ = o.ptr_;
o.ptr_ = nullptr;
return *this;
}
// Transfer ownership of a pointer to this sref. This *does not*
// increment the reference count and is primarily meant for getting
// srefs for freshly allocated objects that have come with an
// implicit reference count of 1.
static constexpr sref transfer(T* p) {
return sref(p);
}
// Transfer ownership of this sref to a pointer, clearing this sref.
// This *does not* decrement the reference count and is meant for
// transitioning an automatic reference to a manual one.
T* transfer_to_ptr() {
T* res = ptr_;
ptr_ = nullptr;
return res;
}
// Create a new reference to a pointer. This *does* increment the
// reference count and is primarily meant for transferring between
// manual pointer-based reference counting and automatic counting.
// The caller must ensure that it is valid to increment the
// reference count (for example, the count is not currently zero).
static sref newref(T* p) {
if (p)
p->inc();
return sref(p);
}
void reset()
{
if (ptr_) {
ptr_->dec();
ptr_ = nullptr;
}
}
bool init(T* p) {
if (ptr_ || !p->tryinc())
return false;
ptr_ = p;
return true;
}
bool operator==(const sref<T>& pr) const { return ptr_ == pr.ptr_; }
bool operator!=(const sref<T>& pr) const { return ptr_ != pr.ptr_; }
bool operator==(T* p) const { return ptr_ == p; }
bool operator!=(T* p) const { return ptr_ != p; }
explicit operator bool() const noexcept { return !!ptr_; }
T * operator->() const noexcept { return ptr_; }
T & operator*() const noexcept { return *ptr_; }
T * get() const noexcept { return ptr_; }
private:
T *ptr_;
};
template<typename T, typename... Args>
sref<T> make_sref(Args&&... args)
{
return sref<T>::transfer(new T(std::forward<Args>(args)...));
}
class referenced {
public:
// Start with 1 reference by default
referenced(uint64_t refcount = 1) { ref_.v = refcount - 1; }
// The number of valid references is:
// ref_.invalid ? 0 : ref_.count+1;
inline void inc() {
asm volatile("lock; incq %0" : "+m" (ref_.v) :: "memory", "cc");
}
// Attempt to increment the reference count, failing if the count is
// currently zero. If there is ever a tryinc from zero, the count
// will remain zero forever after.
inline bool tryinc() {
// If references is 0 (i.e. ref_.count is 0xffffffff) a 32-bit
// increment will increases ref_.count to 0, but ref_.invalid
// will remain unchanged.
asm volatile("lock; incl %0" : "+m" (ref_.count) :: "memory", "cc");
return ref_.invalid == 0;
}
inline void dec() {
unsigned char c;
// If references is 1 (i.e. ref_.v is 0), a 64-bit decrement will
// underflow ref_.invalid to 0xffffffff (and ref_.count to 0xffffffff).
asm volatile("lock; decq %0; sets %1" : "+m" (ref_.v), "=qm" (c)
:: "memory", "cc");
if (c)
onzero();
}
uint64_t get_consistent() const {
return ref_.invalid ? 0 : (ref_.count + 1);
}
protected:
virtual ~referenced() { }
virtual void onzero() { delete this; }
private:
mutable union {
volatile uint64_t v;
struct {
volatile uint32_t count;
volatile uint32_t invalid;
};
} ref_;
};