Loading...
Searching...
No Matches
refPtrTracker.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_BASE_TF_REF_PTR_TRACKER_H
25#define PXR_BASE_TF_REF_PTR_TRACKER_H
26
28
29#include "pxr/pxr.h"
30
31#include "pxr/base/tf/api.h"
32#include "pxr/base/tf/hash.h"
33#include "pxr/base/tf/hashmap.h"
36#include <iosfwd>
37#include <mutex>
38#include <vector>
39
40PXR_NAMESPACE_OPEN_SCOPE
41
42class TfRefBase;
43template <class T> class TfRefPtr;
44
97 TfRefPtrTracker(const TfRefPtrTracker&) = delete;
98 TfRefPtrTracker& operator=(const TfRefPtrTracker&) = delete;
99public:
100 enum TraceType { Add, Assign };
101
102 TF_API static TfRefPtrTracker& GetInstance()
103 {
105 }
106
108 TF_API
109 size_t GetStackTraceMaxDepth() const;
110
112 TF_API
114
116 struct Trace {
118 std::vector<uintptr_t> trace;
119
122
124 TraceType type;
125 };
126
128 typedef TfHashMap<const void*, Trace, TfHash> OwnerTraces;
129
133 typedef TfHashMap<const TfRefBase*, size_t, TfHash> WatchedCounts;
134
137 TF_API
139
141 TF_API
143
146 TF_API
147 void ReportAllWatchedCounts(std::ostream& stream) const;
148
150 TF_API
151 void ReportAllTraces(std::ostream& stream) const;
152
154 TF_API
155 void ReportTracesForWatched(std::ostream& stream,
156 const TfRefBase* watched) const;
157
163 static bool WatchNone(const void*)
164 {
165 return false;
166 }
167
170 static bool WatchAll(const void*)
171 {
172 return true;
173 }
174
175private:
178
180 void _Watch(const TfRefBase* obj);
181
183 void _Unwatch(const TfRefBase* obj);
184
187 void _AddTrace(const void* owner, const TfRefBase* obj, TraceType = Add);
188
190 void _RemoveTraces(const void* owner);
191
192private:
193 typedef std::mutex _Mutex;
194 typedef std::lock_guard<std::mutex> _Lock;
195 mutable _Mutex _mutex;
196 size_t _maxDepth;
197 WatchedCounts _watched;
198 OwnerTraces _traces;
199
200 friend class Tf_RefPtrTrackerUtil;
201 friend class TfSingleton<TfRefPtrTracker>;
202};
203
204TF_API_TEMPLATE_CLASS(TfSingleton<TfRefPtrTracker>);
205
206// For internal use only.
207class Tf_RefPtrTrackerUtil {
208public:
210 static void Watch(const TfRefBase* obj)
211 {
212 TfRefPtrTracker::GetInstance()._Watch(obj);
213 }
214
216 static void Unwatch(const TfRefBase* obj)
217 {
218 TfRefPtrTracker::GetInstance()._Unwatch(obj);
219 }
220
223 static void AddTrace(const void* owner, const TfRefBase* obj,
224 TfRefPtrTracker::TraceType type = TfRefPtrTracker::Add)
225 {
226 TfRefPtrTracker::GetInstance()._AddTrace(owner, obj, type);
227 }
228
230 static void RemoveTraces(const void* owner)
231 {
232 TfRefPtrTracker::GetInstance()._RemoveTraces(owner);
233 }
234};
235
236#define TF_DECLARE_REFPTR_TRACK(T) \
237inline void Tf_RefPtrTracker_FirstRef(const void*, T* obj); \
238inline void Tf_RefPtrTracker_LastRef(const void*, T* obj); \
239inline void Tf_RefPtrTracker_New(const void* owner, T* obj); \
240inline void Tf_RefPtrTracker_Delete(const void* owner, T* obj); \
241inline void Tf_RefPtrTracker_Assign(const void* owner, T* obj, T* oldObj);
242
243#define TF_DEFINE_REFPTR_TRACK(T, COND) \
244inline void Tf_RefPtrTracker_FirstRef(const void*, T* obj) { \
245 if (obj && COND(obj)) Tf_RefPtrTrackerUtil::Watch(obj); \
246} \
247inline void Tf_RefPtrTracker_LastRef(const void*, T* obj) { \
248 Tf_RefPtrTrackerUtil::Unwatch(obj); \
249} \
250inline void Tf_RefPtrTracker_New(const void* owner, T* obj) { \
251 Tf_RefPtrTrackerUtil::AddTrace(owner, obj); \
252} \
253inline void Tf_RefPtrTracker_Delete(const void* owner, T* obj) { \
254 Tf_RefPtrTrackerUtil::RemoveTraces(owner); \
255} \
256inline void Tf_RefPtrTracker_Assign(const void* owner, T* obj, T* oldObj) { \
257 if (oldObj != obj) { \
258 Tf_RefPtrTrackerUtil::AddTrace(owner, obj, TfRefPtrTracker::Assign);\
259 } \
260}
261
262PXR_NAMESPACE_CLOSE_SCOPE
263
264#endif
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:73
Reference-counted smart pointer utility class.
Definition: refPtr.h:601
Provides tracking of TfRefPtr objects to particular objects.
Definition: refPtrTracker.h:96
const TfRefBase * obj
The object being pointed to.
TF_API OwnerTraces GetAllTraces() const
Returns traces for all owners. Returns a copy for thread safety.
TF_API void ReportAllWatchedCounts(std::ostream &stream) const
Writes all watched objects and the number of owners of each to stream.
TF_API void ReportAllTraces(std::ostream &stream) const
Writes all traces to stream.
TF_API size_t GetStackTraceMaxDepth() const
Returns the maximum stack trace depth.
TfHashMap< const void *, Trace, TfHash > OwnerTraces
Maps a TfRefPtr address to the most recent trace for it.
std::vector< uintptr_t > trace
The stack trace when the TfRefPtr was created or assigned to.
TfHashMap< const TfRefBase *, size_t, TfHash > WatchedCounts
Maps a TfRefBase object pointer to the number of TfRefPtr objects using it.
TF_API WatchedCounts GetWatchedCounts() const
Returns the watched objects and the number of owners of each.
TF_API void ReportTracesForWatched(std::ostream &stream, const TfRefBase *watched) const
Writes traces for all owners of watched.
TraceType type
Whether the TfRefPtr was created or assigned to.
TF_API void SetStackTraceMaxDepth(size_t)
Sets the maximum stack trace depth.
static bool WatchAll(const void *)
Handy function to pass as second argument to TF_DEFINE_REFPTR_TRACK.
static bool WatchNone(const void *)
Handy function to pass as second argument to TF_DEFINE_REFPTR_TRACK.
Manage a single instance of an object (see.
Definition: singleton.h:122
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:137
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141
Manage a single instance of an object.