line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------- |
2
|
|
|
|
|
|
|
| Copyright (c) 2013-2017, Nucleic Development Team. |
3
|
|
|
|
|
|
|
| |
4
|
|
|
|
|
|
|
| Distributed under the terms of the Modified BSD License. |
5
|
|
|
|
|
|
|
| |
6
|
|
|
|
|
|
|
| The full license is in the file LICENSE, distributed with this software. |
7
|
|
|
|
|
|
|
|----------------------------------------------------------------------------*/ |
8
|
|
|
|
|
|
|
#pragma once |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
/* |
11
|
|
|
|
|
|
|
Implementation note |
12
|
|
|
|
|
|
|
=================== |
13
|
|
|
|
|
|
|
SharedDataPtr/SharedData offer the same basic functionality as std::shared_ptr, |
14
|
|
|
|
|
|
|
but do not use atomic counters under the hood. |
15
|
|
|
|
|
|
|
Since kiwi operates within a single thread context, atomic counters are not necessary, |
16
|
|
|
|
|
|
|
especially given the extra CPU cost. |
17
|
|
|
|
|
|
|
Therefore the use of SharedDataPtr/SharedData is preferred over std::shared_ptr. |
18
|
|
|
|
|
|
|
*/ |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
namespace kiwi |
21
|
|
|
|
|
|
|
{ |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
class SharedData |
24
|
|
|
|
|
|
|
{ |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
public: |
27
|
6817
|
|
|
|
|
|
SharedData() : m_refcount(0) {} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
SharedData(const SharedData &other) = delete; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
SharedData(SharedData&& other) = delete; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
int m_refcount; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
SharedData &operator=(const SharedData &other) = delete; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
SharedData &operator=(SharedData&& other) = delete; |
38
|
|
|
|
|
|
|
}; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
template <typename T> |
41
|
|
|
|
|
|
|
class SharedDataPtr |
42
|
|
|
|
|
|
|
{ |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
public: |
45
|
|
|
|
|
|
|
using Type = T; |
46
|
|
|
|
|
|
|
|
47
|
1438
|
|
|
|
|
|
SharedDataPtr() : m_data(nullptr) {} |
48
|
|
|
|
|
|
|
|
49
|
6817
|
|
|
|
|
|
explicit SharedDataPtr(T *data) : m_data(data) |
50
|
|
|
|
|
|
|
{ |
51
|
6817
|
|
|
|
|
|
incref(m_data); |
52
|
6817
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
73410
|
|
|
|
|
|
~SharedDataPtr() |
55
|
|
|
|
|
|
|
{ |
56
|
73410
|
|
|
|
|
|
decref(m_data); |
57
|
73410
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
T *data() |
60
|
|
|
|
|
|
|
{ |
61
|
|
|
|
|
|
|
return m_data; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
const T *data() const |
65
|
|
|
|
|
|
|
{ |
66
|
|
|
|
|
|
|
return m_data; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
operator T *() |
70
|
|
|
|
|
|
|
{ |
71
|
|
|
|
|
|
|
return m_data; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
operator const T *() const |
75
|
|
|
|
|
|
|
{ |
76
|
|
|
|
|
|
|
return m_data; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
16433
|
|
|
|
|
|
T *operator->() |
80
|
|
|
|
|
|
|
{ |
81
|
16433
|
|
|
|
|
|
return m_data; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
14866
|
|
|
|
|
|
const T *operator->() const |
85
|
|
|
|
|
|
|
{ |
86
|
14866
|
|
|
|
|
|
return m_data; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
T &operator*() |
90
|
|
|
|
|
|
|
{ |
91
|
|
|
|
|
|
|
return *m_data; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
const T &operator*() const |
95
|
|
|
|
|
|
|
{ |
96
|
|
|
|
|
|
|
return *m_data; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
bool operator!() const |
100
|
|
|
|
|
|
|
{ |
101
|
0
|
|
|
|
|
|
return !m_data; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
157761
|
|
|
|
|
|
bool operator<(const SharedDataPtr<T> &other) const |
105
|
|
|
|
|
|
|
{ |
106
|
157761
|
|
|
|
|
|
return m_data < other.m_data; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
2
|
|
|
|
|
|
bool operator==(const SharedDataPtr<T> &other) const |
110
|
|
|
|
|
|
|
{ |
111
|
2
|
|
|
|
|
|
return m_data == other.m_data; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
bool operator!=(const SharedDataPtr<T> &other) const |
115
|
|
|
|
|
|
|
{ |
116
|
|
|
|
|
|
|
return m_data != other.m_data; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
50582
|
|
|
|
|
|
SharedDataPtr(const SharedDataPtr<T> &other) : m_data(other.m_data) |
120
|
|
|
|
|
|
|
{ |
121
|
50582
|
|
|
|
|
|
incref(m_data); |
122
|
50582
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
28383
|
|
|
|
|
|
SharedDataPtr(SharedDataPtr&& other) noexcept : m_data(other.m_data) |
125
|
|
|
|
|
|
|
{ |
126
|
28383
|
|
|
|
|
|
other.m_data = nullptr; |
127
|
28383
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
1438
|
|
|
|
|
|
SharedDataPtr<T> &operator=(const SharedDataPtr<T> &other) |
130
|
|
|
|
|
|
|
{ |
131
|
1438
|
0
|
|
|
|
|
if (m_data != other.m_data) |
|
|
50
|
|
|
|
|
|
132
|
|
|
|
|
|
|
{ |
133
|
1438
|
|
|
|
|
|
T *temp = m_data; |
134
|
1438
|
|
|
|
|
|
m_data = other.m_data; |
135
|
1438
|
|
|
|
|
|
incref(m_data); |
136
|
1438
|
|
|
|
|
|
decref(temp); |
137
|
|
|
|
|
|
|
} |
138
|
1438
|
|
|
|
|
|
return *this; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
205087
|
|
|
|
|
|
SharedDataPtr<T>& operator=(SharedDataPtr<T>&& other) noexcept |
142
|
|
|
|
|
|
|
{ |
143
|
205087
|
50
|
|
|
|
|
if (m_data != other.m_data) |
|
|
100
|
|
|
|
|
|
144
|
|
|
|
|
|
|
{ |
145
|
204903
|
|
|
|
|
|
T *temp = m_data; |
146
|
204903
|
|
|
|
|
|
m_data = other.m_data; |
147
|
204903
|
|
|
|
|
|
other.m_data = nullptr; |
148
|
204903
|
|
|
|
|
|
decref(temp); |
149
|
|
|
|
|
|
|
} |
150
|
205087
|
|
|
|
|
|
return *this; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
SharedDataPtr<T> &operator=(T *other) |
154
|
|
|
|
|
|
|
{ |
155
|
|
|
|
|
|
|
if (m_data != other) |
156
|
|
|
|
|
|
|
{ |
157
|
|
|
|
|
|
|
T *temp = m_data; |
158
|
|
|
|
|
|
|
m_data = other; |
159
|
|
|
|
|
|
|
incref(m_data); |
160
|
|
|
|
|
|
|
decref(temp); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
return *this; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
private: |
166
|
58837
|
|
|
|
|
|
static void incref(T *data) |
167
|
|
|
|
|
|
|
{ |
168
|
58837
|
100
|
|
|
|
|
if (data) |
|
|
50
|
|
|
|
|
|
169
|
58118
|
|
|
|
|
|
++data->m_refcount; |
170
|
58837
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
279751
|
|
|
|
|
|
static void decref(T *data) |
173
|
|
|
|
|
|
|
{ |
174
|
279751
|
100
|
|
|
|
|
if (data && --data->m_refcount == 0) |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
175
|
719
|
50
|
|
|
|
|
delete data; |
|
|
0
|
|
|
|
|
|
176
|
279751
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
T *m_data; |
179
|
|
|
|
|
|
|
}; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
} // namespace kiwi |