-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathptr.hxx
329 lines (276 loc) · 8.73 KB
/
ptr.hxx
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#ifndef _CS_PTR_HXX_
#define _CS_PTR_HXX_
namespace cornerstone {
typedef std::atomic<int32> ref_counter_t;
template<typename T> class ptr;
template<typename T> class ptr<T&>;
template<typename T> class ptr<T*>;
template<typename T, typename ... TArgs>
ptr<T> cs_new(TArgs... args) {
size_t sz = sizeof(ref_counter_t) * 2 + sizeof(T);
any_ptr p = ::malloc(sz);
if (p == nilptr) {
throw std::runtime_error("no memory");
}
try {
ref_counter_t* p_int = new (p) ref_counter_t(0);
p_int = new (reinterpret_cast<any_ptr>(p_int + 1)) ref_counter_t(0);
T* p_t = new (reinterpret_cast<any_ptr>(p_int + 1)) T(args...);
(void)p_t;
}
catch (...) {
::free(p);
p = nilptr;
}
return ptr<T>(p);
}
template<typename T>
ptr<T> cs_alloc(size_t size) {
size_t sz = size + sizeof(ref_counter_t) * 2;
any_ptr p = ::malloc(sz);
if (p == nilptr) {
throw std::runtime_error("no memory");
}
try {
ref_counter_t* p_int = new (p) ref_counter_t(0);
p_int = new (reinterpret_cast<any_ptr>(p_int + 1)) ref_counter_t(0);
(void)p_int;
}
catch (...) {
::free(p);
p = nilptr;
}
return ptr<T>(p);
}
template<typename T>
inline ptr<T> cs_safe(T* t) {
return ptr<T>(reinterpret_cast<any_ptr>(reinterpret_cast<ref_counter_t*>(t) - 2));
}
template<typename T>
class ptr {
private:
ptr(any_ptr p, T* p_t = nilptr)
: p_(p), p_t_(p_t == nilptr ? reinterpret_cast<T*>(reinterpret_cast<ref_counter_t*>(p) + 2) : p_t) {
_inc_ref();
}
public:
ptr() : p_(nilptr), p_t_(nilptr) {}
template<typename T1>
ptr(const ptr<T1>& other)
: p_(other.p_), p_t_(other.p_t_) {
_inc_ref();
}
ptr(const ptr<T>& other)
: p_(other.p_), p_t_(other.p_t_) {
_inc_ref();
}
template<typename T1>
ptr(ptr<T1>&& other)
: p_(other.p_), p_t_(other.p_t_) {
other.p_ = nilptr;
other.p_t_ = nilptr;
}
ptr(ptr<T>&& other)
: p_(other.p_), p_t_(other.p_t_) {
other.p_ = nilptr;
other.p_t_ = nilptr;
}
~ptr() {
_dec_ref_and_free();
}
template<typename T1>
ptr<T>& operator = (const ptr<T1>& other) {
_dec_ref_and_free();
p_ = other.p_;
p_t_ = other.p_t_;
_inc_ref();
return *this;
}
ptr<T>& operator = (const ptr<T>& other) {
_dec_ref_and_free();
p_ = other.p_;
p_t_ = other.p_t_;
_inc_ref();
return *this;
}
public:
inline T* get() const {
return p_t_;
}
inline void reset() {
_dec_ref_and_free();
p_ = nilptr;
p_t_ = nilptr;
}
inline T* operator -> () const {
return get();
}
inline T& operator *() const {
return *get();
}
inline operator bool() const {
return p_ != nilptr;
}
inline bool operator == (const ptr<T>& other) const {
return p_ == other.p_;
}
inline bool operator != (const ptr<T>& other) const {
return p_ != other.p_;
}
inline bool operator == (const T* p) const {
if (p_ == nilptr) {
return p == nilptr;
}
return get() == p;
}
inline bool operator != (const T* p) const {
if (p_ == nilptr) {
return p != nilptr;
}
return get() != p;
}
private:
inline void _inc_ref() {
if (p_ != nilptr)++ *reinterpret_cast<ref_counter_t*>(p_);
}
inline void _dec_ref_and_free() {
if (p_ != nilptr && 0 == (-- *reinterpret_cast<ref_counter_t*>(p_))) {
++ *(reinterpret_cast<ref_counter_t*>(p_) + 1);
p_t_->~T();
// check if there are still references on this, if no, free the memory
if ((-- *(reinterpret_cast<ref_counter_t*>(p_) + 1)) == 0) {
reinterpret_cast<ref_counter_t*>(p_)->~atomic<int32>();
(reinterpret_cast<ref_counter_t*>(p_) + 1)->~atomic<int32>();
::free(p_);
}
}
}
private:
any_ptr p_;
T* p_t_;
public:
template<typename T1>
friend class ptr;
template<typename T1, typename ... TArgs>
friend ptr<T1> cs_new(TArgs... args);
template<typename T1>
friend ptr<T1> cs_safe(T1* t);
template<typename T1>
friend ptr<T1> cs_alloc(size_t size);
};
template<typename T>
class ptr<T&> {
public:
ptr() : p_(nilptr), p_t_(nilptr) {}
ptr(const ptr<T&>&& other)
: p_(other.p_), p_t_(other.p_t_) {
other.p_ = nilptr;
other.p_t_ = nilptr;
}
template<typename T1>
ptr(ptr<T1&>&& other)
: p_(other.p_), p_t_(other.p_t_) {
other.p_ = nilptr;
other.p_t_ = nilptr;
}
ptr(const ptr<T>& src)
: p_(src.p_), p_t_(src.p_t_) {
_inc_ref();
}
template<typename T1>
ptr(const ptr<T1>& src)
: p_(src.p_), p_t_(src.p_t_) {
_inc_ref();
}
ptr(const ptr<T&>& other)
: p_(other.p_), p_t_(other.p_t_) {
_inc_ref();
}
template<typename T1>
ptr(const ptr<T1&>& other)
: p_(other.p_), p_t_(other.p_t_) {
_inc_ref();
}
~ptr() {
_dec_ref_and_free();
}
template<typename T1>
ptr<T&>& operator = (const ptr<T1&>& other) {
_dec_ref_and_free();
p_ = other.p_;
p_t_ = other.p_t_;
_inc_ref();
return *this;
}
ptr<T&>& operator = (const ptr<T&>& other) {
_dec_ref_and_free();
p_ = other.p_;
p_t_ = other.p_t_;
_inc_ref();
return *this;
}
template<typename T1>
ptr<T&>& operator = (const ptr<T1>& other) {
_dec_ref_and_free();
p_ = other.p_;
p_t_ = other.p_t_;
_inc_ref();
return *this;
}
ptr<T&>& operator = (const ptr<T>& other) {
_dec_ref_and_free();
p_ = other.p_;
p_t_ = other.p_t_;
_inc_ref();
return *this;
}
inline operator bool() const {
return p_ != nilptr && reinterpret_cast<ref_counter_t*>(p_)->load() > 0;
}
inline T& operator *() {
if (*this) {
return *get();
}
throw std::runtime_error("try to reference to a nilptr");
}
inline ptr<T> operator &() {
if (*this) {
return ptr<T>(p_, p_t_);
}
return ptr<T>();
}
inline const T& operator *() const {
if (*this) {
return *get();
}
throw std::runtime_error("try to reference to a nilptr");
}
inline ptr<T> operator &() const {
if (*this) {
return ptr<T>(p_, p_t_);
}
return ptr<T>();
}
private:
inline T* get() const {
return p_t_;
}
inline void _inc_ref() {
if (p_ != nilptr)++ *(reinterpret_cast<ref_counter_t*>(p_) + 1);
}
inline void _dec_ref_and_free() {
if (p_ != nilptr && 0 == (-- *(reinterpret_cast<ref_counter_t*>(p_) + 1))) {
// check if there are still owners on this, if no, free the memory
if (reinterpret_cast<ref_counter_t*>(p_)->load() == 0) {
reinterpret_cast<ref_counter_t*>(p_)->~atomic<int32>();
(reinterpret_cast<ref_counter_t*>(p_) + 1)->~atomic<int32>();
::free(p_);
}
}
}
private:
any_ptr p_;
T* p_t_;
};
}
#endif //_CS_PTR_HXX_