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
|
//+---------------------------------------------------------------------------
//
// Microsoft Windows
// Copyright (C) 1992, Microsoft Corporation.
//
// File: otrack.hxx
//
// Contents: This file defines facilities for a standard implementation
// of reference-counted objects, incorporating mechanisms for
// tracking the usage history of the objects.
//
// Classes: CRefcountedObject
//
// History: 6-Apr-92 MikeSe Created
//
//----------------------------------------------------------------------------
#ifndef __OTRACK_HXX__
#define __OTRACK_HXX__
#include <windows.h>
//+-------------------------------------------------------------------------
//
// Class: CRefcountedObject (rco)
//
// Purpose: Provides basis for tracking (OLE) objects (aka interface handles)
//
// History: 6-Apr-92 MikeSe Created
//
// Notes: Access to this class is only indirect, through the macros
// defined later.
//
//--------------------------------------------------------------------------
class CRefcountedObject
{
protected:
CRefcountedObject ()
:_cReferences(1)
{
# if DBG == 1
_CRefcountedObject();
# endif
}
ULONG TrackAddRef ( void );
ULONG TrackRelease ( void );
void TrackClassName ( char * pszName );
# if DBG == 1
~CRefcountedObject()
{
DestroyRefcounted();
}
# endif
private:
void _CRefcountedObject ( void );
void DestroyRefcounted ( void );
# if DBG == 1
BOOL IsClassTracking( char * pszName );
static BOOL _TrackAll;
public:
// dumps information on all outstanding objects
static void DumpTrackingInfo ( int fDeleteNode = 0 );
static void TrackClass(BOOL, char * pszName );
# endif // DBG == 1
protected:
unsigned long _cReferences;
};
//+-------------------------------------------------------------------------
//
// The following macros encapsulate use of the above
//
// INHERIT_TRACKING:
//
// For any class which implements a Cairo interface, add this macro
// in the class declaration, eg:
//
// class CMyFoo: INHERIT_TRACKING, IFoo
//
// Do not repeat this in any subclass. If both INHERIT_UNWIND and
// INHERIT_TRACKING are used, the former should appear first.
# define INHERIT_TRACKING protected CRefcountedObject
// The following declarations are for non-retail builds
# if DBG == 1
//
// DECLARE_STD_REFCOUNTING:
//
// To make use of standard refcounting code, place the above
// in the class declaration, in the public method section. This
// macro defines the AddRef and Release methods for the class.
//
# define DECLARE_STD_REFCOUNTING \
STDMETHOD_(ULONG, AddRef) () \
{ \
return TrackAddRef(); \
}; \
STDMETHOD_(ULONG, Release) () \
{ \
ULONG ul = TrackRelease(); \
if ( ul==0 ) delete this; \
return ul; \
};
//
// ENLIST_TRACKING(class name)
//
// Place an invocation of this in each constructor, at any appropriate
// point (generally immediately before returning from the constructor).
//
// ENLIST_TRACKING(CMyFoo)
//
# define ENLIST_TRACKING(cls) TrackClassName( #cls )
//
// NB: In a subclass of a class which has INHERIT_TRACKING, do not
// use INHERIT_TRACKING again. However, do use ENLIST_TRACKING in
// in the constructor of the derived class, and use TRACK_ADDREF
// and TRACK_RELEASE if the subclass overrides the AddRef and Release
// methods.
//
// TRACK_CLASS(fTrack, pszClassName)
//
// Use this to add or remove a class in the list of tracked classes.
// You can turn tracking of all classes on or off by using a NULL
// pszClassName. If fTrack is TRUE, the class will be tracked, if FALSE,
// the class will no longer be tracked. The default configuration is
// that all classes are tracked.
//
// NOTE: this affects only objects created after this macro is executed.
//
# define TRACK_CLASS(fTrack, cls) \
CRefcountedObject::TrackClass( fTrack, cls )
//
// DUMP_TRACKING_INFO()
//
// Place this anywhere it would be useful to dump out the object
// tracking database. By default, this is always issued at program
// termination.
//
# define DUMP_TRACKING_INFO() DUMP_TRACKING_INFO_KEEP()
# define DUMP_TRACKING_INFO_KEEP() CRefcountedObject::DumpTrackingInfo(0)
# define DUMP_TRACKING_INFO_DELETE() CRefcountedObject::DumpTrackingInfo(1)
// Output from this is controlled by setting the following mask values
// in OtInfoLevel
# define DEB_OT_OBJECTS 0x00000001L
// display object addresses, reference count and name (Default)
# define DEB_OT_CALLERS 0x80000000L
// display call history
// In addition, set the following values to cause debug output during
// operation:
# define DEB_OT_ERRORS 0x00000002L
// report errors during tracking operations (Default)
# define DEB_OT_ACTIONS 0x40000000L
// report each create, addref and release
# define DEB_OT_DELETE 0x20000000L
// display call history at object delete
# else // DBG == 0
# define DECLARE_STD_REFCOUNTING \
STDMETHOD_(ULONG, AddRef) () \
{ \
return InterlockedIncrement((long*)&_cReferences); \
}; \
STDMETHOD_(ULONG, Release) () \
{ \
ULONG ul = (ULONG)InterlockedDecrement((long*)&_cReferences); \
if ( ul == 0 ) \
{ \
delete this; \
return 0; \
} \
else \
return ul; \
};
# define ENLIST_TRACKING(cls)
# define DUMP_TRACKING_INFO()
# define TRACK_CLASS(fTrack, cls)
# endif // DBG == 0
#endif // of ifndef __OTRACK_HXX__
|