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
|
#pragma once
#include "base/assert.hpp"
#include "base/mutex.hpp"
#include "std/map.hpp"
#include "std/typeinfo.hpp"
namespace dp
{
class PointerTracker
{
public:
~PointerTracker();
template <typename T>
void Ref(T * p, bool needDestroyCheck)
{
threads::MutexGuard g(m_mutex);
if (p == NULL)
return;
map_t::iterator it = m_countMap.find(p);
if (it == m_countMap.end())
{
m_countMap.insert(make_pair((void *)p, make_pair(1, typeid(p).name())));
if (needDestroyCheck)
m_alivePointers.insert(p);
}
else
it->second.first++;
}
void Deref(void * p);
void Destroy(void * p);
private:
typedef map<void *, pair<int, string> > map_t;
map_t m_countMap;
typedef set<void *> alive_pointers_t;
alive_pointers_t m_alivePointers;
threads::Mutex m_mutex;
};
#define DISABLE_DEBUG_PRT_TRACKING
#if defined(DEBUG) && !defined(DISABLE_DEBUG_PRT_TRACKING)
#define CHECK_POINTERS
#endif
#if defined(CHECK_POINTERS)
extern PointerTracker g_tracker;
#define REF_POINTER(p, c) g_tracker.Ref(p, c)
#define DEREF_POINTER(p) g_tracker.Deref(p)
#define DESTROY_POINTER(p) g_tracker.Destroy(p)
#define DECLARE_CHECK bool m_checkOnDestroy
#define DECLARE_CHECK_GET bool IsCheckOnDestroy() const { return m_checkOnDestroy; }
#define DECLARE_CHECK_SET void SetCheckOnDestroy(bool doCheck) { m_checkOnDestroy = doCheck; }
#define SET_CHECK_FLAG(x) SetCheckOnDestroy(x)
#define GET_CHECK_FLAG(x) (x).IsCheckOnDestroy()
#define ASSERT_CHECK_FLAG(x) ASSERT(GET_CHECK_FLAG(x), ())
#else
#define REF_POINTER(p, c)
#define DEREF_POINTER(p)
#define DESTROY_POINTER(p)
#define DECLARE_CHECK
#define DECLARE_CHECK_GET
#define DECLARE_CHECK_SET
#define SET_CHECK_FLAG(x)
#define GET_CHECK_FLAG(x) false
#define ASSERT_CHECK_FLAG(x)
#endif
template <typename T>
class DrapePointer
{
public:
DrapePointer() : m_p(NULL) { SET_CHECK_FLAG(true); }
bool operator==(DrapePointer<T> const & other) const
{
return m_p == other.m_p;
}
protected:
DrapePointer(T * p, bool needDestroyedCheck = true)
: m_p(p)
{
SET_CHECK_FLAG(needDestroyedCheck);
REF_POINTER(m_p, GET_CHECK_FLAG(*this));
}
DrapePointer(DrapePointer<T> const & other)
: m_p(NULL)
{
SET_CHECK_FLAG(GET_CHECK_FLAG(other));
Reset(other.GetNonConstRaw());
}
DrapePointer<T> & operator=(DrapePointer<T> const & other)
{
SET_CHECK_FLAG(GET_CHECK_FLAG(other));
Reset(other.GetNonConstRaw());
return *this;
}
void Destroy()
{
DESTROY_POINTER(m_p);
delete m_p;
DEREF_POINTER(m_p);
m_p = NULL;
}
void Reset(T * p)
{
ResetImpl(p);
}
T * GetRaw() { return m_p; }
T const * GetRaw() const { return m_p; }
T * GetNonConstRaw() const { return m_p; }
DECLARE_CHECK_GET;
DECLARE_CHECK_SET;
// Need to be const for copy constructor and assigment operator of TransfromPointer
void SetToNull() const
{
ResetImpl(NULL);
}
private:
void ResetImpl(T * p) const
{
DEREF_POINTER(m_p);
m_p = p;
REF_POINTER(m_p, GET_CHECK_FLAG(*this));
}
private:
// Mutable for Move method
mutable T * m_p;
DECLARE_CHECK;
};
template <typename T> class MasterPointer;
template <typename T>
class TransferPointer : public DrapePointer<T>
{
typedef DrapePointer<T> base_t;
public:
TransferPointer(TransferPointer<T> const & other)
: base_t(other)
{
ASSERT_CHECK_FLAG(other);
other.SetToNull();
}
TransferPointer<T> & operator=(TransferPointer<T> const & other)
{
ASSERT_CHECK_FLAG(other);
base_t::operator =(other);
other.SetToNull();
return *this;
}
~TransferPointer()
{
ASSERT(base_t::GetRaw() == NULL, ());
Destroy();
}
void Destroy() { base_t::Destroy(); }
// IsNull need for test
bool IsNull() { return base_t::GetRaw() == NULL; }
private:
friend class MasterPointer<T>;
TransferPointer() {}
explicit TransferPointer(T * p) : base_t(p) {}
};
template <typename T> class RefPointer;
template <typename T> RefPointer<T> MakeStackRefPointer(T * p);
template<typename T>
class RefPointer : public DrapePointer<T>
{
typedef DrapePointer<T> base_t;
public:
RefPointer() : base_t() {}
~RefPointer() { base_t::Reset(NULL); }
template <typename Y>
RefPointer(RefPointer<Y> const & p) : base_t(p.GetNonConstRaw(), GET_CHECK_FLAG(p)) {}
bool IsContentLess(RefPointer<T> const & other) const { return *GetRaw() < *other.GetRaw(); }
bool IsNull() const { return base_t::GetRaw() == NULL; }
T * operator->() { return base_t::GetRaw(); }
T const * operator->() const { return base_t::GetRaw(); }
T * GetRaw() { return base_t::GetRaw(); }
T const * GetRaw() const { return base_t::GetRaw(); }
private:
template <typename Y> friend class RefPointer;
friend class MasterPointer<T>;
friend RefPointer<T> MakeStackRefPointer<T>(T *);
explicit RefPointer(T * p, bool needDestroyedCheck = true) : base_t(p, needDestroyedCheck) {}
};
template <typename T>
RefPointer<T> MakeStackRefPointer(T * p) { return RefPointer<T>(p, false); }
template <typename T>
class MasterPointer : public DrapePointer<T>
{
typedef DrapePointer<T> base_t;
public:
MasterPointer() : base_t() {}
explicit MasterPointer(T * p) : base_t(p) {}
explicit MasterPointer(TransferPointer<T> & transferPointer)
{
Reset(transferPointer.GetRaw());
transferPointer.Reset(NULL);
}
~MasterPointer()
{
base_t::Reset(NULL);
}
RefPointer<T> GetRefPointer() const
{
return RefPointer<T>(base_t::GetNonConstRaw());
}
TransferPointer<T> Move()
{
TransferPointer<T> result(GetRaw());
base_t::Reset(NULL);
return result;
}
void Destroy()
{
Reset(NULL);
}
void Reset(T * p)
{
base_t::Destroy();
base_t::Reset(p);
}
bool IsNull() const { return base_t::GetRaw() == NULL; }
T * operator->() { return base_t::GetRaw(); }
T const * operator->() const { return base_t::GetRaw(); }
T * GetRaw() { return base_t::GetRaw(); }
T const * GetRaw() const { return base_t::GetRaw(); }
};
template<typename T>
TransferPointer<T> MovePointer(T * p)
{
return MasterPointer<T>(p).Move();
}
template <typename T>
T * NonConstGetter(dp::MasterPointer<T> & p)
{
return p.GetRaw();
}
struct MasterPointerDeleter
{
template <typename Y, typename T>
void operator() (pair<Y, MasterPointer<T> > & value)
{
Destroy(value.second);
}
template <typename T>
void operator() (MasterPointer<T> & value)
{
Destroy(value);
}
private:
template <typename T>
void Destroy(MasterPointer<T> & value)
{
value.Destroy();
}
};
} // namespace dp
|