/
counted.cpp
82 lines (72 loc) · 2.07 KB
/
counted.cpp
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
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2004-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Counted"
namespace de {
#ifdef DENG2_DEBUG
int Counted::totalCount = 0; ///< Should return back to zero when program ends.
# ifdef DENG_USE_COUNTED_TRACING
static QHash<void *, QByteArray> countedAllocs;
void Counted::printAllocs()
{
qDebug() << "Counted objects:" << Counted::totalCount;
for(auto i = countedAllocs.constBegin(); i != countedAllocs.constEnd(); ++i)
{
qDebug() << "-=- Object" << i.key();
qDebug() << i.value();
}
}
# endif
#endif
Counted::Counted() : _refCount(1)
{
#ifdef DENG2_DEBUG
totalCount++;
# ifdef DENG_USE_COUNTED_TRACING
QString trace;
DENG2_BACKTRACE(32, trace);
countedAllocs[this] = trace.toLatin1();
# endif
#endif
}
Counted::~Counted()
{
#ifdef DENG2_DEBUG
totalCount--;
# ifdef DENG_USE_COUNTED_TRACING
countedAllocs.remove(this);
# endif
#endif
DENG2_ASSERT(_refCount == 0);
}
void Counted::release() const
{
//qDebug() << "Counted" << c << typeid(*c).name() << "ref dec'd to" << c->_refCount - 1;
DENG2_ASSERT(_refCount > 0);
if(!--_refCount)
{
delete this;
}
}
void Counted::addRef(dint count) const
{
DENG2_ASSERT(_refCount >= 0);
_refCount += count;
DENG2_ASSERT(_refCount >= 0);
}
} // namespace de