88
99#include " cpp_utility.h"
1010
11-
12- struct RefBlockBase {
11+ struct RefBlockBase
12+ {
1313 // atomic operation
14- std::atomic<size_t > m_shared_count{1 }; // when init, create the shared_ptr and own it
15- std::atomic<size_t > m_weak_count{1 }; // when init, m_shared_count as one "weak reference"
14+ std::atomic<size_t > m_shared_count{1 }; // when init, create the shared_ptr and own it
15+ std::atomic<size_t > m_weak_count{1 }; // when init, m_shared_count as one "weak reference"
1616
1717 virtual ~RefBlockBase () = default ;
1818
1919 // type erasure
20- virtual void dispose_resource () = 0;
21- virtual void destroy_self () = 0;
22- virtual void * get_resource_ptr () { return nullptr ; }
23-
20+ virtual void
21+ dispose_resource () = 0 ;
22+ virtual void
23+ destroy_self () = 0 ;
24+ virtual void *
25+ get_resource_ptr ()
26+ {
27+ return nullptr ;
28+ }
2429
2530 // ---- thread safe counter operation ----
26- void increment_shared () noexcept {
31+ void
32+ increment_shared () noexcept
33+ {
2734 m_shared_count.fetch_add (1 , std::memory_order_relaxed);
2835 }
2936
30- void increment_weak () noexcept {
37+ void
38+ increment_weak () noexcept
39+ {
3140 m_weak_count.fetch_add (1 , std::memory_order_relaxed);
3241 }
3342
34- void decrement_shared () noexcept {
43+ void
44+ decrement_shared () noexcept
45+ {
3546 // fetch_sub return the value before minus
3647 // use acq_rel (Acquire-Release) ensure memory safe
37- if (m_shared_count.fetch_sub (1 , std::memory_order_acq_rel) == 1 ){
48+ if (m_shared_count.fetch_sub (1 , std::memory_order_acq_rel) == 1 ) {
3849 dispose_resource ();
3950 decrement_weak ();
4051 }
4152 }
4253
43- void decrement_weak () noexcept {
44- if (m_weak_count.fetch_sub (1 , std::memory_order_acq_rel) == 1 ){
54+ void
55+ decrement_weak () noexcept
56+ {
57+ if (m_weak_count.fetch_sub (1 , std::memory_order_acq_rel) == 1 ) {
4558 // weak counter oges 0, destroy control block
4659 destroy_self ();
4760 }
4861 }
4962
50- bool try_increment_shared () noexcept {
63+ bool
64+ try_increment_shared () noexcept
65+ {
5166 size_t count = m_shared_count.load (std::memory_order_relaxed);
5267
53- while (count != 0 ){
68+ while (count != 0 ) {
5469 // try to replace count with count + 1
55- if (m_shared_count.compare_exchange_weak (count, count + 1 , std::memory_order_acq_rel)){
70+ if (m_shared_count.compare_exchange_weak (count, count + 1 , std::memory_order_acq_rel)) {
5671 return true ; // success
5772 }
5873 }
@@ -62,44 +77,52 @@ struct RefBlockBase {
6277
6378// for 'new'
6479// Y is the actual type, D is the del type
65- template <typename Y, typename D>
66- struct RefBlockImpl : public RefBlockBase {
80+ template <typename Y, typename D> struct RefBlockImpl : public RefBlockBase
81+ {
6782 Y* m_resource;
6883 D m_deleter;
6984
70- RefBlockImpl (Y* res, D del)
71- : m_resource(res), m_deleter(utility::move(del)){}
85+ RefBlockImpl (Y* res, D del) : m_resource(res), m_deleter(utility::move(del)) {}
7286
73- void dispose_resource () override {
87+ void
88+ dispose_resource () override
89+ {
7490 // call the deleter
7591 m_deleter (m_resource);
7692 }
7793
78- void destroy_self () override {
94+ void
95+ destroy_self () override
96+ {
7997 // destroy self
8098 delete this ;
8199 }
82100};
83101
84- template <typename T>
85- struct RefBlockMakeShared : public RefBlockBase {
102+ template <typename T> struct RefBlockMakeShared : public RefBlockBase
103+ {
86104 // T's data will followed directly after this struct
87105 // use an aligned char array for padding
88106 alignas (T) char m_storage[sizeof (T)];
89107
90- void * get_resource_ptr () override {
108+ void *
109+ get_resource_ptr () override
110+ {
91111 return reinterpret_cast <T*>(m_storage);
92112 }
93113
94- void dispose_resource () override {
114+ void
115+ dispose_resource () override
116+ {
95117 // call the deconstruct but not release the memory
96- reinterpret_cast <T*>(m_storage) -> ~T ();
118+ reinterpret_cast <T*>(m_storage)-> ~T ();
97119 }
98120
99- void destroy_self () override {
121+ void
122+ destroy_self () override
123+ {
100124 delete this ;
101125 }
102126};
103127
104128#endif
105-
0 commit comments