Directory: | cvmfs/ |
---|---|
File: | cvmfs/util/shared_ptr.h |
Date: | 2025-07-13 02:35:07 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 50 | 55 | 90.9% |
Branches: | 19 | 26 | 73.1% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | */ | ||
4 | |||
5 | #ifndef CVMFS_UTIL_SHARED_PTR_H_ | ||
6 | #define CVMFS_UTIL_SHARED_PTR_H_ | ||
7 | |||
8 | #include <cstdint> | ||
9 | #include <cstdlib> | ||
10 | |||
11 | #include "util/atomic.h" | ||
12 | |||
13 | #ifdef CVMFS_NAMESPACE_GUARD | ||
14 | namespace CVMFS_NAMESPACE_GUARD { | ||
15 | #endif // CVMFS_NAMESPACE_GUARD | ||
16 | |||
17 | template<typename T> | ||
18 | class SharedPtr { | ||
19 | public: | ||
20 | typedef T element_type; | ||
21 | |||
22 | 9357 | SharedPtr() { // never throws | |
23 | 9357 | value_ = NULL; | |
24 | 9357 | count_ = NULL; | |
25 | 9357 | } | |
26 | |||
27 | template<class Y> | ||
28 | 1700 | explicit SharedPtr(Y *p) { | |
29 | 1700 | value_ = static_cast<element_type *>(p); | |
30 | 1700 | count_ = new atomic_int64; | |
31 | 1700 | atomic_write64(count_, 1); | |
32 | 1700 | } | |
33 | |||
34 | 11850 | ~SharedPtr() { // never throws | |
35 |
2/2✓ Branch 0 taken 2732 times.
✓ Branch 1 taken 4526 times.
|
11850 | if (count_) { |
36 | 2798 | atomic_dec64(count_); | |
37 |
2/2✓ Branch 1 taken 775 times.
✓ Branch 2 taken 1957 times.
|
2798 | if (atomic_read64(count_) == 0) { |
38 |
1/2✓ Branch 0 taken 775 times.
✗ Branch 1 not taken.
|
819 | delete value_; |
39 |
1/2✓ Branch 0 taken 775 times.
✗ Branch 1 not taken.
|
819 | delete count_; |
40 | } | ||
41 | } | ||
42 | 11850 | } | |
43 | |||
44 | 1614 | SharedPtr(SharedPtr const &r) | |
45 | 1614 | : value_(r.value_), count_(r.count_) { // never throws | |
46 |
1/2✓ Branch 0 taken 1581 times.
✗ Branch 1 not taken.
|
1614 | if (count_) { |
47 | 1614 | atomic_inc64(count_); | |
48 | } | ||
49 | 1614 | } | |
50 | |||
51 | template<class Y> | ||
52 | explicit SharedPtr(SharedPtr<Y> const &r) | ||
53 | : value_(r.value_), count_(r.count_) { // never throws | ||
54 | if (count_) { | ||
55 | atomic_inc64(count_); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | 2006 | SharedPtr &operator=(SharedPtr const &r) { // never throws | |
60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1202 times.
|
2006 | if (this == &r) |
61 | ✗ | return *this; | |
62 | |||
63 | 2006 | Reset(); | |
64 | 2006 | value_ = r.value_; | |
65 | 2006 | count_ = r.count_; | |
66 |
2/2✓ Branch 0 taken 398 times.
✓ Branch 1 taken 804 times.
|
2006 | if (count_) { |
67 | 398 | atomic_inc64(count_); | |
68 | } | ||
69 | 2006 | return *this; | |
70 | } | ||
71 | |||
72 | template<class Y> | ||
73 | SharedPtr &operator=(SharedPtr<Y> const &r) { // never throws | ||
74 | Reset(); | ||
75 | value_ = r.Get(); | ||
76 | count_ = r.GetCountPtr(); | ||
77 | if (count_) { | ||
78 | atomic_inc64(count_); | ||
79 | } | ||
80 | return *this; | ||
81 | } | ||
82 | |||
83 | 2028 | void Reset() { // never throws | |
84 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1116 times.
|
2028 | if (count_) { |
85 | 108 | atomic_dec64(count_); | |
86 |
2/2✓ Branch 1 taken 86 times.
✓ Branch 2 taken 22 times.
|
108 | if (atomic_read64(count_) == 0) { |
87 |
1/2✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
|
86 | delete value_; |
88 |
1/2✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
|
86 | delete count_; |
89 | } | ||
90 | 108 | value_ = NULL; | |
91 | 108 | count_ = NULL; | |
92 | } | ||
93 | 2028 | } | |
94 | |||
95 | template<class Y> | ||
96 | 11 | void Reset(Y *p) { | |
97 | 11 | Reset(); | |
98 | 11 | value_ = static_cast<element_type *>(p); | |
99 | 11 | count_ = new atomic_int64; | |
100 | 11 | atomic_write64(count_, 1); | |
101 | 11 | } | |
102 | |||
103 | ✗ | T &operator*() const { // never throws | |
104 | ✗ | return *value_; | |
105 | } | ||
106 | |||
107 | 4085 | T *operator->() const { // never throws | |
108 | 4085 | return value_; | |
109 | } | ||
110 | |||
111 | ✗ | element_type *Get() const { // never throws | |
112 | ✗ | return value_; | |
113 | } | ||
114 | |||
115 | atomic_int64 *GetCountPtr() const { return count_; } | ||
116 | |||
117 | 99 | bool Unique() const { // never throws | |
118 |
3/4✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 66 times.
✓ Branch 4 taken 33 times.
|
99 | return count_ && (atomic_read64(count_) == 1); |
119 | } | ||
120 | |||
121 | 8495 | int64_t UseCount() const { // never throws | |
122 | 8495 | return count_ ? atomic_read64(count_) : -1; | |
123 | } | ||
124 | |||
125 | private: | ||
126 | element_type *value_; | ||
127 | atomic_int64 *count_; | ||
128 | }; | ||
129 | |||
130 | template<class T, class U> | ||
131 | bool operator==(SharedPtr<T> const &a, | ||
132 | SharedPtr<U> const &b) { // never throws | ||
133 | return a.value_ == b.value_; | ||
134 | } | ||
135 | |||
136 | template<class T, class U> | ||
137 | bool operator!=(SharedPtr<T> const &a, | ||
138 | SharedPtr<U> const &b) { // never throws | ||
139 | return a.value_ != b.value_; | ||
140 | } | ||
141 | |||
142 | template<class T, class U> | ||
143 | bool operator<(SharedPtr<T> const &a, SharedPtr<U> const &b) { // never throws | ||
144 | return a.value_ < b.value_; | ||
145 | } | ||
146 | |||
147 | template<class T> | ||
148 | typename SharedPtr<T>::element_type *GetPointer( | ||
149 | SharedPtr<T> const &p) { // never throws | ||
150 | return p.value_; | ||
151 | } | ||
152 | |||
153 | template<class T, class U> | ||
154 | SharedPtr<T> StaticPointerCast(SharedPtr<U> const &r) { // never throws | ||
155 | return SharedPtr<T>(static_cast<T *>(r.value_)); | ||
156 | } | ||
157 | |||
158 | template<class T, class U> | ||
159 | SharedPtr<T> ConstPointerCast(SharedPtr<U> const &r) { // never throws | ||
160 | return SharedPtr<T>(const_cast<T *>(r.value_)); | ||
161 | } | ||
162 | |||
163 | template<class T, class U> | ||
164 | SharedPtr<T> DynamicPointerCast(SharedPtr<U> const &r) { // never throws | ||
165 | return SharedPtr<T>(dynamic_cast<T *>(r.value_)); | ||
166 | } | ||
167 | |||
168 | template<class T, class U> | ||
169 | SharedPtr<T> ReinterpretPointerCast(SharedPtr<U> const &r) { // never throws | ||
170 | return SharedPtr<T>(reinterpret_cast<T *>(r.value_)); | ||
171 | } | ||
172 | |||
173 | #ifdef CVMFS_NAMESPACE_GUARD | ||
174 | } // namespace CVMFS_NAMESPACE_GUARD | ||
175 | #endif | ||
176 | |||
177 | #endif // CVMFS_UTIL_SHARED_PTR_H_ | ||
178 |