forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_ptr.hh
725 lines (623 loc) · 18.5 KB
/
shared_ptr.hh
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
#ifndef SHARED_PTR_HH_
#define SHARED_PTR_HH_
#include "shared_ptr_debug_helper.hh"
#include <utility>
#include <type_traits>
#include <functional>
#include <iostream>
#include "util/is_smart_ptr.hh"
#include "util/indirect.hh"
// This header defines two shared pointer facilities, lw_shared_ptr<> and
// shared_ptr<>, both modeled after std::shared_ptr<>.
//
// Unlike std::shared_ptr<>, neither of these implementations are thread
// safe, and two pointers sharing the same object must not be used in
// different threads.
//
// lw_shared_ptr<> is the more lightweight variant, with a lw_shared_ptr<>
// occupying just one machine word, and adding just one word to the shared
// object. However, it does not support polymorphism.
//
// shared_ptr<> is more expensive, with a pointer occupying two machine
// words, and with two words of overhead in the shared object. In return,
// it does support polymorphism.
//
// Both variants support shared_from_this() via enable_shared_from_this<>
// and lw_enable_shared_from_this<>().
//
#ifndef DEBUG_SHARED_PTR
using shared_ptr_counter_type = long;
#else
using shared_ptr_counter_type = debug_shared_ptr_counter_type;
#endif
template <typename T>
class lw_shared_ptr;
template <typename T>
class shared_ptr;
template <typename T>
class enable_lw_shared_from_this;
template <typename T>
class enable_shared_from_this;
template <typename T, typename... A>
lw_shared_ptr<T> make_lw_shared(A&&... a);
template <typename T>
lw_shared_ptr<T> make_lw_shared(T&& a);
template <typename T>
lw_shared_ptr<T> make_lw_shared(T& a);
template <typename T, typename... A>
shared_ptr<T> make_shared(A&&... a);
template <typename T>
shared_ptr<T> make_shared(T&& a);
template <typename T, typename U>
shared_ptr<T> static_pointer_cast(const shared_ptr<U>& p);
template <typename T, typename U>
shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& p);
template <typename T, typename U>
shared_ptr<T> const_pointer_cast(const shared_ptr<U>& p);
// We want to support two use cases for shared_ptr<T>:
//
// 1. T is any type (primitive or class type)
//
// 2. T is a class type that inherits from enable_shared_from_this<T>.
//
// In the first case, we must wrap T in an object containing the counter,
// since T may be a primitive type and cannot be a base class.
//
// In the second case, we want T to reach the counter through its
// enable_shared_from_this<> base class, so that we can implement
// shared_from_this().
//
// To implement those two conflicting requirements (T alongside its counter;
// T inherits from an object containing the counter) we use std::conditional<>
// and some accessor functions to select between two implementations.
// CRTP from this to enable shared_from_this:
template <typename T>
class enable_lw_shared_from_this {
shared_ptr_counter_type _count = 0;
using ctor = T;
T* to_value() { return static_cast<T*>(this); }
T* to_internal_object() { return static_cast<T*>(this); }
protected:
enable_lw_shared_from_this() noexcept {}
enable_lw_shared_from_this(enable_lw_shared_from_this&&) noexcept {}
enable_lw_shared_from_this(const enable_lw_shared_from_this&) noexcept {}
enable_lw_shared_from_this& operator=(const enable_lw_shared_from_this&) noexcept { return *this; }
enable_lw_shared_from_this& operator=(enable_lw_shared_from_this&&) noexcept { return *this; }
public:
lw_shared_ptr<T> shared_from_this();
lw_shared_ptr<const T> shared_from_this() const;
template <typename X>
friend class lw_shared_ptr;
};
template <typename T>
struct shared_ptr_no_esft {
shared_ptr_counter_type _count = 0;
T _value;
using ctor = shared_ptr_no_esft;
T* to_value() { return &_value; }
shared_ptr_no_esft* to_internal_object() { return this; }
shared_ptr_no_esft() = default;
shared_ptr_no_esft(const T& x) : _value(x) {}
shared_ptr_no_esft(T&& x) : _value(std::move(x)) {}
template <typename... A>
shared_ptr_no_esft(A&&... a) : _value(std::forward<A>(a)...) {}
template <typename X>
friend class lw_shared_ptr;
};
template <typename T>
using shared_ptr_impl
= std::conditional_t<
std::is_base_of<enable_lw_shared_from_this<std::remove_const_t<T>>, T>::value,
enable_lw_shared_from_this<std::remove_const_t<T>>,
shared_ptr_no_esft<std::remove_const_t<T>>
>;
template <typename T>
class lw_shared_ptr {
mutable shared_ptr_impl<T>* _p = nullptr;
private:
lw_shared_ptr(shared_ptr_impl<T>* p) noexcept : _p(p) {
if (_p) {
++_p->_count;
}
}
template <typename... A>
static lw_shared_ptr make(A&&... a) {
return lw_shared_ptr(new typename shared_ptr_impl<T>::ctor(std::forward<A>(a)...));
}
public:
using element_type = T;
lw_shared_ptr() noexcept = default;
lw_shared_ptr(std::nullptr_t) noexcept : lw_shared_ptr() {}
lw_shared_ptr(const lw_shared_ptr& x) noexcept : _p(x._p) {
if (_p) {
++_p->_count;
}
}
lw_shared_ptr(lw_shared_ptr&& x) noexcept : _p(x._p) {
x._p = nullptr;
}
[[gnu::always_inline]]
~lw_shared_ptr() {
if (_p && !--_p->_count) {
delete _p->to_internal_object();
}
}
lw_shared_ptr& operator=(const lw_shared_ptr& x) noexcept {
if (_p != x._p) {
this->~lw_shared_ptr();
new (this) lw_shared_ptr(x);
}
return *this;
}
lw_shared_ptr& operator=(lw_shared_ptr&& x) noexcept {
if (_p != x._p) {
this->~lw_shared_ptr();
new (this) lw_shared_ptr(std::move(x));
}
return *this;
}
lw_shared_ptr& operator=(std::nullptr_t) noexcept {
return *this = lw_shared_ptr();
}
lw_shared_ptr& operator=(T&& x) noexcept {
this->~lw_shared_ptr();
new (this) lw_shared_ptr(make_lw_shared<T>(std::move(x)));
return *this;
}
T& operator*() const noexcept { return *_p->to_value(); }
T* operator->() const noexcept { return _p->to_value(); }
T* get() const noexcept { return _p->to_value(); }
long int use_count() const noexcept {
if (_p) {
return _p->_count;
} else {
return 0;
}
}
operator lw_shared_ptr<const T>() const noexcept {
return lw_shared_ptr<const T>(_p);
}
explicit operator bool() const noexcept {
return _p;
}
bool owned() const noexcept {
return _p->_count == 1;
}
bool operator==(const lw_shared_ptr<const T>& x) const {
return _p == x._p;
}
bool operator!=(const lw_shared_ptr<const T>& x) const {
return !operator==(x);
}
bool operator==(const lw_shared_ptr<std::remove_const_t<T>>& x) const {
return _p == x._p;
}
bool operator!=(const lw_shared_ptr<std::remove_const_t<T>>& x) const {
return !operator==(x);
}
bool operator<(const lw_shared_ptr<const T>& x) const {
return _p < x._p;
}
bool operator<(const lw_shared_ptr<std::remove_const_t<T>>& x) const {
return _p < x._p;
}
template <typename U>
friend class lw_shared_ptr;
template <typename X, typename... A>
friend lw_shared_ptr<X> make_lw_shared(A&&...);
template <typename U>
friend lw_shared_ptr<U> make_lw_shared(U&&);
template <typename U>
friend lw_shared_ptr<U> make_lw_shared(U&);
template <typename U>
friend class enable_lw_shared_from_this;
};
template <typename T, typename... A>
inline
lw_shared_ptr<T> make_lw_shared(A&&... a) {
return lw_shared_ptr<T>::make(std::forward<A>(a)...);
}
template <typename T>
inline
lw_shared_ptr<T> make_lw_shared(T&& a) {
return lw_shared_ptr<T>::make(std::move(a));
}
template <typename T>
inline
lw_shared_ptr<T> make_lw_shared(T& a) {
return lw_shared_ptr<T>::make(a);
}
template <typename T>
inline
lw_shared_ptr<T>
enable_lw_shared_from_this<T>::shared_from_this() {
return lw_shared_ptr<T>(this);
}
template <typename T>
inline
lw_shared_ptr<const T>
enable_lw_shared_from_this<T>::shared_from_this() const {
return lw_shared_ptr<const T>(const_cast<enable_lw_shared_from_this*>(this));
}
template <typename T>
static inline
std::ostream& operator<<(std::ostream& out, const lw_shared_ptr<T>& p) {
if (!p) {
return out << "null";
}
return out << *p;
}
// Polymorphic shared pointer class
struct shared_ptr_count_base {
// destructor is responsible for fully-typed deletion
virtual ~shared_ptr_count_base() {}
shared_ptr_counter_type count = 0;
};
template <typename T>
struct shared_ptr_count_for : shared_ptr_count_base {
T data;
template <typename... A>
shared_ptr_count_for(A&&... a) : data(std::forward<A>(a)...) {}
};
template <typename T>
class enable_shared_from_this : private shared_ptr_count_base {
public:
shared_ptr<T> shared_from_this();
shared_ptr<const T> shared_from_this() const;
template <typename U>
friend class shared_ptr;
template <typename U, bool esft>
friend struct shared_ptr_make_helper;
};
template <typename T>
class shared_ptr {
mutable shared_ptr_count_base* _b = nullptr;
mutable T* _p = nullptr;
private:
explicit shared_ptr(shared_ptr_count_for<T>* b) noexcept : _b(b), _p(&b->data) {
++_b->count;
}
shared_ptr(shared_ptr_count_base* b, T* p) noexcept : _b(b), _p(p) {
if (_b) {
++_b->count;
}
}
explicit shared_ptr(enable_shared_from_this<std::remove_const_t<T>>* p) noexcept : _b(p), _p(static_cast<T*>(p)) {
if (_b) {
++_b->count;
}
}
public:
using element_type = T;
shared_ptr() noexcept = default;
shared_ptr(std::nullptr_t) noexcept : shared_ptr() {}
shared_ptr(const shared_ptr& x) noexcept
: _b(x._b)
, _p(x._p) {
if (_b) {
++_b->count;
}
}
shared_ptr(shared_ptr&& x) noexcept
: _b(x._b)
, _p(x._p) {
x._b = nullptr;
x._p = nullptr;
}
template <typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
shared_ptr(const shared_ptr<U>& x) noexcept
: _b(x._b)
, _p(x._p) {
if (_b) {
++_b->count;
}
}
template <typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
shared_ptr(shared_ptr<U>&& x) noexcept
: _b(x._b)
, _p(x._p) {
x._b = nullptr;
x._p = nullptr;
}
~shared_ptr() {
if (_b && !--_b->count) {
delete _b;
}
}
shared_ptr& operator=(const shared_ptr& x) noexcept {
if (this != &x) {
this->~shared_ptr();
new (this) shared_ptr(x);
}
return *this;
}
shared_ptr& operator=(shared_ptr&& x) noexcept {
if (this != &x) {
this->~shared_ptr();
new (this) shared_ptr(std::move(x));
}
return *this;
}
shared_ptr& operator=(std::nullptr_t) noexcept {
return *this = shared_ptr();
}
template <typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
shared_ptr& operator=(const shared_ptr<U>& x) noexcept {
if (*this != x) {
this->~shared_ptr();
new (this) shared_ptr(x);
}
return *this;
}
template <typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
shared_ptr& operator=(shared_ptr<U>&& x) noexcept {
if (*this != x) {
this->~shared_ptr();
new (this) shared_ptr(std::move(x));
}
return *this;
}
explicit operator bool() const noexcept {
return _p;
}
T& operator*() const noexcept {
return *_p;
}
T* operator->() const noexcept {
return _p;
}
T* get() const noexcept {
return _p;
}
long use_count() const noexcept {
if (_b) {
return _b->count;
} else {
return 0;
}
}
template <bool esft>
struct make_helper;
template <typename U, typename... A>
friend shared_ptr<U> make_shared(A&&... a);
template <typename U>
friend shared_ptr<U> make_shared(U&& a);
template <typename V, typename U>
friend shared_ptr<V> static_pointer_cast(const shared_ptr<U>& p);
template <typename V, typename U>
friend shared_ptr<V> dynamic_pointer_cast(const shared_ptr<U>& p);
template <typename V, typename U>
friend shared_ptr<V> const_pointer_cast(const shared_ptr<U>& p);
template <bool esft, typename... A>
static shared_ptr make(A&&... a);
template <typename U>
friend class enable_shared_from_this;
template <typename U, bool esft>
friend struct shared_ptr_make_helper;
template <typename U>
friend class shared_ptr;
};
template <typename U, bool esft>
struct shared_ptr_make_helper;
template <typename T>
struct shared_ptr_make_helper<T, false> {
template <typename... A>
static shared_ptr<T> make(A&&... a) {
return shared_ptr<T>(new shared_ptr_count_for<T>(std::forward<A>(a)...));
}
};
template <typename T>
struct shared_ptr_make_helper<T, true> {
template <typename... A>
static shared_ptr<T> make(A&&... a) {
auto p = new T(std::forward<A>(a)...);
return shared_ptr<T>(p, p);
}
};
template <typename T, typename... A>
inline
shared_ptr<T>
make_shared(A&&... a) {
using helper = shared_ptr_make_helper<T, std::is_base_of<shared_ptr_count_base, T>::value>;
return helper::make(std::forward<A>(a)...);
}
template <typename T>
inline
shared_ptr<T>
make_shared(T&& a) {
using helper = shared_ptr_make_helper<T, std::is_base_of<shared_ptr_count_base, T>::value>;
return helper::make(std::forward<T>(a));
}
template <typename T, typename U>
inline
shared_ptr<T>
static_pointer_cast(const shared_ptr<U>& p) {
return shared_ptr<T>(p._b, static_cast<T*>(p._p));
}
template <typename T, typename U>
inline
shared_ptr<T>
dynamic_pointer_cast(const shared_ptr<U>& p) {
auto q = dynamic_cast<T*>(p._p);
return shared_ptr<T>(q ? p._b : nullptr, q);
}
template <typename T, typename U>
inline
shared_ptr<T>
const_pointer_cast(const shared_ptr<U>& p) {
return shared_ptr<T>(p._b, const_cast<T*>(p._p));
}
template <typename T>
inline
shared_ptr<T>
enable_shared_from_this<T>::shared_from_this() {
auto unconst = reinterpret_cast<enable_shared_from_this<std::remove_const_t<T>>*>(this);
return shared_ptr<T>(unconst);
}
template <typename T>
inline
shared_ptr<const T>
enable_shared_from_this<T>::shared_from_this() const {
auto esft = const_cast<enable_shared_from_this*>(this);
auto unconst = reinterpret_cast<enable_shared_from_this<std::remove_const_t<T>>*>(esft);
return shared_ptr<const T>(unconst);
}
template <typename T, typename U>
inline
bool
operator==(const shared_ptr<T>& x, const shared_ptr<U>& y) {
return x.get() == y.get();
}
template <typename T>
inline
bool
operator==(const shared_ptr<T>& x, std::nullptr_t) {
return x.get() == nullptr;
}
template <typename T>
inline
bool
operator==(std::nullptr_t, const shared_ptr<T>& y) {
return nullptr == y.get();
}
template <typename T, typename U>
inline
bool
operator!=(const shared_ptr<T>& x, const shared_ptr<U>& y) {
return x.get() != y.get();
}
template <typename T>
inline
bool
operator!=(const shared_ptr<T>& x, std::nullptr_t) {
return x.get() != nullptr;
}
template <typename T>
inline
bool
operator!=(std::nullptr_t, const shared_ptr<T>& y) {
return nullptr != y.get();
}
template <typename T, typename U>
inline
bool
operator<(const shared_ptr<T>& x, const shared_ptr<U>& y) {
return x.get() < y.get();
}
template <typename T>
inline
bool
operator<(const shared_ptr<T>& x, std::nullptr_t) {
return x.get() < nullptr;
}
template <typename T>
inline
bool
operator<(std::nullptr_t, const shared_ptr<T>& y) {
return nullptr < y.get();
}
template <typename T, typename U>
inline
bool
operator<=(const shared_ptr<T>& x, const shared_ptr<U>& y) {
return x.get() <= y.get();
}
template <typename T>
inline
bool
operator<=(const shared_ptr<T>& x, std::nullptr_t) {
return x.get() <= nullptr;
}
template <typename T>
inline
bool
operator<=(std::nullptr_t, const shared_ptr<T>& y) {
return nullptr <= y.get();
}
template <typename T, typename U>
inline
bool
operator>(const shared_ptr<T>& x, const shared_ptr<U>& y) {
return x.get() > y.get();
}
template <typename T>
inline
bool
operator>(const shared_ptr<T>& x, std::nullptr_t) {
return x.get() > nullptr;
}
template <typename T>
inline
bool
operator>(std::nullptr_t, const shared_ptr<T>& y) {
return nullptr > y.get();
}
template <typename T, typename U>
inline
bool
operator>=(const shared_ptr<T>& x, const shared_ptr<U>& y) {
return x.get() >= y.get();
}
template <typename T>
inline
bool
operator>=(const shared_ptr<T>& x, std::nullptr_t) {
return x.get() >= nullptr;
}
template <typename T>
inline
bool
operator>=(std::nullptr_t, const shared_ptr<T>& y) {
return nullptr >= y.get();
}
template <typename T>
static inline
std::ostream& operator<<(std::ostream& out, const shared_ptr<T>& p) {
if (!p) {
return out << "null";
}
return out << *p;
}
template<typename T>
using shared_ptr_equal_by_value = indirect_equal_to<shared_ptr<T>>;
template<typename T>
using shared_ptr_value_hash = indirect_hash<shared_ptr<T>>;
namespace std {
template <typename T>
struct hash<lw_shared_ptr<T>> : private hash<T*> {
size_t operator()(const lw_shared_ptr<T>& p) const {
return hash<T*>::operator()(p.get());
}
};
template <typename T>
struct hash<::shared_ptr<T>> : private hash<T*> {
size_t operator()(const ::shared_ptr<T>& p) const {
return hash<T*>::operator()(p.get());
}
};
}
template<typename T>
struct is_smart_ptr<::shared_ptr<T>> : std::true_type {};
template<typename T>
struct is_smart_ptr<::lw_shared_ptr<T>> : std::true_type {};
#endif /* SHARED_PTR_HH_ */